Distinguish Data On Serial Communication

Hi Everyone!

I have a question concerning serial communication: I have a joystick connected to a PC which I would like to send the input to my Arduino UNO, up to now I can get all the joystick information to Serial, my question is how can distinguish data coming to the Arduino? For example, the joystick has 4 axes, how to send data via Serial and know which axis does the data belong to ?

Thanks!

2 options:

  1. You would send data telling the Arduino which axis the data was for, such as
<X,123>

This says the X axis value is 123.

  1. Send both/all axis data as one message where the position in the message tells the Arduino which axis is which, like
<xxx,yyy,zzz,www>

In this format, the Arduino knows the Z axis value is always the third value.

1 Like
1 Like

I like the : <X, 123> approch better.

I was thinking of sending structs,but
small issue is i can ONLY send String and the implementation isn't quite as straight forward as i thought.

Can you please elaborate on this constraint?

Yes!

I am using Qt and the serial communication function used to write on the serial, expect a QByteArray( QByteArray can be used to store both raw bytes (including '\0's) and traditional 8-bit '\0'-terminated strings ) so i can't really send a struct(only QByteArray or Strings)

Please post a link to Qt, "the serial communication function" and the QByteArray.

Expect serious problems using String objects on the Uno. There is always a way to avoid them.

Here is the link : QIODevice Class | Qt Core 5.15.12

https://doc.qt.io/qt-5/qbytearray.html

Because of possible packing issues I would avoid the struct and just send the QByteArray character arrays "<X,123>".

I don't think the efficiency of the binary transmission of the struct outweighs the complexity of implementing it.

From what I can tell, String Objects are not needed on the Arduino side.

1 Like

Why?

1 Like

The fact that a String object was used when sending the data in no way requires the use of String on the receiving device.

Seems easier and more straight forward to implement, i will try both approches tonight and see which one is more convenient.

That part of my problem, i don't know where to start to implement this on the UNO side.

I guess on the Qt side i can create a function doing the "<X,123>" and changing X and 123 depending on what axis and variable change.

With the Serial Input Basics tutorial linked in reply #2.

1 Like

Okay! I will have a look. One last thing : is it normal that i can't use the Serial Monitor while i am receiving data ? Is there a way to counter this ?

You need to sync the arduino with the incoming messages. That's why I suggested the "<" and ">" - they can be used for that purpose. Otherwise, the Arduino could start receiving part way through a message and it wouldn't make sense.

So your arduino code would wait for a "<", then the next character would be the axis/joystick channel. Then there would be the comma (you could forget the comma if the channel will always be a single character. Then you can use Serial.parseInt() to read the value. Finally there would be the ">" markling the end of the message which you can discard.

1 Like

If you have your PC program using QT connected to the usb serial port of the Arduino Uno and its onboard usb>ttl converter, you can not connect the monitor to that same port.

You should be able to connect a second USB serial device using a separate usb to ttl converter connected to pins with a software serial instance.

1 Like

Thanks everyone, i was able to get everything to work correctly!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.