building apps for usb accessories develop apps that can connect with hardware from the past and...
TRANSCRIPT
Session 3-81
Building apps for USB Accessories
George Roussos Arvind AiyarSenior Program Manager Senior Software Engineer
Develop apps that can connect with hardware from the past and present
Windows 10 introduces
1) USB Accessories on Phones2) Windows.Devices.SerialCommunication
enabling you to develop apps that connect with hardware from the past and present.
USB Accessories1. Typical accessories and demo2. Vendor specific accessories and
demo
Serial Communication3. Overview and demo4. Getting Started5. Device discovery6. Basic communication7. Additional features
Resources and Call To Action
Agenda
• Windows 10 Mobile SKU adds support for USB connected accessories on new Phones and Tablets via USB Dual Role.
• Developers can build accessories based upon open industry standard hardware without per-unit royalties.
USB Accessories
Windows 10 supports two kinds of USB Accessories in Mobile SKU:
1. Typical accessories2. Vendor specific
accessories
USB Accessories in Windows 10 Mobile SKU
Click icon to add picture
Typical accessories on Mobile SKUAccessories that just work with OS
• Windows natively supports these accessory types regardless of who makes the hardware.
• Windows provides built-in OS experiences for many of them
Examples Accessories Namespace
AudioMicrophoneMIDISpeakers
Windows.Media.CaptureWindows.Devices.MidiAudioGraph (new for Windows 10)
FilesRemovable Storage Windows.Storage
Point of Service
Magnetic Stripe Reader
Barcode Scanner
Windows.Devices.PointofService.BarcodeScanner
Windows.Devices.PointofService.MagneticStripeReader
Also productivity oriented accessories like keyboards, mice and network adapters
Demo: USB Flash Drive
• Examples: Fitness devices, Robots, Smart Toys, etc
Vendor specific accessories
Demo: Arduino
Vendor specific accessories on Mobile SKU
API Namespace SDK Sample(s)
HID Windows.Devices.HumanInterfaceDevice
Custom HID Device Access
Serial Windows.Devices.SerialCommunication Custom Serial Device Access
USB Windows.Devices.Usb Custom USB Device Access
• Use OS inbox drivers for HID, USBSerial, or WinUSB.
• Accessories should report hardware or compatible IDs that install these drivers.
• Mobile SKU does NOT support installing vendor drivers from WU
• Audience Challenge: Produce a USB to RS232 cable that reports these compatible IDs
Click icon to add picture
Driver installation for vendor specific accessories
API Compatible IDs
HID Follow Standard. Report Vendor_Specific Top Level Collection
Serial USB\Class_02&SubClass_02&Prot_01USB\Class_02&SubClass_02
WinUSB
WinUSB (Winusb.sys) Installation
1. Start developing a universal app on desktop w/ USB port
2. Make sure the UX of your app lays out properly on phones
3. Once available get a Dual Role capable Mobile device to test your app with accessories
Start developing apps for USB Accessories
• New Phones and Tablets that support USB dual role
• Before you buy a new device
• Should be clear in device marketing if USB dual role is supported or not
• On your current deviceUSB Settings UX informs users if their mobile device supports USB connections or not
Identifying mobile systems that support USB Accessories
Serial Communication using WinRT
Overview
Device Protocol APIs
New Windows Runtime APIs:• Bluetooth• Bluetooth
Smart• HID• USB• Serial• Serial
Innovative
Devices
Ecosystem of Apps
Independent
Developers
Unified and built-in model for device discovery, access and instantiation
New Windows Runtime API that simplifies communicating to/from your device
Works with Universal Apps – write once, deploy everywhere …
Serial in Windows 10
Prerequisites
1. Working knowledge of Serial Communication• Introduction to Serial Communications – MSDN
2 . Good to know …• Win32 Serial Programming interface - MSDN• .NET serial programming interface - MSDN
3. Knowledge of Windows Runtime concepts and paradigms
• Async operations• Data buffers and streams• Fundamentals of working with devices
3
Before you begin…
AppContainer
Architecture diagram
Architecture
Windows Store app
Device Access Broker
Native Serial layer
Serial device
Windows.Devices.SerialCommunication API
Demo: Arduino
Getting Started
Accessing devices from the Windows Runtime
1) Author an app manifestDeclare the correct capability for your device
2) Device discoveryFind out what devices are connected to your
system
3) Find the device you’re looking forInformation about a specific device
4) CommunicateAccess information on your device
5) Clean up resources
Capability
Device discovery
Communication
Cleanup
App capabilities (i.e. app manifest)
1. Declare the device capability• DeviceCapability Name=“serialcommunication”
2. Declare the device • Device Id=“any” - if you want all serial devices
• Device Id=“vidpid:<vid> <pid>” - if you want a specific device
3. Declare usages• Function Type=“name:serialPort”
3.
Capability
Device discovery
Communication
Cleanup
Editing the Package.appxmanifest
AppXmanifest: examplePackage.appxmanifest:
<Capabilities>
<DeviceCapability Name="serialcommunication">
<Device Id="any">
<Function Type="name:serialPort" />
</Device>
</DeviceCapability >
<DeviceCapability Name="serialcommunication">
<Device Id="vidpid:2341 0043">
<Function Type="name:serialPort" />
</Device>
</DeviceCapability>
</Capabilities>
• Finding serial devices on your system
Device discovery
Device discovery
• Traditional mechanism uses COM ports – which has issues• COM1, COM2 isn’t very descriptive about the device• The actual port name will vary across systems• Port assignment relies on driver components that are not
always there.
• Augmented discovery model in WinRT• Supports more descriptive device identifiers or
names that are independent of driver components.• Supports identification of a USB serial device via
VID/PID
Capability
Device discovery
Communication
Cleanup
Device discovery
1. Use the static DeviceSelector methods in SerialDevice to build a serial-specific AQS:
• SerialDevice.GetDeviceSelector();
• SerialDevice.GetDeviceSelector(portName);
• SerialDevice.GetDeviceSelectorFromUsbVidPid(vid, pid);• You can augment the selector to be more specific.
2. Use the Device Enumeration APIs to find the serial device• DeviceInformation.FindAllAsync(aqsFilter) OR• DeviceInformation.CreateWatcher(aqsFilter)
3. Use an enumerated DeviceInformation object to instantiate a SerialDevice• SerialDevice.FromIdAsync(DeviceInformation.Id)
Capability
Device discovery
Communication
Cleanup
Device discoveryusing Windows.Devices.SerialCommunication;
using Windows.Devices.Enumeration
// Create an AQS using on of the static DeviceSelector methods
// USB VID/PID selector
string selector = SerialDevice.GetDeviceSelectorFromUsbVidPid(vid, pid);
// Friendly Name selector
string selector2 = SerialDevice.GetDeviceSelector(“COM1”);
// Pass the selector to FindAllAsync to get a collection of DeviceInformation // objects.
DeviceInformationCollection deviceInformation;
deviceInformation = await DeviceInformation.FindAllAsync(selector);
Device instantiationawait MainPage.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
var openedDevice = await SerialDevice.FromIdAsync(deviceInformation.Id);
if (openedDevice == null)
{
}
else
{
// Begin watching for disconnect. Begin accessing the device.
}
}
Runtime Checks:
1. Capability declarations in the manifest.
2. Allowed by OS Policy.
3. Exclusivity.
4. Consent and Dynamic Access.
If your app is denied access as a result of any of the above, the function returns a NULL SerialDevice object.
What happens when you call FromIdAsync ?
Device instantiation
SerialDevice is NULL ?if (openedDevice == null)
{
var deviceAccessStatus = DeviceAccessInformation.CreateFromId(deviceInformation.Id).CurrentStatus;
if (deviceAccessStatus == DeviceAccessStatus.DeniedByUser)
{
notificationMessage = "Access to the device was blocked by the user : " + deviceInformation.Id;
}
else if (deviceAccessStatus == DeviceAccessStatus.DeniedBySystem)
{
// (Possible failure with Package.appxmanifest declaration, seen at development time only)
}
else
{
// Most likely the device is opened by another app, but cannot be sure
notificationMessage = "Unknown error, possibly opened by another app : " + deviceInformation.Id;
}
• How it all comes together …
Basic communication
Device Configuration• Baud rate, Parity, Stop Bits etc.
Reading• InputStreams and DataReader
Writing• OutputStreams and DataWriter
Basic communication
Declare capability
Device discovery
Communication
Cleanup
Basic Communication
//
// Configure the device
//
serialDevice.BaudRate = 9600;
serialDevice.Parity = SerialParity.None;
serialDevice.StopBits = SerialStopBitCount.One;
serialDevice.Handshake = SerialHandshake.None;
serialDevice.DataBits = 8;
Basic Communication///
/// Write to the device
///
var dataWriter = new DataWriter(serialDevice.OutputStream);
dataWriter.WriteString(“hello");
var bytesWritten = await dataWriter.StoreAsync();
this.NotifyUser("Write completed - " + bytesWritten.ToString() + " bytes written, NotifyType.StatusMessage);
dataWriter.DetachStream();
Basic Communication///
/// Read from the device
///
var dataReader = new DataReader(serialDevice.InputStream);
uint readBufferLength = 8;
UInt32 bytesRead = await dataReader.LoadAsync(readBufferLength);
if (bytesRead > 0)
{
string temp = dataReader.ReadString(bytesRead);
this.NotifyUser("Read completed - " + bytesRead.ToString() + " bytes were read", NotifyType.StatusMessage);
}
dataReader.DetachStream();
1. When done with device communication, the app must close the SerialDevice.
2. Be aware of language-specific nuances for object close and deletion.
Closing the device
Declare capability
Device discovery
Communication
Cleanup
Closing the device
C++:
delete device;
device = nullptr;
C#:
device.Dispose();
device = null;
JS:
this._device.close();
this._device = null;
1. It must close the SerialDevice as it will be invalidated.
2. It should not send any new I/O.
3. On app resume, app must re-open the device.
App must register for suspension
Suspension/Resume
• How it all comes together …
Arduino: Redux
Additional features
• App can do long running operations using background tasks.
• Supports all existing triggers e.g. System Event trigger, Control Channel trigger, DeviceUse trigger
• Tasks aren’t throttled like other system background tasks (no CPU time quota) but will run with reduced priority to keep foreground apps responsive.
Background tasks
Tracing:• Windows.Devices.SerialCommunic
ation
• Device Broker
Details on MSDN page
Debugging
Related SessionsIntroducing the Windows App Model
Moving to Universal: Porting to Windows 10 from Windows 8.1 XAML or Windows Phone Silverlight
Developing Windows 10 Universal Apps in Visual Studio 2015
Universal App Packaging and Deployment for Windows 10 DevicesWindows 10 App Lifecycle: From Activation & Suspension to Background Execution and Multitasking
Optimizing Universal Apps for Continuum
New Retail Peripherals and NFC/HCE Support in Windows 10
Developing universal audio and video apps for Windows
Building Rich, Contextually Aware Applications Using Sensors
Windows for Makers: Raspberry Pi 2, Arduino and More!
Internet of Things: Overview
Building Windows Apps that Discover, Connect, and Interact with Other Devices and Cloud Services Using AllJoyn
Resources: Typical accessories on Mobile SKU
Accessories that just work with OS
• Windows natively supports these accessory types regardless of who makes the hardware.
• Windows provides built-in OS experiences for many of them
Examples Accessories Namespace
AudioMicrophoneMIDISpeakers
Windows.Media.CaptureWindows.Devices.MidiAudioGraph (new for Windows 10)
FilesRemovable Storage Windows.Storage
Point of Service
Magnetic Stripe Reader
Barcode Scanner
Windows.Devices.PointofService.BarcodeScanner
Windows.Devices.PointofService.MagneticStripeReader
Also productivity oriented accessories like keyboards, mice and network adapters
Resources: Vendor specific accessories on Mobile SKU
API Namespace SDK Sample(s)
HID Windows.Devices.HumanInterfaceDevice
Custom HID Device Access
Serial Windows.Devices.SerialCommunication Custom Serial Device Access
USB Windows.Devices.Usb Custom USB device access
USB Accessories1. Start developing a Universal Windows App on desktop w/ USB
port2. Make sure the UX of you’re app lays out properly on phones3. Once available, get a Dual Role capable Mobile device to test
your app with accessories4. Make sure your vendor specific accessories report the correct
hardware IDs to work with Mobile SKU :
Feedback: Anything missing in our APIs?mailto:[email protected]
Call to Action
© 2015 Microsoft Corporation. All rights reserved.