Serial Communication-Arduino to Arduino

Hello All,

Okay, I'm trying to send ints and floats serially between two Arduino's but I have been unsuccessful at this so far.

I'm using this code to send the data

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

void loop()
{
if(Serial.available()>0)
{
if(Serial.read()=='S')
{
float test = 87;
writeBytesToSerial(test);
}
}
}

void writeBytesToSerial(int toWrite)
{
union u_tag
{
// byte[4] bytes;
byte bytes[2];
float buffer;
} u;

u.buffer = toWrite;

Serial.write(u.bytes[0]);
Serial.write(u.bytes[1]);
Serial.write(u.bytes[2]);
Serial.write(u.bytes[3]);
}

And this code to read the data

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

void loop()
{
Serial.write('?');
while(Serial.available()<2)
{
//do nothing
}
float test_rec;
test_rec=ReadBytesFromSerial();
Serial.println(test_rec);
Serial.flush();
}

int ReadBytesFromSerial()
{
union u_tag
{
// byte[4] bytes;
byte bytes[2] ;
float buffer;
} u;

u.bytes[0]=Serial.read();
u.bytes[1]=Serial.read();
u.bytes[2]=Serial.read();
u.bytes[3]=Serial.read();

return(u.buffer);
}

When I run both of these I get something like -2203. Can anyone help me get this work??? PLEASE!!!

You need to change:

void writeBytesToSerial(int toWrite)

to

void writeBytesToSerial(float toWrite)

Then we'll take it from there :slight_smile:


Check out our new shield: http://www.ruggedcircuits.com/html/gadget_shield.html

And

Have you connected the grounds together? :slight_smile:

 while(Serial.available()<2)
 {
     //do nothing
 }
 float test_rec;
 test_rec=ReadBytesFromSerial();

So, as soon as there are 2 bytes to read:

 u.bytes[0]=Serial.read();
 u.bytes[1]=Serial.read();
 u.bytes[2]=Serial.read();
 u.bytes[3]=Serial.read();

you read all 4 of them. And it doesn't work. No real surprise there.

Wow, I guess I forgot to change a few things in my code. It's amazing what you don't see after staring at something for so long. :o

I didn't know you had to connect grounds together, so I'll try that later on today (thanks mowcius!!) By the way, why do I need to connect the grounds?

So here is what could looks like after I made the suggested changes:

I'm using this code to send the data

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

void loop()
{
if(Serial.available()>0)
{
if(Serial.read()=='?')
{
float test = 87;
writeBytesToSerial(test);
}
}
}

void writeBytesToSerial(float toWrite)
{
union u_tag
{
byte bytes[2];
float buffer;
} u;

u.buffer = toWrite;

Serial.write(u.bytes[0]);
Serial.write(u.bytes[1]);
Serial.write(u.bytes[2]);
Serial.write(u.bytes[3]);
}

And this code to read the data

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

void loop()
{
Serial.write('?');
while(Serial.available()<4)
{
//do nothing
}
float test_rec;
test_rec=ReadBytesFromSerial();
Serial.println(test_rec);
Serial.flush();
}

float ReadBytesFromSerial()
{
union u_tag
{
byte bytes[4] ;
float buffer;
} u;

u.bytes[0]=Serial.read();
u.bytes[1]=Serial.read();
u.bytes[2]=Serial.read();
u.bytes[3]=Serial.read();

return(u.buffer);
}

Thanks PaulS and RuggesCircuits for your suggestions! Hopefully this will work when I test later on today.

I didn't know you had to connect grounds together, so I'll try that later on today (thanks mowcius!!) By the way, why do I need to connect the grounds?

Becuase otherwise there is no potential difference.
If you are using a DC power supply for both of them then you don't normally (as the grounds are effectively tied together then) but if you are using batteries then you do as they are not connected in any way.

If they are both connected via USB then you don't need to either as they are both effectively connected to the same power source.

Mowcius

Okay, I tried running the modified code listed below and all it did was print a single ' ? ' on the Serial Monitor. So I'm quite puzzled. The program should send the ? to the Serial Port and when the other Arduino see is it should start running the writeBytesToSerial function, but apparently it does not work. Any suggestions? Other methods, changes to my code?

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

void loop()
{
if(Serial.available()>0)
{
if(Serial.read()=='?')
{
float test = 87;
writeBytesToSerial(test);
}
}
}

void writeBytesToSerial(float toWrite)
{
union u_tag
{
byte bytes[4];
float buffer;
} u;

u.buffer = toWrite;

Serial.write(u.bytes[0]);
Serial.write(u.bytes[1]);
Serial.write(u.bytes[2]);
Serial.write(u.bytes[3]);
}

And this code to read the data

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

void loop()
{
Serial.write('?');
while(Serial.available()<4)
{
//do nothing
}
float test_rec;
test_rec=ReadBytesFromSerial();
Serial.println(test_rec);
Serial.flush();
}

float ReadBytesFromSerial()
{
union u_tag
{
byte bytes[4] ;
float buffer;
} u;

u.bytes[0]=Serial.read();
u.bytes[1]=Serial.read();
u.bytes[2]=Serial.read();
u.bytes[3]=Serial.read();

return(u.buffer);
}

Two points

  1. For godsake use the # button when posting code.

By the way, why do I need to connect the grounds?

see:-
http://www.thebox.myzen.co.uk/Tutorial/Power_Supplies.html

A serial connection has one device on each end.

Okay, I tried running the modified code listed below and all it did was print a single ' ? ' on the Serial Monitor.

So, the Arduino is on one end and the Serial Monitor is on the other end. No place for the other Arduino in this setup, so it, of course, never receives the serial data.

Actually I have two Arduino's connected to each other. I open the Serial Monitor for the Arduino with the Read code to see what it is being sent. Both of the Arduino's are connected to my computer for power.

And sorry about not using the # button, I'm still new to posting.

I open the Serial Monitor for the Arduino with the Read code to see what it is being sent.

No you don't that will not work, the signals are all wrong and will interfere with each other.

It's a LITTLE extra work to learn what you'd need, but if you do it, things will be much easier for you....

Learn how to use the NewSoftSerial library.

If you do that, your Arduinos can communicate with, say, pins D2 and D3, and you CAN use the serial monitor for debugging work.

Details at...

... just use your second Arduino in place of the "Some Serial Device", which in the essay was a "big" PC running Windows XP.

And you can forget about the PA4B... but DO read the stuff about the "rx" and "tx" names, and the way people get confused.

So, the Arduino is on one end and the Serial Monitor is on the other end. No place for the other Arduino in this setup, so it, of course, never receives the serial data.

If the second arduino is connected to the first arduino via the tx/rx pins, then the serial monitor will receive what is being sent from the first arduino, assuming both are set to the same baud rate.

A response to ZoomKat's post, the one just above this....

I have a bad feeling about a setup where digital pins 0 and 1 are being used BOTH for comms with a "host/ development" PC (for the serial monitor) AND for comms with the other Arduino.

Yes, it probably CAN be done, if you get everything "just so"... but there would be fewer ways for "things" to go wrong if the Arduinos were connected via, say, digital pins 2 and 3... pin2 of Arduino "A" to pin 2 of Arduino "B" and 3 to 2.... and one or both Arduino's also communicated with one or two PC's running serial monitors, at least during the development stage of the process.

But then again, I do tend to worry too much. I can't say that I KNOW the "everything over D0 and D1" WON'T work.... I just wouldn't want to fight with the issues that could arise when I can so easily go around even the possibility of them.

I have a bad feeling about a setup where digital pins 0 and 1 are being used BOTH for comms with a "host/ development" PC (for the serial monitor) AND for comms with the other Arduino.

Perhaps a couple of aspirin and some rest. :slight_smile:

Yes, it probably CAN be done, if you get everything "just so"...

Instead of a second arduino, I have my ssc-32 (really no different from a second arduino) connected to the arduino basically using three wires (allagator test leads, but #22 hookup works too). tx/rx, tx/rx, ground/ground. I'm no electrician, but I managed to hook up the three wires. :wink:

The way I use it is checking out a web servo control setup. With the arduino conneced to my laptop and the serial monitor open, I send a servo control request from my web page, which goes thru the router to the arduino ethernet shield. The arduino parces out the control data and sends it out the serial port to the ssc-32. What is sent to the ssc-32 is also displayed in the serial monitor, so I can see if there are issues with how the arduino is handling the request data. No rocket science involved.