USB-Host Module

Overview of USB-Host functions

Group

Available functions

Configuration

CDC/VCP specific:

Data Transfer

Introduction

The USB-Host Module can be used to send and receive data from USB-Devices. The module supports the following USB classes:

  • CDC (e.g. USB to Serial Adapters)

  • HID (e.g. Barcode-Scanner, Keyboard)

  • Vendor Specific USB to Serial Adapters:
    • CP210x

    • FTDI

Device Info Object

class tf_ClassUSB.USBDeviceInfo
vid: str
pid: str
deviceClass: str
deviceSubClass: str
deviceName: str
productName: str
manufacturer: str
print()

USB-Host Methods

class tf_ClassUSB.ClassUSB(_address)
resetToDefaults() → bool

Resets all settings back to default values. This function is useful for resetting all settings to their defined default state at the start of a test.

Returns:

True if reset was successfully, False if not.

Return type:

bool

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
usb = ext.getUSB(0)
usb.resetToDefaults()
getDeviceInfo() → USBDeviceInfo

Retrieves the USB device information.

Returns:

USB device information

Return type:

USBDeviceInfo

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
usb = ext.getUSB(0)
info = usb.getDeviceInfo()
report.checkString("Device VID", info.vid, "10C4")
report.checkString("Device PID", info.pid, "EA60")
report.checkString("Device Name", info.deviceName, "CP210X")
discardInbuffer() → bool

This will discard the inbuffer. It is recommended to call this method before you start listening for new data

Returns:

bool: feedback whether the data was accepted or not. (true = success, false = failed)

getInbufferSize() → int

Returns the amount of data bytes in the inbuffer.

Returns:

number of bytes

Return type:

int

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
usb = ext.getUSB(0)
usb.setBaudrate(115200)
dataCnt = usb.getInbufferSize()

print(dataCnt)
getInbufferData(startIndex: int = 0, length: int = 0) → bytearray

Returns the received bytes. The data remains in the inbuffer and can be read as often as you want. To clear the inbuffer use the discardInbuffer() function

Parameters:
  • startIndex – The startindex in the buffer from which the data should be read (default is 0, which means from the beginning)

  • length – Number of bytes which should be read from the inbuffer. 0 means: all data until end of the buffer. (default is 0, which means all data will be returned)

Returns:

data of the inbuffer

Return type:

bytearray

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
usb = ext.getUSB(0)
allData = usb.getInbufferData()
print(allData)
async waitForResponse(timeout_ms: int) → bool

Returns the received Frame. The data remains in the inbuffer and can be read as often as you want. To clear the inbuffer use the discardInbuffer() function

Parameters:

index – The index of the frame (0-255)

Returns:

data of the frame

Return type:

bytearray

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
usb = ext.getUSB(0)
usb.setBaudrate(115200)

await usb.waitForResponse(2000)
rxData = usb.getInbufferData()
print(rxData)
sendData(data: bytearray) → bool

Sends out the given data (only for CDC and VCP Devices).

Args:

data (bytearray): data to be sent

Returns:

bool: feedback whether the data was accepted or not. (true = success, false = failed)

setBaudrate(baudrate: int) → bool

With this method you can setup the baudrate for a virtual COM-Port (VCP) Device.

Parameters:

baudrate – Choose any baudrate between 300 and 1000000.

Returns:

True if baudrate was set successfully, False if not.

Return type:

bool

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
usb = ext.getUSB(0)
usb.setBaudrate(115200)
setDataSize(datasize) → bool
setParity(parity: int) → bool

Sets the parity for the virtual COM-Port (VCP) Device. Default: NONE

Parameters:

parity – One of the following constants: PARITY_NONE, PARITY_ODD, PARITY_EVEN

Returns:

True if parity was set successfully, False if not.

Return type:

bool

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
usb = ext.getUSB(0)
usb.setParity(usb.PARITY_EVEN)
setStopbits(stopbits: int) → bool

Sets the number of stopbits for the virtual COM-Port (VCP) Device. Default: 1 Stop Bit

Parameters:

stopbits – One of the following constants: STOPBITS_0_5, STOPBITS_1, STOPBITS_1_5, STOPBITS_2

Returns:

True if stopbits was set successfully, False if not.

Return type:

bool

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
usb = ext.getUSB(0)
usb.setStopbits(usb.STOPBITS_1_5)
setDTR(state: bool) → bool

Sets the DTR (Data Terminal Ready) state for the virtual COM-Port (VCP or CDC class) Device.

Parameters:

state (bool) – True to set DTR, False to clear DTR

Returns:

True if DTR was set successfully, False if not.

Return type:

bool

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
usb = ext.getUSB(0)
usb.setDTR(True)
setRTS(state: bool) → bool

Sets the RTS (Request to Send) state for the virtual COM-Port (VCP or CDC class) Device.

Parameters:

state (bool) – True to set RTS, False to clear RTS

Returns:

True if RTS was set successfully, False if not.

Return type:

bool

Example usage:

from testframework import *

#ext.init()     #only neccessary if you run this code in debug console, outside of a testrun
usb = ext.getUSB(0)
usb.setRTS(True)