another UsbSerial - baudrate question

Dirk A

Member
Licensed User
Longtime User
hello,
I'm using usbserial with an FTDI device , I can send and receive data , but only at baudrate of 9600 .
Now I noticed that the baudrate parameter in usb.Open is ignored if the connected device is an accessory .
B4X:
Dim usb As UsbSerial
Dim dev As Int
dev = usb.Open(19200)

in my code dev = 2 ( usb_accessory)

How come you can't change the baudrate of an accessory ? And why is my ftdi device recognized as an accessory and not as an usb_device, can I change this ?
 

agraham

Expert
Licensed User
Longtime User
You can't change the baud rate of an accessory because accessories don't have baud rates as they do not mimic a serial connection, they implement a communication protocol over USB with the accessory as the master USB device and the Android device as the slave.

I've no idea why your FTDI device is being recognised as an accessory and am astonished that it works if so. I assume that you have an OTG cable to switch the device USB port into host mode which should preclude the FTDI being recognised as an accessory.
 
Upvote 0

Dirk A

Member
Licensed User
Longtime User
I don't have an OTG cable right now but I have a standard demo app from the ftdi website that allows you to send and receive data using the ftdi device. Using that app I'm able to send correctly at whatever baudrate I want using the same device...
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
What Android device and FTDI chip are you using? Does the Android device have a host mode USB port that doesn't need an OTG cable?

Unfortunately I can see no changes I could make in the library USB device detection logic - which is very simple. It looks like the Android UsbManager.getDeviceList() method is not detecting the FTDi chip as a slave mode device on your device.

I am totally baffled as to how it is able to be recognised as an Accessory. Android UsbManager.getAccessoryList must be reporting it as such for the library to open it and return USB_ACCESSORY.
 
Upvote 0

Dirk A

Member
Licensed User
Longtime User
the chip is Ft311D this is a usb host , and I'm using Android 4.1.2

Do I need the OTG cable ? The manufacturer of the device said that my android device should be a usb slave OR use an OTG cable ...
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Solved! It's actually working properly. That chip IS an Android Accessory, NOT a slave mode USB to UART adaptor, and in host mode will connect to a standard Android device slave USB port which is why you don't need an OTG cable to get it to work. It is correctly being reported to the library as an Accessory.

The UsbSerial library only recognises FTDI FT232R series chips, and any others with the same Product and User IDs, as UART devices when they are connected to a host USB port on an Android device.
 
Upvote 0

Dirk A

Member
Licensed User
Longtime User
So this means I can't use b4a with my device properly using the usbserial... crap

I do have the java source code of the example program where you can open the FT311D device and , change configuration settings , read and write data.

I don't know if this is possible , but would it be any use to you if you had the java code so that maybe you could make the usbserial library compatible with the FT311D too ?
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Unlike the individual slave mode serial adaptor chips an Android Accessory is a higher level abstraction of a device. It is presented as an input stream and an output stream and it is up to the application program to detect and control whatever is at the far end of the Accessory.

I have seen FT311UARTInterface.java and it appears there is nothing in there that cannot be implemented by the standard USB library, or even through the streams of a UsbSerial Accessory as long as you don't need to detect things like Manufacturer, Model, Version etc.

The UART config data appears to be an 8 bye packet sent through the normal output stream. It is, however, a mystery to me how the chip identifies this as config data and not data data. It is also of interest that it seems to avoid sending a data packet of exactly 64 bytes, instead sending it as 63 bytes and then one byte. Very odd, particularly as there is further processing after the stream so there is no guarantee that the data will be sent in one single USB data transfer anyway.
 
Upvote 0

Dirk A

Member
Licensed User
Longtime User
I think I've read somewhere that the configuration byte should be the first one you send , and the configuration can only be set once...

Ok I think I will be able to get to a solution then using the usb library, or UsbSerial Accessory streams.

Thanks for the help !
 
Upvote 0

DipDip

New Member
Licensed User
Longtime User
You can't change the baud rate of an accessory because accessories don't have baud rates as they do not mimic a serial connection
In complement:
usb-accessory-baudrate_imagesia-com_a7y4_large.png
 
Upvote 0

tman

Member
Licensed User
Longtime User
Unlike the individual slave mode serial adaptor chips an Android Accessory is a higher level abstraction of a device. It is presented as an input stream and an output stream and it is up to the application program to detect and control whatever is at the far end of the Accessory.

I have seen FT311UARTInterface.java and it appears there is nothing in there that cannot be implemented by the standard USB library, or even through the streams of a UsbSerial Accessory as long as you don't need to detect things like Manufacturer, Model, Version etc.

The UART config data appears to be an 8 bye packet sent through the normal output stream. It is, however, a mystery to me how the chip identifies this as config data and not data data. It is also of interest that it seems to avoid sending a data packet of exactly 64 bytes, instead sending it as 63 bytes and then one byte. Very odd, particularly as there is further processing after the stream so there is no guarantee that the data will be sent in one single USB data transfer anyway.
 
Upvote 0

tman

Member
Licensed User
Longtime User
I purchased a usb to serial adapter from startech and it has the ft312d chip. This adapter works great using the terminal app from FTDI. I tried to create my own app in B4A using the USBSerial libray and ran into a problem. Of course I have no way to configure the device. However, the factory default setting of 9600 baud, 8 bits, no parity, one stop bit is perfect for my application. I can send a 5 byte message perfectly but my app requires that I send only one byte. When I try it the stop bit is not received. It would be great if we could have a B4A library to support this accessory.
In your posting you mentioned that you looked at the java code: (FT311UARTInterface.java) Would you be willing to revisit this topic? Can this be made into a B4A library. I would love to create a B4A app using this accessory.
 
Upvote 0

tman

Member
Licensed User
Longtime User
Have you tried felUsbSerial library?
I did try the felUsbSerial library it I couldn't get it to work at all. On FelHR85's web page he said he was interested in getting a FT312D apdapter to try out. I got the impression he was going to add support for this device in his program. The manufacturer (FTDI) recommends using loading FT311UARTInterface.java in Eclipse. I cannot code in java using Eclipse, that is why I love B4A. I thought about using a Usb to serial device instead of an accessory but my phone and many others do not support USB host mode - the phone can't supply the 5 volt needed for the usb port. However most phones support USB accessory mode! The accessory is the "host" and has its own 5 volt supply. Also no special drivers are needed since everything is contained in the accessory. It makes sense to create serial port apps using "Android Open Accessory" devices since it supported by more phones. Can the FT311UARTInterface.java be made into a library for B4A users like me? I think special commands are sent to the accessory to configure it and it should be configured first each time the app is run.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can rewrite this Java code in B4A. The USB library supports USB accessory devices.

The relevant code that sets the baud rate is in the SetConfig method. It should be simple to rewrite it.

Something like:
B4X:
Dim buffer(8) As Byte
buffer(0) = baud
buffer(1) = Bit.ShiftRight(baud, 8)
buffer(2) = Bit.ShiftRight(baud, 16)
buffer(3) = Bit.ShiftRight(baud, 24)
buffer(4) = dataBits
buffer(5) = stopbits
buffer(6) = parity
buffer(7) = flowControl
OutputStream.WriteBytes(buffer, 0, buffer.Length)
 
Upvote 0
Top