Hi
I reach out to this forum to see if someone can help me to explain how the Serial.read() function actually works on a Arduino Mega board.
I have read dozens of articles and examples and it seems straight forward and quite simple - but I'm completely unable to to get it to work.
A small background to the project I'm working with.
I'm have a task where i will try to parse a data stream coming from an application called "CondorUDP2Com". The use of this program is to decode a data stream from Condor Soaring 2 and present it on a com port so it can be used for example by Arduinos.
I will use this data to ultimately drive step motors so simulate instruments in a cockpit.
Data will arrive in frames of a header , start of data marker , variable index and a float value.
An example of a data frame is like this.
78 78 78 78 78 78 xxxxxx
7a 01 c5 54 da 41 z 1 27.29139 // Speed in knots
7a 02 f6 66 79 44 z 2 997.60876 // Altitude in feet
7a 03 e2 55 54 bf z 3 -0.829443 // Vario m/s ??
7a 04 f4 55 54 bf z 4 -0.829443 // Electric vario m/s ??
(Info after the 6 HEX values is just my own notes to know what I expect to see at the end)
The programmer of the CondorUDP2Com has described the frame as
At the beginning of each frame, 6 bytes (hex: 78) are sent. Then to indicate which value is sent over, two bytes are transmitted (first is always 7A and second is an integer indicating index from "UDP preview". Then 4 bytes are sent, which is just a float value.
Enough background
I use a Arduino Mega 2560 bord and I have connected Digital pin 18/19 as Serial1, Serial (USB) used for monitoring and upload of data.
If I try to fetch data from the CondorUPD2Com directly or if I try to emulate data by sending it via the programs Termite Terminal or Pololu Serial Transmitter don't I get the data I expect
To my understanding so should Serial1.read() rad one byte , in my world so should it have been one "78" (HEX) corresponding to a char/integer value of 120 in the Arduino.
But if I try so simulate the header by sending 0x78 in so do I get it decoded as
Setup finished
Available bytes: 1
Read hex: 232
1 loop(s) done
Available bytes: 5
Read hex: 232
2 loop(s) done
Available bytes: 4
Read hex: 232
3 loop(s) done
Available bytes: 3
Read hex: 232
4 loop(s) done
Available bytes: 2
Read hex: 232
5 loop(s) done
Available bytes: 1
Read hex: 8
6 loop(s) done
Why do I get 232 (HEX 9A) and not 120 as I expect ? And why is the last byte a 8 and not a 120?
My expectation is to receive 120 120 120 120 120 120 equivalent of x x x x x x as the beginning of the data frame is.
Attached is the program I'm trying with for the moment - disclaimer I'm a newbie programmer when it comes to Arduino code, and it contains a lot of bloat variables that are a result of hours of unsuccessful attempts to identify the incoming data.
The way I try to tackle this problem is to first sync into a data fram by reading the "78":s (x) and discarding them, when a "79" (z) is encountered do I know that I'm on a "data line" - discard that one.
Read the variable and the float - will probably be done via arrays.
But now is the program stripped down to just trying to identify a header sign - and I'm not able to do it.
Thanks a lot for some input as this drives me insane as i can't figure out what is going wrong
Probably some very easy data conversion somewhere - but I can't se it
Br
Gerth
main.cpp (3.06 KB)