How to set up a structured control system over serial port?

Status
Not open for further replies.

amundsen

Well-known member
Hello,

I want to control a program running on a Teensy 3.2 from my computer over the serial port (USB) with structured messages, that is trigger/control specific functions with matching messages, the messages also containing parameters. I plan to use ASCII strings with variable lengths as identifiers rather than using cryptic numeric codes.

Ex :
getstatus board 0xAF
reset
config wordlength 8
config interval 10

Therefore, I have a few questions:
1. Is the principle a bad idea, that is, would branching with short, fixed-length messages like *abc* be much more efficient?
2. What's the most efficient branching structure for control messages? A suite of if+else statements or a switch? In the latter case, is there a simple solution to make it compatible with strings?
3. Even before branching, I need a way to split the message and its arguments and store them. What's the most efficient way to do it? Currently, I plan to insert a special character (for instance an asterisk) to identify the end of the identifier (this would allow functions names with different lengths) then use spaces to separate the identifier and the first parameter or the first and eventuel additional parameters.

Thank you in advance.
 
Variable length messages are fine, as long as there's a clear way to know the message begin & end.

Teensy 3.2 is very fast, relative to the maximum speed of 12 Mbit/sec USB. The difference between switch-case versus if-else usually would not matter for this sort of project. But to answer your question, sometimes the compiler uses a jump table to optimize large switch-case. It depends on the case constants. Sometimes it generates code equivalent to if-else.

If you do care about efficiency, using Serial.read(buffer, maxlen) to read as much data as you can in 1 function call is more efficient than using Serial.available() and Serial.read() for each individual byte. Both are fast enough to keep up with the maximum USB speed. More efficient coding will buy you more free time to do other work while waiting for incoming data. If your project is structured so you receive a command and then perform the task to completion before trying to receive the next command, then there's little point to optimizing the command reading code.
 
Thank you Paul.

My care for efficiency is mostly to avoid slowing down the program with message reception tests while running its main function, that is sending data from its sensors. However when a specific function is triggered, efficiency doesn't matter so much because it will be for configuration or check purposes outside the regular use of the program (art installation).
 
Status
Not open for further replies.
Back
Top