51

Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

Embed Size (px)

Citation preview

Page 1: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present
Page 2: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

Session 3-81

Building apps for USB Accessories

George Roussos Arvind AiyarSenior Program Manager Senior Software Engineer

Page 3: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

Develop apps that can connect with hardware from the past and present

Page 4: Building apps for USB Accessories 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.

Page 5: Building apps for USB Accessories Develop apps that can 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

Page 6: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

• 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

Page 7: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

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

Page 8: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

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

Page 9: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

Demo: USB Flash Drive

Page 10: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

• Examples: Fitness devices, Robots, Smart Toys, etc

Vendor specific accessories

Page 11: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

Demo: Arduino

Page 13: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

• 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

Page 14: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

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

Page 15: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

• 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

Page 16: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

Serial Communication using WinRT

Page 17: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

Overview

Page 18: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

Device Protocol APIs

New Windows Runtime APIs:• Bluetooth• Bluetooth

Smart• HID• USB• Serial• Serial

Innovative

Devices

Ecosystem of Apps

Independent

Developers

Page 19: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

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

Page 20: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

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…

Page 21: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

AppContainer

Architecture diagram

Architecture

Windows Store app

Device Access Broker

Native Serial layer

Serial device

Windows.Devices.SerialCommunication API

Page 22: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

Demo: Arduino

Page 23: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

Getting Started

Page 24: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

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

Page 25: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

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

Page 26: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

Editing the Package.appxmanifest

Page 27: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

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>

Page 28: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

• Finding serial devices on your system

Device discovery

Page 29: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

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

Page 30: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

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

Page 31: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

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);

Page 32: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

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.

}

}

Page 33: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

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

Page 34: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

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;

}

Page 35: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

• How it all comes together …

Basic communication

Page 36: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

Device Configuration• Baud rate, Parity, Stop Bits etc.

Reading• InputStreams and DataReader

Writing• OutputStreams and DataWriter

Basic communication

Declare capability

Device discovery

Communication

Cleanup

Page 37: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

Basic Communication

//

// Configure the device

//

serialDevice.BaudRate = 9600;

serialDevice.Parity = SerialParity.None;

serialDevice.StopBits = SerialStopBitCount.One;

serialDevice.Handshake = SerialHandshake.None;

serialDevice.DataBits = 8;

Page 38: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

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();

Page 39: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

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();

Page 40: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

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

Page 41: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

Closing the device

C++:

delete device;

device = nullptr;

C#:

device.Dispose();

device = null;

JS:

this._device.close();

this._device = null;

Page 42: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

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

Page 43: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

• How it all comes together …

Arduino: Redux

Page 44: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

Additional features

Page 45: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

• 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

Page 46: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

Tracing:• Windows.Devices.SerialCommunic

ation

• Device Broker

Details on MSDN page

Debugging

Page 47: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

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

Page 48: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

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

Page 50: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

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

Page 51: Building apps for USB Accessories Develop apps that can connect with hardware from the past and present

© 2015 Microsoft Corporation. All rights reserved.