Communicating with usb on windows

USB Request Blocks (URBs)

This section describes a USB Request Block (URB) and provides information about how a USB client driver can use Windows Driver Model (WDM) routines to allocate, build, and submit URBs to the USB driver stack.

A Universal Serial Bus (USB) client driver cannot communicate with its device directly. Instead, the client driver creates requests and submits them to the USB driver stack for processing. Within each request, the client driver provides a variable-length data structure called a USB Request Block (URB). The URB structure describes the details of the request and also contains information about the status of the completed request. The client driver performs all device-specific operations, including data transfers, through URBs. The client driver must initialize the URB with information about the request before submitting it to the USB driver stack. For certain types of requests, Microsoft provides helper routines and macros that allocate an URB structure and fill the necessary members of the URB structure with details provided by the client driver.

Each URB begins with a standard fixed-sized header (_URB_HEADER) whose purpose is to identify the type of operation requested. The Length member of _URB_HEADER specifies the size, in bytes, of the URB. The Function member, which must be one of a series of system-defined URB_FUNCTION_XXX constants, determines the type of operation that is requested. In the case of data transfers, for instance, this member indicates the type of transfer. Function codes URB_FUNCTION_CONTROL_TRANSFER, URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER, and URB_FUNCTION_ISOCH_TRANSFER indicate control, bulk/interrupt, and isochronous transfers respectively. The USB driver stack uses the Status member to return a USB-specific status code.

To submit an URB, the client driver uses the IOCTL_INTERNAL_USB_SUBMIT_URB request, which is delivered to the device by means of an I/O request packet (IRP) of type IRP_MJ_INTERNAL_DEVICE_CONTROL.

After the USB driver stack is done processing the URB, the driver stack uses the Status member of the URB structure to return a USB-specific status code.

NoteВ В KMDF and UMDF driver developers should use the respective framework interfaces for communicating with a USB device. For more information, see Working with USB Devices for KMDF drivers and Working with USB Interfaces in UMDF. These topics discuss the underlying WDM driver interfaces used for USB device communication.

In this section

This topic describes how a USB client driver can use Windows Driver Model (WDM) driver routines to allocate and format an URB before sending the request to the Microsoft-provided USB driver stack.

This topic describes the steps that are required to submit an initialized URB to the USB driver stack to process a particular request.

This topic describes best practices for a client driver for allocating, building, and sending an URB to the USB driver stack included with WindowsВ 8.

Overview of developing Windows applications for USB devices

Summary

  • Guidelines for choosing the right programming model
  • UWP app and desktop app developer experience
Читайте также:  Желтые папки windows 10

Important APIs

This topic provides guidelines for deciding whether you should write a UWP app or a Windows desktop app to communicate with a USB device.

Windows provides API sets that you can use to write apps that talk to a custom USB devices. The API performs common USB-related tasks such as, finding the device, data transfers.

«Custom device» in this context means, a device for which Microsoft does not provide an in-box class driver. Instead, you can install WinUSB (Winusb.sys) as the device driver.

Choosing a programming model

If you install Winusb.sys, here are the programming model options:

Windows 8.1 provides a new namespace: Windows.Devices.Usb. The namespace cannot be used in earlier version of Windows. Other Microsoft Store resources are here: UWP app.

Before Windows 8.1, apps that were communicating through Winusb.sys, were desktop apps written by using WinUSB Functions. In Windows 8.1, the API set has been extended. Other Windows desktop app resources are here: Windows desktop app.

The strategy for choosing the best programming model depends on various factors.

Will your app communicate with an internal USB device?

The APIs are primarily designed for accessing peripheral devices. The API can also access PC internal USB devices. However access to PC internal USB devices from a UWP app is limited to a privileged app that is explicitly declared in device metadata by the OEM for that PC.

Will your app communicate with USB isochronous endpoints?

If your app transmits data to or from isochronous endpoints of the device, you must write a Windows desktop app. In Windows 8.1, new WinUSB Functions have been added to the API set that allow a desktop app to send data to and receive data from isochronous endpoints.

Is your app a «control panel» type of app?

UWP apps are per-user apps and do not have the ability to make changes outside the scope of each app. For these types of apps, you must write a Windows desktop app.

Is the USB device class supported classes by UWP apps?

Write a UWP app if your device belongs to one these device classes.

  • name:cdcControl, classId:02 * *
  • name:physical, classId:05 * *
  • name:personalHealthcare, classId:0f 00 00
  • name:activeSync, classId:ef 01 01
  • name:palmSync, classId:ef 01 02
  • name:deviceFirmwareUpdate, classId:fe 01 01
  • name:irda, classId:fe 02 00
  • name:measurement, classId:fe 03 *
  • name:vendorSpecific, classId:ff * *

Note If your device belongs to DeviceFirmwareUpdate class, your app must be a privileged app.

If your device does not belong to one the preceding device classes, write a Windows desktop app.

Driver requirement

Driver requirement UWP app Windows desktop app
Function driver Microsoft-provided Winusb.sys (kernel-mode driver). Microsoft-provided Winusb.sys (kernel-mode driver).
Filter driver If filter drivers are present, access is limited to privileged apps. The app is declared as privileged apps in device metadata by the OEM. Filter driver can be present in the kernel mode device stack as long as it doesn’t block access to Winusb.sys.

Code samples

Sample UWP app Windows desktop app
Get started with these samples
  • Custom USB device access sample
  • USB CDC Control sample
  • Firmware Update USB Device sample
  • Start with the WinUsb Application template included with Microsoft Visual Studio (Ultimate or Professional)
  • Extend the template by using code examples shown in How to Access a USB Device by Using WinUSB Functions.

Development tools

Microsoft Visual Studio 2013

Microsoft Windows Software Development Kit (SDK) for Windows 8.1

Use WinUSB Application template included with Visual Studio (Ultimate or Professional) and Windows Driver Kit (WDK) 8

How to communicate with a USB device under Windows and Java?

I’d like to communicate with a USB device under Windows and Java but I can’t find a good library to do so. I don’t want the user to have to install any extra hardware or device drivers to make this work. That is, I want to be able to interact with USB just like other Windows applications do.

I am familiar with jUSB and JSR 80 but both seem to be dead projects (at least for Windows).

4 Answers 4

I did quite a bit of research on this some time ago, and the unfortunate fact was that all the useful free USB+Windows+Java projects were dead. There is commercial and expensive (price $39.99 is not per developer, but per copy of your software sold!) JCommUSB library which probably works, although I have no experience of it; we had to build our own custom C wrappers to the USB drivers and communicate with them through JNI.

libusb-win32 requires you to install their generic driver, which then makes a USB device available to you. I’m not sure that it’s possible to do driver-less access of an USB device unless the device belongs to one of several standard classes (storage and HID, in particular).

There is a Java wrapper for libusb-win32 which might work for you. I haven’t used it myself, though.

The fastest and easiest way is to hack some native code 🙂 I wrote a small wrapper for HID devices that enabled my Java applications to read data from CalComp digitizers, so it’s definitely doable and not too hard. The bad thing is that my work is still proprietary code owned by my former employer, so for legal reasons I can’t release that as open-source — yet.

The good thing is that you can get a flying start with the HID example code from the Microsoft DDK 🙂

An example is conducted for a Freescale microcontroller

Not the answer you’re looking for? Browse other questions tagged java windows usb or ask your own question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Is there a way to communicate with USB devices from browser?

Is there a way to communicate with USB devices within regular browser (FF, Safari, IE8, etc)? I assume it could be Javascript -> Java bridge or something else.

P.S. Does it possible with minimal dependencies? If not, the whole point of browser-based software is useless (in my case)

6 Answers 6

If your CC and barcode scanners work as HID devices (basically, keyboard emulation), yes you can, and I have seen it done. If they’re more complicated than that, it depends, the system is going to require drivers for the device and it is then OS dependant. But, for example, there are smartcard readers that will hook the card into the system’s certificate framework for authentication.

You’re not going to be able to use a chipped credit card with that sort of reader, however, since the protocol for those requires bidirectional communication with the card. I think there may be a way to talk to a proper point of sale CC terminal over USB, but that will most likely require drivers.

Google chrome offers a javascript USB device interface but this is chrome specific.

Not sure what you would do on other browsers.

There are two choices

  1. Use a browser plugin runtime like (Silverlight or Flash) that talks to Windows HID.

or 2. Use a local HTTP server and have JQuery post backs to the local HTTP Server which inturns talks to HID.

Electron can also be put to a good use here, since its build on node, it can access HID devices. There’s also a nice node plugin to access HID devices.

With Silverlight 4 (beta) there are ways do things like that, but only for trusted applications and maybe not the way you hope.

It provides a API for COM Automation. That means you need a drive that exposes some COM interop for you or you have to write something to control the USB-Port/Device yourself.

this article seems to be a good sample:

Why would the point of browser based sofware be useless?

Browser -> USB seems like a huge security flaw.

I don’t let the browser talk to my hard drive, why should it talk to my flash drive?

I don’t let my browser talk to my built in webcam; why should it take to my usb webcam?

Working with USB devices in .NET

Using .Net (C#), how can you work with USB devices?

How can you detect USB events (connections/disconnections) and how do you communicate with devices (read/write).

Is there a native .Net solution to do this?

11 Answers 11

There is no native (e.g., System libraries) solution for this. That’s the reason why SharpUSBLib exists as mentioned by moobaa.

If you wish to roll your own handler for USB devices, you can check out the SerialPort class of System.IO.Ports.

I’ve tried using SharpUSBLib and it screwed up my computer (needed a system restore). Happened to a coworker on the same project too.

I’ve found an alternative in LibUSBDotNet: http://sourceforge.net/projects/libusbdotnet Havn’t used it much yet but seems good and recently updated (unlike Sharp).

EDIT: As of mid-February 2017, LibUSBDotNet was updated about 2 weeks ago. Meanwhile SharpUSBLib has not been updated since 2004.

I used the following code to detect when USB devices were plugged and unplugged from my computer:

You have to make sure you call the Dispose() method when closing your application. Otherwise, you will receive a COM object error at runtime when closing.

I’d recommend LibUSBDotNet, the library I have been using for 2 years. If you have to work with an USB device (send requests, process responses), this library was the best solution I could find.

Pros:

  • Has all methods you need to work in synch or asynch mode.
  • Source code provided
  • Enough samples to start using it straight away.

Cons:

  • Poor documentation (it’s common problem for open source projects). Basically, you can find just common description of methods in the CHM help file and that’s it. But I still find provided samples and source code is enough for coding. Just sometimes I see a strange behaviour and want to know why it was implemented in this way and can’t get even a hint.
  • Seems unsupported any more. Last version was issued in Oct 2010. And it’s hard to get answers sometimes.
Читайте также:  Windows or linux web hosting
Оцените статью
Development tools UWP app Windows desktop app
Developer environment