Jerry Nixon on Windows

Tuesday, May 15, 2012

Metro Answers: Can I use the Serial Port?

The Question

Using a WinRT Metro style application, is it possible to access custom devices that connect using a serial port?

The Short Answer

The short answer is no. With this exception: the device is on USB (with a dongle) with a modern driver exposing IOCTLS device information – this gives Metro applications device access.

The Long Answer

On the desktop (and Windows 7), applications access devices through the I/O manager. The I/O manager does not care which application is accessing a device. The I/O manager only cares which user is accessing a device. If there is device security, this is it.

User Controls Capabilities

Metro applications use the I/O manager, too. However, Metro security is different. Device access is restricted by application. This allows users to grant application capabilities. As a result, the Device Access Broker stands in between devices and apps.

Metro applications run in a secure environment called the Application Container. This puts the user in control to choose what resources an application can use. The Device Access Broker enforces the capability permissions granted by the user.

This is the reason low level APIs cannot be opened to applications. When the user is granting access – granting “all USB access” is something the typical user will not understand and therefore cannot securely provision.

Vendor Controls Access

Hardware vendors also have the ability to restrict access to their device. Scenarios may exist where access to a device is private or dangerous. Metro supports two access models:

Open Device Access ModelAny application can access the device. For example, a camera, maybe a pedometer.

The Companion Application section in the Device Metadata instructs what to automatically download when you plug in the device. It also allows to you restrict access to your device based on the consuming Metro application’s GUID.

Types of Devices

MSDN: Manufactures of almost any connected device can create tailored device experiences that leverage the unique capabilities of their device through a Metro style device app. Similarly, system makers can leverage embedded functionality within Metro style device apps. Devices that don’t have a built-in user experience in Windows 8 are known as specialized devices. A range of options for developing specialized devices is available, from using in-box device drivers to providing custom functionality through proprietary device drivers.

Types of APIs

Certain Windows APIs are restricted. For those, there are alternatives. Moreover, there are broad categories of APIs for Metro device access:

Windows.Networking. There are 2 types of networked devices – those on the local network and those on the internet. UPnP and Web Services for Discovery are the two protocols supported to make device services discoverable. PnPX extensions instructs windows about the device and what needs to be automatically downloaded for it.

DeviceIoControl. This requires a custom driver. The devices must be marked as restricted. This is important because restricted is required in order to allow Metro applications to access it. This is not required for desktop applications to access it.

The C++ Requirement

These are are available in C++ only – which allows high performance buffer manipulation and management. This is important to deliver the best experience to your device, your app, and (most of all) the user. This means, if you are writing a C# application, you will need to have a hybrid application.

Hybrid applications allow developers to write components in C++, C# or VB.Net and wrap them as WinMD (MD for Metadata) components. These can be consumed by any language, including JavaScript. This provides a bridge for language specific features (as described above) or to share existing assets across languages.

Hey! I'm Jerry Nixon, a Microsoft Developer Evangelist in Colorado. If you are into Windows and Windows Phone, you are at the right place. I blog, I teach, and I speak; should you see me out somewhere, say hi! Thanks for your visit.