Using serial with FTDI

Hi, I've been experimenting with SerialOTG having had no luck reading data from a sensor device using the standard App Inventor Serial blocks.
If you are open to adding additional functions, I would really appreciate the ability to read a message with user defined start and end characters. In my particular application, a meassage can come in varying numbers of bytes but each message begins with a SOT (Hex 02) character and ends with an EOT (Hex 03) character. Alternatively, could you suggest how I could adapt your SerialOTG sample to collect such messages individually.
Many thanks. You've done a great job with this component and I would support it replacing the current Serial component in App Inventor.
Great job!

1 Like

I understand your problem. It’s a common way to send messages.
A problem with this implementation of Appinventor and SerialOTG is that they are executed in the same thread. The result is that you have to use polling of incoming bytes, otherwise the UI will be locked. Even if I expose the callback function that is implemented in the library, I will just be notified that x number of bytes have been received.

There is one simple solution to your problem if you only use 7-bit ASCII between the STX/ETX characters. I can maybe make the end character in readBytes() selectable (I considered this but preferred to make it simple). Then you can poll for an incoming message with a timer, delete all character up to STX and you have your message.

In general you must use state machine to solve this kind of problem. Something like this:

In timer activated routine:
State 1, poll until STX. Wen found, goto state 2
State 2, collect characters until ETX, goto state 3
State 3, notify consumer that message is received.

Consumer:
take care of message, reset to state 1

If you have arbitrary bytes between STX/ETX, than you need some handling of an escape character.
ESC-STX replaces STX, ESC-ESC replaces ESC and ESC-ETX replaces ETX. This make it more complicated.

2 Likes

How long is your data? If not too long then you can do it with the available blocks, read data byte by byte with the clock.

Could be 2k - 3k per message with some messages only about 100 bytes.

The library buffer is 1024b, so if you have a 3k message then you have to read multiple bytes in a loop or with a short timer period to avoid overrun. Maybe it’s impossible at a high baudrate.
ReadBytes() to list is the fastest method. It will read all bytes received and return a list. List length shows the number of bytes received.

1 Like

Thanks. That is helpful. The transmitting baud rate I need to handle is 115200. I can read the data OK using SerialOTG but my challenge is identifying the SOT and EOT characters to split out individual messages. I'll follow your advice and see how I get on. Thanks for your help.

Interesting to hear about advanced applications. Please let me know how it goes.

If you have problems with blocks, feel free to ask.

Src for Appinventor suggested new Serial component, SerialOTG extension, src and precompiled libraries are now available at:

I've put together some blocks that seem to correctly extract messages enclosed between STX and ETX characters from individual strings that include full or partial messages.
My challenge for another day is to adapt them to read bytes from a list populated by using the ReadBytes() procedure on a serial port as suggested by rkl099. I presume I may need to stop and start the timer clock used to poll the serial port to allow my blocks to extract, decode and process messages? Oh for a "GetMessage" function in a serial component. SerialOTG has certainly helped me make more progress than I thought I would when trying to use the App Inventor standard serial component.

Yes. The serial embedded component has deficiencies in the drivers. It will soon be replaced by an extension created by @rkl099

1 Like

After some requests and learning more about PhysicaloidLibrary, I have added support for more CDC ACM devices:
Feather M0 VID 239A, Teensy 4.0 VID 16C0, OpenCR VID 0483.
The updated extension can be found in a debug version at:

Are there any more requests?

Where can i find the aix file?
Thanks

I think the author clearly wrote it:

Hello rkl099. Thanks for this extension. There is one problem, there are two usb-com devices on my device. In the program I work with one, but sometimes it connects to another. Is it possible to poll device names and connect to the selected one as in terminal programs? Or bind the device to this android program?

1 Like

I have some questions:
What tablet or phone do you use to connect two devices?
Do you want to connect two identical usb serial converters?

Android radio tape recorder "teyes cc2". No, different (pl2303 and ch340).

This is a part of the physicaloid library that I have not changed except for adding some VID:s. The library is written to handle only one serial device. It need to be modified or rewritten in the future. But for now we have to live with the current implementation.

Known problems are:
Simplified detection of VID and PID.
Lack of call-back function.
Limited VID support.
Handling of serial devices with multiple ports.
Handling of multiple devices.

I think it's not necessary to have a library that connects to two devices. Need one that allows you to choose one device from several available to decide for yourself which device you want to connect to. Now it connects to the first on the list.

Ver 1.1 is now available with support for more devices as requested by some users: Adafruit Feather_M0 and CLUE, Micro:bit, Teensy, OpenCR.