Reading and Parsing Serial Data

Hello, I am working with a robot and an Arduino mega and trying to receive packets of data from the robot through UART. For right now I have been trying to use serial.readBytes() and trying to store the data into an array. Is there a way to store the packets of data that I receive back and then be able to look at all the data in that array? Right now I seem to just be receiving the size of the packet back in the number of bytes from the robot. (ex. i receive 24 in the serial monitor as the response to the printing of the Serial.readBytes command.)
Here is the code:

</
#include <SoftwareSerial.h>
SoftwareSerial mySerial(0, 14); // RX, TX
byte val[6] = {0xAA, 0xAA, 0x02, 0x14, 0x00, 0xEC}; //get alarms

const int BUFFER_SIZE = 28; // sets size
byte buf[BUFFER_SIZE]; // creates array of that size. readBytes only allows char or byte

void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
Serial.setTimeout(2000); //sets timeout for the read
while (!Serial) {
// wait for serial port to connect. Needed for Native USB only
}
// set the data rate for the SoftwareSerial port
mySerial.begin(115200);
}

void loop() // run over and over
{
if
(mySerial.available()>0);
Serial.println(mySerial.available()); // prints the available serial bytes (printing zeros currently)

mySerial.write(val, 6); // selects the message to send and its length
mySerial.read();
Serial.println(mySerial.read(), HEX); // prints FFFFFFF rn

//int rlen = Serial.readBytes(buf, BUFFER_SIZE);
char box1 = Serial.readBytes(buf, BUFFER_SIZE);
Serial.println(box1, DEC); // prints the received data rn the size
/>

Try printing buf.

Please use code tags when posting code.

This line does nothing useful (mind the semicolon).

  if
    (mySerial.available()>0);

This line attempts to read a character even when none have been sent, in which case it returns the integer -1.

   mySerial.read();

You will find it worthwhile to study the Serial Input Basics tutorial.

The serial input basics tutorial shows how to read serial data into a string (null terminated character array) using non blocking code. There is also a section that shows how to parse data from the serial input.

Serial.println(mySerial.available()); // prints the available serial bytes (printing zeros currently)

I am not sure that you understand what the available() function actually returns. It returns the number of bytes available to be read from the receive buffer, not the actual data.

wildbill:
Try printing buf.

Haha thank you. This seemed like the obvious solution in retrospect but it works and I can finally get the message back that I was looking for now!
Thanks!!

read serial data into a string (null terminated character array)

Don't do this if your incoming data includes 0x00.
Instead use input basics but don't try to process the result as a c-string. Just look at the uint8_t bytes themselves. You will need to track the number of bytes you have put into the array.

If your input is actually really text input then check out Arduino Software Solutions for various solutions to reading text from a Serial input with the pros and cons of each and choose the one that suits you.

@OP

1. How many data bytes are you expecting to receive from robot in a frame?
2. Are you expecting to receive ASCII-codes bytes or natural-binary bytes?
3. Is thereany "START Mark" for the froma of Step-1? If so, what is this?
4. Is thereany "STOP Mark" for the froma of Step-1? If so, what is this?
5. Do you need to issue any command from MEGA to your robot to get data? If so, what is command?

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