Table of Contents

USB PC Communication


These protocols facilitate communication between the SITCore device and a PC. The USB page has more details and is a prerequisite to this tutorial.

By default, USB client support is used for deploying and debugging applications. However, deploying and debugging can be switched to a serial mode using MOD pin, freeing the USB Client port to serve other purposes.

The USB port can also be freed by completely disabling the USB debug interface, which is a feature of IP protection.

USB CDC

The USB Communications Device Class (CDC) is natively supported by Windows and Linux. It is a way for a PC to use a USB port as a virtual serial port. Once loaded, the PC will use this port like any other serial port (COM port). Windows 10 works without the need for any drivers, but earlier operating systems may need a driver. While it works with most operating systems, CDC is typically limited to 64 KBytes/second.

Tip

Needed NuGets: GHIElectronics.TinyCLR.Devices.UsbClient

var usbclientController = UsbClientController.GetDefault();
    
var usbClientSetting = new UsbClientSetting(){
        Mode = UsbClientMode.Cdc,
        ManufactureName = "Manufacture_Name",
        ProductName = "Product_Name",
        SerialNumber = "12345678",            
};

var cdc = new Cdc(usbclientController, usbClientSetting);

cdc.DeviceStateChanged += (a,b) => Debug.WriteLine("Connection changed."); 
cdc.DataReceived += (a,count) => Debug.WriteLine("Data received:" + count);

cdc.Enable();

while (cdc.DeviceState != DeviceState.Configured);
        Debug.WriteLine("UsbClient Connected");

// The example will read data from port to dataR array
// Copy dataR to dataW array, plus 1 for each element
// Write dataW array back to port

while (true){
    var len = cdc.Stream.BytesToRead;

    if (len > 0){
        var dataR = new byte[len];
        var dataW = new byte[len];
        int read = cdc.Stream.Read(dataR);

        for (var i = 0; i < read; i++){
            dataW[i] = (byte)(dataR[i] + 1);
        }
        cdc.Stream.Write(dataW);
    }
    Thread.Sleep(100);
}


WinUSB

The WinUSB drivers are unique to Windows and take advantage of the power and speed of USB to provide faster communication than CDC. The speed is limited by the data processing on the IoT device. Windows 10 loads the drivers automatically, Windows 7 requires drivers.

Note

Unlike CDC mode, a disadvantage of WinUSB is that it requires a special code on the PC side to read and write to the device.

var usbclientController = UsbClientController.GetDefault();
    
var usbClientSetting = new UsbClientSetting(){
    Mode = UsbClientMode.WinUsb,
    ManufactureName = "Manufacture_Name",
    ProductName = "Product_Name",
    SerialNumber = "12345678",
    Guid = "{your guid}",
};

var winUsb = new WinUsb(usbclientController, usbClientSetting);

winUsb.DeviceStateChanged += (a,b) => Debug.WriteLine("Connection changed."); 
winUsb.DataReceived += (a,count) => Debug.WriteLine("Data received:" + count);
                 
winUsb.Enable();

while (winUsb.DeviceState != DeviceState.Configured) ;
        Debug.WriteLine("UsbClient Connected");

// The example will read data from port to dataR array
// Copy dataR to dataW array, plus 1 for each element
// Write dataW array back to port
    
while (true){
    var len = winUsb.Stream.BytesToRead;

    if (len > 0){
        var dataR = new byte[len];
        var dataW = new byte[len];
        int read = winUsb.Stream.Read(dataR);

        for (var i = 0; i < read; i++){
            dataW[i] = (byte)(dataR[i] + 1);
        }
        winUsb.Stream.Write(dataW);
    }
    Thread.Sleep(100);
}

WebUSB

Some modern browsers, like Chrome and Edge, include a way to give access to the USB from the web content. TinyCLR CDC and WinUSB already include the necessary descriptors to enable WebUSB. This repo includes a full WebUSBApp and SerialWebUSBApp examples to load onto the device, that is any TinyCLR device. Once loaded, visit https://ghi-electronics.github.io/TinyCLR-WebUSB/ to connect to the device.

Note

For CDC (Serial Web USB), needs to load SerialWebUSBApp into the device and visit https://ghi-electronics.github.io/TinyCLR-WebUSB/serialwebusb.html

WebUSB Connect

User will see TinyCLR WebUsb device as an option to Connect

WebUSB Select

On successful connection the input drop downs are enabled.

WebUSB Select

After selecting Update State the device sends back a message indicating the outcome.

WebUSB Success

In this case the PB0 is set HIGH and the LED turns on.

WebUSB LED

The secret sauce is in using the correct endpoints, which are fixed in both CDC and WinUSB drivers. This line can be used to initiate the class let webusb = new WebUSB(2, 1); which is found inside webusb.js file