Pages: [1]   Go Down
Author Topic: How to receive byte array and put it into another byte array through serial port  (Read 672 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There is a byte array being sent from one Arduino(A) to another Arduino (B) like this

//*******************************Arduino( A) *************************************************
byte crypt [8]={0x7E, 0x05, 0xBD, 0xEB, 0x1A, 0x3A, 0x6E, 0x91 };
void loop(){
      for (int j=0;j<8;j++)
    {
      if (crypt[j]<0x10){
      Serial.print("0");
      }
      Serial.print(crypt[j],HEX);
      Serial.print(" ");
    }
   
   Serial.println();
    delay(1000);
}

what i get from serial monitor for this code is
7E 05 BD EB 1A 3A 6E 91

//**********************************************************************************************

I have connected the Rx of the arduino(A) to Tx of the arduino(B) and Tx to Rx.

//*********************************Arduino(B)******************************************************************
byte crypt [8];
void loop(){
while(Serial.available() <=0); // wait for incoming serial data

  if (Serial.available() >= 8 )  // wait for eight bytes
  {
    for(int i=0;i <=8; i++) crypt = Serial.read();
     for (int j=0;j<8;j++)
    {
    if (crypt[j]<0x10){
      Serial.print("0");
      }
      Serial.print(crypt[j],HEX);
      Serial.print(" ");
    }
    Serial.println("");
  }
}

what i get from serial monitor for this code is
37 45 20 30 35 20 42 44
20 45 42 20 31 41 20 33
20 36 45 20 39 31 20 0D
//*********************************************************************************************************************

The problem is how do i read the bytes in arduino(B) and put it into a byte array like it was in the arduino(A)?


Any help would be appreciated


 
« Last Edit: January 29, 2013, 05:51:11 am by hukasasha » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The problem is that you are converting binary data to ASCII data, and now you want to convert it back to binary data. The question is why? Why not send the data in binary, so that no conversion is required on either end?

Of course, you could not visualize that data in the Serial Monitor, but, the Serial Monitor is not where that data is needed, is it?
Logged

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

@ PaulS

Does that mean i have to send it using "Serial.write"  rather than "Serial.print"?

No, its not the serial monitor where the data is needed. I just wanted to be sure the received data is put into a byte array exactly like it was sent, so that i can take it and process it in the other end(in arduino B).
« Last Edit: January 29, 2013, 07:02:07 am by hukasasha » Logged

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

@ PaulS

Thank you for the hint.

I replaced "Serial.print" with "Serial.write" and it worked!

here is the fix:-

//*******************************Arduino( A) *************************************************
byte crypt [8]={0x7E, 0x05, 0xBD, 0xEB, 0x1A, 0x3A, 0x6E, 0x91 };
void loop(){
      for (int j=0;j<8;j++)
    {
      Serial.write(crypt[j]);
    }
    delay(1000);
}

what i get from serial monitor for this code is
~½ë:n‘       //which doesn't matter how it looks

//**********************************************************************************************

I have connected the Rx of the arduino(A) to Tx of the arduino(B) and Tx to Rx.

//*********************************Arduino(B)******************************************************************
byte crypt [8];
void loop(){
while(Serial.available() <=0); // wait for incoming serial data

  if (Serial.available() >= smiley-cool  // wait for four bytes
  {
    for(int i=0;i <=8; i++) crypt=Serial.read();
    for (int j=0;j<8;j++)
    {
      if (crypt[j]<0x10){
      Serial.print("0");
      }
      Serial.print(crypt[j],HEX);
      Serial.print(" ");
    }
    Serial.println("");
  }
}

what i get from serial monitor for this code is
7E 05 BD EB 1A 3A 6E 91   //just like how i wanted it smiley
//*********************************************************************************************************************
Logged

Pages: [1]   Go Up
Jump to: