-
Notifications
You must be signed in to change notification settings - Fork 597
3. Create UsbSerialDevice
There are different ways to create the UsbSerialDevice object that handles all serial port operations.
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);
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);
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);
When a device is opened as a serial port you can assign a name to it.
serialPort.setPortName("COM1");
String portName = serialPort.getPortName();