webusb

WebUSB and FTDI232


I try to connect to a serial-usb interface (FT232) but can't find anything on how to configure it...

What I've got :

What I need :

I need to connect to this interface using WebUSB, send messages, read messages.

What I've achieved :

Right now, I can connect to the interface and "read" something.

My problem :

I can't find anything on the web on how to configure the USB interface, the serial port configuration must be 57600 baud/s, E, 8, 1 but nothing I could find told how to configure these values...

See the following code, from what I understood, I've got to put some value in the controlTransferOut but I can't find what to put and why..

device = await navigator.usb.requestDevice({ 'filters': [] });
console.log(device);

await device.open();
if (device.configuration === null) {
    await device.selectConfiguration(1);
}
await device.claimInterface(0);
await device.selectAlternateInterface(0, 0);
await device.controlTransferOut({ /* WHAT DO I PUT HERE?? */ });
let result = await device.transferIn(1, 64);
console.log((new TextDecoder).decode(result.data));

What I can read from the "What I've achieved" section is single chars because the serial is not well configured.

I tweaked it though to kindda make it work, I've opened the serial port with another software, configured it, closed the other software, then connect to the USB interface with webusb and read messages. That worked, but way to complicated...

Any help would be REALLY appreciated :)


Solution

  • According to the datasheet your TTL-232RG-VSW5V-WE cable contains a FT232R chip. The datasheet for that is here. I was hoping to discover a description of the protocol that the chip supports but all I found was documentation for their proprietary drivers.

    On the other hand I know that Linux supports these chips so there must be code in the kernel driver for setting baud rate. The function in question is change_speed() in ftdi_sio.c.

    From this it appears that the control transfer you need to send is,

    device.controlTransferOut({ requestType: 'vendor',
                                recipient: 'device',
                                request: 3 /* FTDI_SIO_SET_BAUDRATE_REQUEST */, 
                                value: divisor_value,
                                index: divisor_index });
    

    divisor_value and divisor_index are the lower and upper 16 bits of the chip's clock divisor which is calculated by the ftdi_232bm_baud_to_divisor function based on the baud rate requested.

    Hopefully this is a good start towards reading the code and figuring out what you need.