ESP32 UART reading special characters including NULL

blim_acs
Posts: 4
Joined: Fri Jan 05, 2024 8:38 pm

ESP32 UART reading special characters including NULL

Postby blim_acs » Tue Oct 29, 2024 8:18 pm

Hello,

I am trying to parse serial messages being sent to the ESP32 but the function "uart_read_bytes" seems to look for a NULL terminator similarly to printf. My message has special characters including the NULL terminator so I was wondering if there was any function I can use to extract the raw data from the uart rx buffer.
This is the message the ESP32 receives:
0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x5f 0x0d 0x00 0x1b 0x79

It breaks the message into 2 parts; the 2nd message being 0x00 0x1b 0x79.

MicroController
Posts: 1673
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: ESP32 UART reading special characters including NULL

Postby MicroController » Tue Oct 29, 2024 10:04 pm

blim_acs wrote:
Tue Oct 29, 2024 8:18 pm
the function "uart_read_bytes" seems to look for a NULL terminator similarly to printf.
No, it doesn't.
It breaks the message into 2 parts; the 2nd message being 0x00 0x1b 0x79.
Since UART transmissions have no concept of 'messages', only of bytes, the size and number of chunks you receive depends on the sizes of buffers and timing. You'll have to determine what defines the start and/or end of a 'message' and piece together a message from the bytes you receive based on that.

blim_acs
Posts: 4
Joined: Fri Jan 05, 2024 8:38 pm

Re: ESP32 UART reading special characters including NULL

Postby blim_acs » Wed Oct 30, 2024 10:08 pm

That was what I expected to happen but when I print the RX buffer immediately after reading, I get multiple messages compared to the single message being sent to the esp32.
  1. int len;
  2. char tmp_buf[128] = "";
  3. uart_get_buffered_data_len(CONSOLE_UART_NUM, (size_t*)&len);
  4. uart_read_bytes(CONSOLE_UART_NUM, tmp_buf, len, (TickType_t)portMAX_DELAY);
  5. printf("s: ");
  6. for (int i = 0; i < len; i++)
  7. {
  8.     printf("%c", tmp_buf[i]);
  9. }
  10.                     printf("\n");
Do you see something wrong in the way I am using the function that would result in this ?

MicroController
Posts: 1673
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: ESP32 UART reading special characters including NULL

Postby MicroController » Thu Oct 31, 2024 12:40 am

blim_acs wrote:
Wed Oct 30, 2024 10:08 pm
I get multiple messages compared to the single message being sent to the esp32.
What do you mean? Do you get more bytes than were actually sent over the wire?

Or do you just get the bytes from more than one message with a single uart_read_bytes() call? - If so, that is also to be expected, depending on timing and buffers. When I wrote that you'd have to piece together the messages, I meant to imply that you'll also have to split the chunks of bytes into messages. The UART doesn't know when a 'message' starts or ends; it will just give you any bytes it receives in the order they arrived.
Do you see something wrong in the way I am using the function that would result in this ?
Can't tell.
But in any case you should make sure that you limit the length you pass to uart_read_bytes() to not more than sizeof(tmp_buf) in case there are more bytes in the UART buffer(s) than would fit into tmp_buf.

Who is online

Users browsing this forum: No registered users and 57 guests