Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4151

Automation, sensing and robotics • Re: Rs232 command change between 2 gears project

$
0
0
raspberry pico, micropython

There are some flaws in the code.
- no telegram frame definition visible
UART data are received as byte stream. In order to extract telegrams out of a byte stream there is the need for some frame definitions.
A simple definition could be: a telegram ends with a newline character. Or it starts with STX and ends with ETX. More complex definitions can be like NMEA as $SDDBT,22.3,f,6.8,M,3.7,F*3F<cr><lf>
Telegram frames can be different for both of your devices.

You use readline to read telegrams. readline needs a 'newline' character at the end of the line.
You should remove the telegram frame from the telegram before you search it in the dictionary. If newline is used, remove it.
You should add telegram frame when sending data.
Keep in mind that 'readline' could give you an incomplete first telegram .

- Interrupt usage
You set up an interrupt for each incoming char. Then, inside the interrupt service routine, you use readline which waits indefinite time for a newline to arrive. Not a good idea, as usually the idea is to complete interrupt service routines as fast as possible.
If you want to go with interrupts, then read single chars, assemble to byte buffer and when a newline is found then process the data. In micropython, I would place the byte buffer in a queue and let the main loop do the heavy lifting.

I would propose to change the code structure and not to use interrupts.
- either use select library, see http://docs.micropython.org/en/latest/l ... elect.html and also see https://forum.micropython.org/viewtopic.php?t=1741 Read one byte after the other, and when a telegram is complete then process it.
- or go with async pattern. Lot of things to learn, but a modern approach to solve problems.

Statistics: Posted by ghp — Fri May 31, 2024 7:27 am



Viewing all articles
Browse latest Browse all 4151

Trending Articles