Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Creating a serial protocol



  • I have developed an application using raspberry pi with QT frontend, sending messages to Arduino to control stepper motors and reading position back.

    Currently it uses the message format of 'command, motor number 0-3, distance to move (float to 1 decimal place * 10 to send as int)' i.e "move,1,100"

    The arduino then converts back to float by dividing by 10 and running command.

    Serial then sends back 'motor letter, position (multiplied by 10 as above)'

    The QT frontend then updates a DRO

    It works, but I had to slow serial baud rate right down, and I generally feel it is not very robust. As the distances / positions can be somewhere between 0 and say 1000 in 0.1 increments I have had to use delimiters to try and separate messages. This seems messy.

    What would be the best way to create a 'protocol' to send set message length presumably including some kind of start and end delimiter and then the motor and distance/position within this and then parse it?

    e.g "start, motor, distance (fixed length), end" -> "S,X,10.2000,E" or the like; even if it is all numbers to decode



  • Have you proven where you bottleneck or problem is? Just a quick google
    http://forum.arduino.cc/index.php?topic=21806.0 - do you think maybe you are hardware strapped?
    Maybe you just have to slow down for a pi / serial comms

    I process LAN / WAN using UDP with custom commands - can you use Ethernet instead for message processing?
    I don't see anything wrong with defining your own command and processing your commands as you see fit / define.

    (I am targeting very entry level PC hardware)
    My application has the capability of processing incoming network messages at some stupid speeds like 16ms (all I cared to test down to - our minimum requirement is like every ~10s).
    I'm doing stupidly much more work than you too.

    If you get worried about data sizes maybe hex? Dunno, I think you need to identify if you are cpu / mhz strapped or something first before optimizing packet sizes and all that. What you have is the basic data needed - it's perfect why complicate it more. IMO.


  • Qt Champions 2019

    @Dave022 Why are you using float for distance? I guess there is some minimal distance the motor can move? If so than use this minimal distance as 1 and get rid of this float.



  • @6thC said in Creating a serial protocol:

    Maybe you just have to slow down for a pi / serial comms

    The issue was that data was coming in too fast from Arduino and parsing out became a bit of a pain. I was using AccelStepper and returning current position every loop. Not ideal I know as this is just a faux DRO and doesn't show missed steps because it is open loop.

    I guess I was looking for the method of creating packets and decoding them in an efficient manner

    @jsulm said in Creating a serial protocol:

    Why are you using float for distance? I guess there is some minimal distance the motor can move? If so than use this minimal distance as 1 and get rid of this float.

    I was weighing up where to do the math; in the end I decided to send distance in mm from QT frontend ( in increments of 0.1mm) and then let the Arduino "firmware" do the conversion to steps for the driver based in the ratio of the different mechanical actuators


Log in to reply