Bluetooth Client: Delimiter byte

1,205 views
Skip to first unread message

Ben C

unread,
Apr 21, 2018, 9:36:02 AM4/21/18
to MIT App Inventor Forum

Hi all,

Been finding answers here for a while so thought i'd ask. Looking for help on delimiter byte as i am finding it very hard to get every incoming string. I have added a packet sequence id to incoming data so I know i am missing about 1 in every 25 packets.

Hardware: multiple tags > UHF radio router > crossover cable > Ethernet shield > AtMega328P > HC05 (BT) > App

As I am writing both sides, I can add or change start and end bytes. I have also slowed down the code to allow for the slow serial connection between processor and BT module. Currently I am stripping off the begin and end TCP session packets as well as an XOR checksum and any \r's/\n's so the inbound packets should look like this:

$G,5,889EC082819C,R,53649,-97,12,69B2,92520A15,4,B,0,3,0,2,84,0100504301971001545375*

Using a serial monitor in place of the BT module, the data to that point is checked and good. When multiple packets arrive at processor from radio router buffer, they are still sent out one at a time.

Several comma separated fields have a varying number of characters, in fact the number of fields change in different modes as well. So I can't read a set number of bytes and I can't I can't check what characters are inside until I've read.

Assuming i stick with '*' as the packet delimiter, the byte to be entered in the BTclient designer (also tried setting as part of Screen 1 initialise) should be 42, or maybe 0042 (x2A). I have tried this and many others but continue to miss it occasionally. If the buffer can do over a hundred bytes and the clock cycle on the app is so much faster than the slow serial feed to the BT module, it should work.

I have had some success with joining the previous read to the current read if the current read doesn't contain a '$'. Also tried splitting the inbound packet at '*' into a list then checking the second index for a complete or partial string. I don't like this though as it is a fudge not a fix. The delimiter functions sounds ideal to make sue I read each packet correctly but I guess I have missed something.

Any advice greatly appreciated, thanks,
Ben.

Lasty and not as important, the character encoding is not a dropdown and the description here (http://ai2.appinventor.mit.edu/reference/components/connectivity.html) is brief, I am assuming the two options are HEX or UTF-8 and as I have non-hex characters UTF-8 is the best choice. Is that right?
BT_capture.JPG

Abraham Getzler

unread,
Apr 21, 2018, 6:25:57 PM4/21/18
to MIT App Inventor Forum
(stock answer:)
Use println() to separate messages when you transmit.
Set Delimiter = 10 in BlueTooth Designer.
Check for Bytes Available > 0 in blocks,
but then request -1 bytes to get only full  messages.

Also, the Delimiter attribute is in decimal, 10 = LF

ABG

Ben C

unread,
Apr 22, 2018, 9:27:39 AM4/22/18
to MIT App Inventor Forum
Thank you. Big help.

"but then request -1 bytes to get only full messages", rather than requesting bytes available to receive. - Fixed

Packet loss seems to be zero and the horrible cleanup functions are now gone.
Reply all
Reply to author
Forward
0 new messages