Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

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

  1. #1

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

    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.

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,808
    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.

  3. #3
    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).

  4. #4
    Senior Member
    Join Date
    Jan 2013
    Posts
    184

    CmdMessenger

    You might want to consider using something like CmdMessenger instead of rolling your own.

  5. #5
    Very interesting, thank you markonian.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •