Go Down

Topic: Serial interface date capture and manuipulation, problem!! (Read 4014 times) previous topic - next topic

ccfj1

Hi All,

I'm new to Adruino and C programming...

I have  a project on the go at the moment and I'm using a v3 nano, I have the serial (RX) pin connected to my test bench setup and I'm pulling in the data from the serial port.

Set up as follows.
--
#define INPUT_PIN 2

void setup() {
     //start serial connection
    Serial.begin(9600);
    Serial.println("Starting Serial");
    pinMode(INPUT_PIN , INPUT);
    Serial.println("Start int ready");
}

So far so good.

Now on to the main loop.

void loop() {
char incomingByte;
if (Serial.available() > 0) {
  incomingByte = Serial.read();
        Serial.println(incomingByte, HEX);
  }
}

So Far so good, this does indeed read my data from the Serial interface and when I use the Monitor I get this.

FFFFFFF0
4
68
48
FFFFFF80
54

Perfect, however, all of the above data is serial injected onto the port at location incomingByte[0] (if you get what I mean).

So, if I did not use "Serial.println" I would get 1 long stream of data on the monitor, not so good.

What I want to be able to do is read the data in as packet like so

FFFFFFF0 4 68 48 FFFFFF80 54

The problem that I have is that the data stream could be any size from 5 to 24 bytes long, so making a statement to read only the required amount is proving difficult.

Luckily the 2nd byte is in fact the packet length, but reading this in as a sequence can sometimes get blank entries (FFFFFFFF) in between the valid packets, so tying down the 2nd byte is not proving reliable.

So this (below) does not give a reliable result!!
--
void loop() {
char incomingByte;
char sequenceBytes[4];
if (Serial.available() > 0) {
  incomingByte = Serial.read();
   //     Serial.println(incomingByte, HEX);
      if (Serial.available() > 0) {
        sequenceBytes[0] = Serial.read();
      }
      if (Serial.available() > 0) {
        sequenceBytes[1] = Serial.read();
      }
      if (Serial.available() > 0) {
        sequenceBytes[2] = Serial.read();
      }
      if (Serial.available() > 0) {
        sequenceBytes[3] = Serial.read();
      }
      if (Serial.available() > 0) {
        sequenceBytes[4] = Serial.read();
      }
}
}
--

Can anyone think of a way to push the values into an array (of the right size) so that I can then read it and then manipulate it, the way I want.

Sorry if this sounds vague, I know it is, but I'm stuck

John



Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy