Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

75%
+4 −0
Q&A Communication with microcontroller via usart - using strings a good idea?

I have a PCB with an onboard microcontroller that I use to turn on and off a bunch of switches/relays. To control which relay should be on I use my computer to communicate via USART to the microcon...

4 answers  ·  posted 1mo ago by Carl‭  ·  last activity 1mo ago by Pete W‭

Question microcontroller C protocol UART-USART
#3: Post edited by user avatar Nick Alexeev‭ · 2026-08-05T20:06:27Z (about 1 month ago)
#2: Post edited by user avatar Carl‭ · 2026-08-05T06:04:44Z (about 1 month ago)
  • I have a PCB with an onboard microcontroller that I use to turn on and off a bunch of switches/relays. To control which relay should be on I use my computer to communicate via USART to the microcontroller, with a local python script sending commands.
  • Right now, I have it set up such that my python script sends two things. First, a string containing the command. Second, the switch number that the command affects.
  • ```python
  • #main.py
  • establish_usart_connection()
  • usart.write(b'RELAY_ON\n')
  • usart.write(bytes([1])) #Turn on channel 1
  • end_usart_connection()
  • ```
  • Then in my microcontroller code I always wait to receive both a string-command and a number. Then, I compare the string-command with my cases using `strcmp` to determine what the microcontroller should do. Essentially something like this.
  • ```C
  • #include <string.h>
  • #define USART_BUFFER_SZ 64
  • static uint8_t usart_buffer[USART_BUFFER_SZ] = {0};
  • static uint8_t switch_number = 0;
  • int main(void)
  • {
  • usart_receive_line(usart_buffer, USART_BUFFER_SZ);
  • switch_number = usart_rx(); //Infinite while loop until receiving a number
  • if(strcmp((const char*)usart_buffer, "RELAY_ON") == 0)
  • {
  • digital_write(relay_arr[switch_number],HIGH);
  • }
  • else if(strcmp((const char*)usart_buffer, "RELAY_OFF") == 0)
  • {
  • /* Turn off relay instead */
  • }
  • }
  • ```
  • **Question**: Is it a good approach to use strings when communicating via usart? In my opinion it makes the code easier to read, because you can clearly see that "okay this commands turns on a relay". The issue that I imagine though, is that this is not a robust approach. But maybe I'm wrong?
  • Also, with this approach I have to include the `string.h` library in my microcontroller code in order to use `strcmp()` which just seems a little silly/unnecessary because of the overhead.
  • I have a PCB with an onboard microcontroller that I use to turn on and off a bunch of switches/relays. To control which relay should be on I use my computer to communicate via USART to the microcontroller, with a local python script sending commands.
  • Right now, I have it set up such that my python script sends two things. First, a string containing the command. Second, the switch number that the command affects.
  • ```python
  • #main.py
  • establish_usart_connection()
  • usart.write(b'RELAY_ON\n')
  • usart.write(bytes([1])) #Turn on relay 1
  • end_usart_connection()
  • ```
  • Then in my microcontroller code I always wait to receive both a string-command and a number. Then, I compare the string-command with my cases using `strcmp` to determine what the microcontroller should do. Essentially something like this.
  • ```C
  • #include <string.h>
  • #define USART_BUFFER_SZ 64
  • static uint8_t usart_buffer[USART_BUFFER_SZ] = {0};
  • static uint8_t switch_number = 0;
  • int main(void)
  • {
  • usart_receive_line(usart_buffer, USART_BUFFER_SZ);
  • switch_number = usart_rx(); //Infinite while loop until receiving a number
  • if(strcmp((const char*)usart_buffer, "RELAY_ON") == 0)
  • {
  • digital_write(relay_arr[switch_number],HIGH);
  • }
  • else if(strcmp((const char*)usart_buffer, "RELAY_OFF") == 0)
  • {
  • /* Turn off relay instead */
  • }
  • }
  • ```
  • **Question**: Is it a good approach to use strings when communicating via usart? In my opinion it makes the code easier to read, because you can clearly see that "okay this commands turns on a relay". The issue that I imagine though, is that this is not a robust approach. But maybe I'm wrong?
  • Also, with this approach I have to include the `string.h` library in my microcontroller code in order to use `strcmp()` which just seems a little silly/unnecessary because of the overhead.
#1: Initial revision by user avatar Carl‭ · 2026-08-05T06:03:44Z (about 1 month ago)
Communication with microcontroller via usart - using strings a good idea?
I have a PCB with an onboard microcontroller that I use to turn on and off a bunch of switches/relays. To control which relay should be on I use my computer to communicate via USART to the microcontroller, with a local python script sending commands.

Right now, I have it set up such that my python script sends two things. First, a string containing the command. Second, the switch number that the command affects.

```python
#main.py
establish_usart_connection()
usart.write(b'RELAY_ON\n')
usart.write(bytes([1])) #Turn on channel 1
end_usart_connection()
```

Then in my microcontroller code I always wait to receive both a string-command and a number. Then, I compare the string-command with my cases using `strcmp` to determine what the microcontroller should do. Essentially something like this.

```C
#include <string.h>

#define USART_BUFFER_SZ 64
static uint8_t usart_buffer[USART_BUFFER_SZ] = {0};
static uint8_t switch_number = 0;
int main(void)
{
    usart_receive_line(usart_buffer, USART_BUFFER_SZ);
    switch_number = usart_rx(); //Infinite while loop until receiving a number
    if(strcmp((const char*)usart_buffer, "RELAY_ON") == 0)
    {
        digital_write(relay_arr[switch_number],HIGH);
    }
    else if(strcmp((const char*)usart_buffer, "RELAY_OFF") == 0)
    {
        /* Turn off relay instead */
    }
}
```

**Question**: Is it a good approach to use strings when communicating via usart? In my opinion it makes the code easier to read, because you can clearly see that "okay this commands turns on a relay". The issue that I imagine though, is that this is not a robust approach. But maybe I'm wrong?

Also, with this approach I have to include the `string.h` library in my microcontroller code in order to use `strcmp()` which just seems a little silly/unnecessary because of the overhead.