help with E32 program (simple)

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
NateOdem
Posts: 13
Joined: Sun May 08, 2022 5:18 pm

Re: help with E32 program (simple)

Post by NateOdem » Wed May 11, 2022 9:28 pm

I never said it was all binary, or a byte stream.
I am trying to read (2) Joysticks, (4) buttons, (4) switches, and (4) pots, arrange the info into a comma separated "command" string, and send over Lora via UART connection,
then..
Receive the command string over LoRa, via UART, parse into variables to perform actions with.
think RC car...sort of.

User avatar
francis
Posts: 28
Joined: Sat Aug 14, 2021 8:14 am

Re: help with E32 program (simple)

Post by francis » Wed May 11, 2022 10:33 pm

Didn't realise you were send it ASCII encoded.

Then '(' is your packet start; ')' is your packet end
Keep on receiving until you get a ')' with an appropriate timeout

Not spectacularly efficient but gorgeously simple.

NateOdem
Posts: 13
Joined: Sun May 08, 2022 5:18 pm

Re: help with E32 program (simple)

Post by NateOdem » Wed May 11, 2022 11:11 pm

So how do I get the receiver to stop reading the line after ')' then? This is the part I am confused about. The only way I get it to work is to add a delay after reading, which slows down the response of the controls.

User avatar
francis
Posts: 28
Joined: Sat Aug 14, 2021 8:14 am

Re: help with E32 program (simple)

Post by francis » Thu May 12, 2022 7:32 am

You don't. A uart is an unstructured stream.
read() gives you everything it has read. It makes no predictions about what it will read. You can ask for everything, or a maximum number of characters to limit memory usage.

It is up to you to handle anything after the ')' as the start of the next packet.

You want something like:

Code: Select all

packet = []
while True:
    got = uart.read()
    for ch in got:
        packet.append(ch)
        if ch == ')':
            handle(packet)
            packet = []
It would be better to use a bytearray than a list for memory reasons, but the above will work

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: help with E32 program (simple)

Post by karfas » Thu May 12, 2022 3:07 pm

I second @francis.

You for sure need to design your data link layer to send/receive whole packes (the short sample program looks like a great starting point for me). This needs to be done while thinking about possible communication failures.
What should happen when
[*] a packet gets corrupted in transit ? How to detect this ?
[*] one or more packets are missed on the receiving side ?

Also note that LoRa might not the right on-the-air protocol to send data continously.
There are regulations you are required to follow. E.g. https://lora.readthedocs.io/en/latest/# ... egulations states that
For example in Europe when using the ISM band frequencies (863 MHz - 870 MHz) users must comply to the following rules:
There is an 0.1% and 1.0% duty cycle per day depending on the channel.
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

NateOdem
Posts: 13
Joined: Sun May 08, 2022 5:18 pm

Re: help with E32 program (simple)

Post by NateOdem » Thu May 12, 2022 4:25 pm

Thank you, I'll try it when I get home tonight.
I was trying to read the 'line' as it came in, and instead, you are suggesting read each character, append the list with that received char, and when the received char == "command end" , execute my function, and reset the list to "empty".
I can follow the logic in that. I'll update tonight.

Thank you.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: help with E32 program (simple)

Post by dhylands » Thu May 12, 2022 9:53 pm

I've found, over the years, that the best way to processing serial data is one character at a time.

I can offer a concrete example here:
https://github.com/dhylands/bioloid3/bl ... et.py#L197

It processes characters coming from a serial line and they're formatted for talking to bioloid servos. The format of the packet looks like this:

FF FF ID LEN INSTR PARAM-11 ... PARAM-N CHECKSUM

I'm not saying you should use this format, I'm just presenting it as an example. The process_byte routine then returns NOT_DONE if the character wasn't the last character from a packet, or CHECKSUM if a checksum error occurs or NONE when the packet was parsed successfully.

Generally, for efficiency, I issue a read with a buffer to get as many characters as are available from the UART and then I call process_byte for each character. When I get a NONE error code then I go and process the packet, and when I've finished processing the packet, I then continue processing characters in the buffer, and then repeat the whole process all over again.

This mechanism works reliably for receiving partial packets or multiple packets, and is the only sane way I've seen for dealing with data.

readline is essentially a really simple variation of this that just happens to do most of the work for you behind the scenes.

NateOdem
Posts: 13
Joined: Sun May 08, 2022 5:18 pm

Re: help with E32 program (simple)

Post by NateOdem » Thu May 12, 2022 10:53 pm

Yea, I just wrote something up using that idea myself. I just need to wait until I get home to try it out on the hardware.
It reads 1 byte at a time, and if it's not the "start" char, then it does nothing, if it is, then it appends the list until it reaches the "end" char, in which case it will check to see if the len(list) is correct, and if so, it will perform the function, and clear the list. ready to loop around.
I think this will ensure that when it starts reading the UART, nothing will happen until it gets to the beginning of the "command", it will not continue after the end of the "command" , and will make sure the command is complete before parsing and completing the function(s). At least, it's my logic so far, let me know if I'm wrong, or if there is a better way.
Again, I'll know more tonight when I'm in front of the hardware.

NateOdem
Posts: 13
Joined: Sun May 08, 2022 5:18 pm

Re: help with E32 program (simple)

Post by NateOdem » Fri May 13, 2022 3:56 pm

Ok, well, it worked when feeding it data through the repl, but not wile receiving data over uart. :x What am I not dong right?

User avatar
francis
Posts: 28
Joined: Sat Aug 14, 2021 8:14 am

Re: help with E32 program (simple)

Post by francis » Sat May 14, 2022 10:18 am

> Ok, well, it worked when feeding it data through the repl, but not wile receiving data over uart. :x What am I not dong right?

Honestly that is a ridiculous diagnostic. But I'll attempt to answer based on the available information:

You are probably doing something wrong

Post Reply