Post History
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...
#2: Post edited
- 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
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.
