Go Down

Topic: byte array copy problem (Read 373 times) previous topic - next topic

superzch

    i have some problem when copy the byte array.i send "AA 12 34" through the serial port  and copy it to readData[] byte array . But after i copy the data of readData[] to sendData[] , it give me an error feedback "AA 12 AA". i don't know why 34 change to AA.

GetData:AA
GetData:12
GetData:34
NewData:AA
NewData:12
NewData:AA

Code: [Select]
byte readData[2],sendData[2];

void setup() {
  Serial.begin(9600);
  while (!Serial) {
  }
}

void loop() {
  if(Serial.available()>0) {
    for(int i=0;i<3;i++) {
      readData[i]=Serial.read();
      if ((i==0 && (readData[i]!=0xAA)) ||(i!=0 && (readData[i]==0xAA || readData[i]==0x00))) {
        break;
      }
      delay(2);
    }

    for(int i=0;i<3;i++) {
      Serial.print("GetData:");
      Serial.println(readData[i],HEX);
    }

    for(int i=0;i<3;i++) {
      sendData[i]=readData[i];
      delay(100);
    }

    for(int i=0;i<3;i++) {
      Serial.print("NewData:");
      Serial.println(readData[i],HEX);
    }

  }
}

UKHeliBob

How many array elements are the in each of these arrays ?
Code: [Select]
byte readData[2],sendData[2];
How many items are you putting in each array ?
Are the numbers the same ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

superzch

I Just send 3 bytes and the two arrays shoud be the same.

UKHeliBob

How many bytes can you put in an array declared as having 2 elements ?
How many bytes are you putting in the array ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

AWOL

Code: [Select]
if(Serial.available()>0) {
    for(int i=0;i<3;i++) {
      readData[i]=Serial.read();

Does anyone see a problem here?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Nick Gammon

Yes.

Plus:

Code: [Select]

byte readData[2],sendData[2];

...

    for(int i=0;i<3;i++) {
      sendData[i]=readData[i];
      delay(100);
    }



Apart from the unusual use of delay here, you are copying 3 bytes into a 2-byte array.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

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