Jerry Nixon @Work: Metro Answers: Can I use the Serial Port?

Jerry Nixon on Windows

Tuesday, May 15, 2012

Metro Answers: Can I use the Serial Port?

image

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:

  1. Open Device Access Model
    Any application can access the device. For example, a camera, maybe a pedometer.
  2. Restricted Device Access Model
    Restricts which app(s) may access devices, For example, a credit card reader.

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

There are some broad categories of devices:

  1. Printers
  2. Mobile Broadband
  3. Cameras
  4. Networked Entertainment Devices
  5. Specialized

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:

  1. Windows Portable Device (WPD). For example, a phone.
  2. Windows.Storage. For example, an external drive.
  3. 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.
  4. 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.

image

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.

Reference: CreateDeviceAccessInstance

Creates the object that's used to access a device. The instantiated object implements the IDeviceIoControl and ICreateDeviceAccessAsync interfaces.

Reference: DeviceIoControl

Sends a control code to a device driver.This action causes the device to perform the corresponding operation.

Documentation: Windows Driver Kit

The Windows Driver Kit (WDK) 8 documentation provides information about driver development for Windows 8 Consumer Preview and earlier versions of Windows.

You might also like this sample: Custom Driver Access (once it is published!)

Special thanks to George Roussos and Nar Ganapoathy. And to their excellent //Build/ session “Building apps that connect to specialized devices”. I recommend it.

Best of luck!