Syncing UART Communications #11503
darrenweinhold
started this conversation in
General
Replies: 1 comment 1 reply
-
With an UART buffer of 9, bytes may be lost if your program is not responsive enough at some point. I'd use a large UART buffer and a simple FSM (finite state machine) algorithm to parse the protocol. That allows to correct the state easily if a byte gets lost. FSM parsing also forces to think what to do if an unexpected input is received. Google "FSM to parse UART messages" for examples. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This is a general programming question and not really micropython related.
I am trying to read data from a UART. The attached device sends 9 bytes of data every 11ms. I am reading this using uasyncio and a streamreader.
Each set of 9 bytes has a particular order. The first byte is always a "7" and the second is always a "3". I am interested in bytes 4 and 5. I'd like to grab the values of these very easily/quickly. I've tried setting the uart buffer to 9 bytes and setting the streamreader buffer to 9 bytes as well. My thinking was that these would both be circular and then each time I read from the buffer, the same byte position should be there. But I find this isn't necessarily the case. When the same bytes are in the location I expect them, I have to search through the buffer for the correct index of the "7" and "3" and it feels like there is a better solution that I am just not thinking of.
Any thoughts on the best way to achieve this?
Edit: Thinking about this more, it seems like I should read in 9 bytes, use the "find" method to get the index of a "7", check that the result is not -1, then use that index as the "starting index" and verify the following index has a "3". Based on that, I can then grab bytes 4 and 5.
Beta Was this translation helpful? Give feedback.
All reactions