Skip to content

3. Create UsbSerialDevice

Felipe Herranz edited this page Nov 18, 2018 · 1 revision

There are different ways to create the UsbSerialDevice object that handles all serial port operations.

Using createUsbSerialDevice method

CreateUsbSerialDevice method will try open a serial port with the provided UsbDevice and UsbDeviceConnection. It performs a lookup through different tables of supported VID/PID to correctly select the correct driver. If it couldn't be matched with any pair of VID/PID will return null.

UsbDevice device;
UsbDeviceConnection usbConnection;
...
UsbSerialDevice serial = UsbSerialDevice.createUsbSerialDevice(device, usbConnection); 

Some Usb devices have different interfaces. Previous method opens the first indexed interface but it is also possible to open any indexed interface.

UsbDevice device;
UsbDeviceConnection usbConnection;
...
UsbSerialDevice serial = UsbSerialDevice.createUsbSerialDevice(device, usbConnection); 

Using createUsbSerialDevice method specifying the driver

Sometimes your device have custom VID/PID pairs (or simply they are not in UsbSerial tables) but the device is perfectly supported. In that case there is a variation of createUsbSerialDevice that allows driver type specification. For trying to open a serial port using the FTDI driver (first indexed interface):

UsbDevice device;
UsbDeviceConnection usbConnection;
...
UsbSerialDevice serial = UsbSerialDevice.createUsbSerialDevice(UsbSerialDevice.FTDI, device, connection,0);

Checking if a device is supported without opening it as serial port

It could be also useful to check if a Usb device can be opened without actually open it.

UsbDevice device;
UsbDeviceConnection usbConnection;
...
boolean supported = UsbSerialDevice.isSupported(device);

Set port name

When a device is opened as a serial port you can assign a name to it.

serialPort.setPortName("COM1");
String portName = serialPort.getPortName();