Hello,
I have 2 pcs. Xbee Pro Series 1 with shields.
I can't to figure out how to send two six digit variables between two arduinos with xbee. On transmitter side send two variables at once then receive on second arduino and decode back to two variables. Can someone help with code?
I need to send this example:
transmit:
var1 = 123456;
var2 = 654321;
receive
var1 = 123456;
var2 =654321;
Can someone please help with code?
as I understand transmitter need to looks like this. But how to decode it to split it back to two variables?
ok, I will use long int if I can do this of course.
I using Serial transmit and receive.
What is the best way to collect data?
I was start to do something but I don't know is it right way.
void loop() {
while(Serial.available() >0) { //only when serial active
header = Serial.read(); // header gets 1st byte
if(header == 65) { // if header is 65 (A), bytes 1 and 2 are next two serial reads
byte1 = Serial.read();
byte2 = Serial.read();
}
}
Serial.print (byte1); // to see what I was received
Serial.print (byte2); // to see what I was received
}
with this I can see next 2 bytes after header A but I don't know how to stick together 6digits in one number (in one variable)
May be you can suggest some better solution and code.
while(Serial.available() >0) { //only when serial active
header = Serial.read(); // header gets 1st byte
if(header == 65) { // if header is 65 (A), bytes 1 and 2 are next two serial reads
byte1 = Serial.read();
byte2 = Serial.read();
If there is at least one byte to read, read it, and then possibly two more. Can you see why this is a bad idea?
When you use Serial.print() on the sender, it converts the value to a string, and sends several characters. You are not sending binary data (and there would need to be FOUR reads if you were, since a long is 4 bytes).
Change your sender to send something like "<123456, 654321>".
Then use code like this:
#define SOP '<'
#define EOP '>'
bool started = false;
bool ended = false;
char inData[80];
byte index;
void setup()
{
Serial.begin(57600);
// Other stuff...
}
void loop()
{
// Read all serial data available, as fast as possible
while(Serial.available() > 0)
{
char inChar = Serial.read();
if(inChar == SOP)
{
index = 0;
inData[index] = '\0';
started = true;
ended = false;
}
else if(inChar == EOP)
{
ended = true;
break;
}
else
{
if(index < 79)
{
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}
// We are here either because all pending serial
// data has been read OR because an end of
// packet marker arrived. Which is it?
if(started && ended)
{
// The end of packet marker arrived. Process the packet
// Reset for the next packet
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
To read the data sent. Where it says "Process the packet", use strtok() and atol() to get the tokens ("123456" and "654321") and convert them to values.
Looks dificult but after several times read I was understand some things.
ok, but how to write right send comand?
int long var1 = 123456;
int long var2 = 654321;
void setup(){
Serial.begin(9600);
}
void loop (){
Serial.print(<var1,var2>); //how to write this correctly??
}
ou, sorry for that, my head is boiling from all info what I was read all day!
ok, as I understand that all data in receive is in inData variable with diferent index, I will be very glad if you can help with total decote all inDtata to two variables var1 and var2