Loading...
Pages: [1]   Go Down
Author Topic: byte array copy problem  (Read 118 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

    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:
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);
    }

  }
}
Logged

East Anglia (UK)
Offline Offline
Edison Member
*
Karma: 46
Posts: 1380
May all of your blinks be without delay
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How many array elements are the in each of these arrays ?
Code:
byte readData[2],sendData[2];
How many items are you putting in each array ?
Are the numbers the same ?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

East Anglia (UK)
Offline Offline
Edison Member
*
Karma: 46
Posts: 1380
May all of your blinks be without delay
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How many bytes can you put in an array declared as having 2 elements ?
How many bytes are you putting in the array ?
Logged

Global Moderator
UK
Online Online
Brattain Member
*****
Karma: 136
Posts: 18992
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if(Serial.available()>0) {
    for(int i=0;i<3;i++) {
      readData[i]=Serial.read();
Does anyone see a problem here?
Logged

Pete, it's a fool looks for logic in the chambers of the human heart.

Global Moderator
Melbourne, Australia
Offline Offline
Shannon Member
*****
Karma: 218
Posts: 13896
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes.

Plus:

Code:
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.
Logged


Pages: [1]   Go Up
Print
 
Jump to: