system
December 30, 2011, 8:34am
1
Hi Forum!
I am trying to communicate over serial between two Arduino's-and its only partially working.
I am sending two floats, and two integers: float|float|int|int ex: 2(.)54|345(.)6|1500|1
The packet I send over serial looks like this: 254345615001, after the floats are converted into separate integers: 2.54 to a 2, 5, and a 4.
When I read this packet on the receiving end, I get either the right packet, or a packet that is split up and printed on different lines.
Does anyone know how to fix this?
Thanks,
NJS552
HUD_SEND_TEST.h (1.43 KB)
HUD_RECIEVE_TEST.h (1.16 KB)
Grag38
December 30, 2011, 9:58am
2
This is not the best way to do it... but you do as you want.
One thing that you can implement is to send your packet between 2 special characters that can help you to define the start and the end of your datas.
so from your '254345615001' could be send like '#254345615001 *'
It will allow you to know when you are reading the '#' char that a new packet is incomming, then you store the datas until you read the '*' char.
Also in my guess : for (int i=0; i<=Serial.available(); i++)
is not the right way of doing that.
I prefer :
int i=0;
while (Serial.available()>0)
incomingdata[i++]=Serial.read();
Considering the code with the '#' and '*' delimiters you could write something like that :
void receiveSerial()
{
byte ch;
static int cpt=0;
static byte flag=false;
if (Serial.available()>0)
{
ch = Serial.read();
switch( ch )
{
case '#' :
{
cpt =0;
flag=false;
} break;
case '*' : flag=true; break;
default: incomingdata[cpt++]=ch;
}
}
if (flag==true)
{
Display the incoming datas, as you want
}
}
system
December 30, 2011, 6:14pm
3
Thanks Grag38!
how do you add the # and * characters to the packet? it would have to be something other than 'itoa' or 'dtostrf', right?
Thanks,
NJS552
system
December 30, 2011, 10:54pm
4
Nevermind.
I put the # and * designators in bytes and sent them before and after the main string.
The FINAL sending code is here:
//HUD Send
char sendSerial[100];
char replySerial[100];
int depthMeters_1;
int depthMeters_2;
int depthMeters_3;
int compassHeading_1;
int compassHeading_2;
float depthMeters = 1.23;
float compassHeading = 456.7;
int verticalMotors = 8912;
int metal = 3;
void setup()
{
Serial.begin(9600);
delay(500);
}
void loop()
{
//depth conversion to separate ints
depthMeters_1 = depthMeters; //3
depthMeters_2 = (depthMeters * 10) - ((int)depthMeters * 10); //31-30 = 1
depthMeters_3 = (depthMeters * 100) - (int(depthMeters * 10)*10); //314-310 = 4
//compass heading conversion
compassHeading_1 = compassHeading; //3
compassHeading_2 = (compassHeading * 10) - ((int)compassHeading * 10); //31-30 = 1
memset(replySerial, 0, sizeof(replySerial));
byte startByte = '#';
byte endByte = '*';
//build a serial packet
Serial.write(startByte );
itoa(depthMeters_1, replySerial, 10); //depth
strcat(sendSerial, replySerial);
itoa(depthMeters_2, replySerial, 10);
strcat(sendSerial, replySerial);
itoa(depthMeters_3, replySerial, 10);
strcat(sendSerial, replySerial);
itoa(compassHeading_1, replySerial, 10); //compass
strcat(sendSerial, replySerial);
itoa(compassHeading_2, replySerial, 10);
strcat(sendSerial, replySerial);
itoa(verticalMotors, replySerial, 10); //verticals
strcat(sendSerial, replySerial);
itoa(metal, replySerial, 10); //metal detector
strcat(sendSerial, replySerial);
Serial.write(sendSerial );
Serial.println(endByte);
memset(sendSerial, 0, sizeof(sendSerial));
delay(100);
}
The FINAL receiving code is here:
//HUD receive
#include <OSD.h>
OSD osd;
byte HUD[390];
int x = 0;
char incomingData[100]; //to store incoming serial data
unsigned long last = 0;
unsigned long interval = 100;
int ledPin = 13;
int ledState = false;
void setup()
{
Serial.begin(9600);
osd.init();
pinMode(ledPin, OUTPUT);
for(x=0; x<390; x++);
{
HUD[x] = 0x00;
}
HUD[30] = 'A';
HUD[57] = 'B';
HUD[360] = 'D';
HUD[387] = 'C';
osd.display(HUD);
}
void loop()
{
unsigned long current = millis();
receiveSerial();
}
inline void receiveSerial()
{
char ch;
static int cpt=0;
static byte flag=false;
if (Serial.available()>0)
{
ch = Serial.read();
switch( ch )
{
case '#' :
{
cpt =0;
flag=false;
break;
}
case '*' :
{
flag=true;
break;
}
default:
{
incomingData[cpt++]=ch;
}
}
}
if (flag==true)
{
//Display the incoming datas, as you want
int depthMeters_1 = incomingData[0];
int depthMeters_2 = incomingData[1];
int depthMeters_3 = incomingData[2];
int compassHeading_1 = incomingData[3];
int compassHeading_2 = incomingData[4];
int compassHeading_3 = incomingData[5];
int compassHeading_4 = incomingData[6];
int motorValue_1 = incomingData[7];
int motorValue_2 = incomingData[8];
int motorValue_3 = incomingData[9];
int motorValue_4 = incomingData[10];
int metal = incomingData[11];
HUD[61] = depthMeters_1; //2
HUD[62] = depthMeters_2; //3
HUD[63] = '.';
HUD[64] = depthMeters_3; //5
//compass
HUD[91] = compassHeading_1; //2
HUD[92] = compassHeading_2; //0
HUD[93] = compassHeading_3; //0
HUD[94] = '.';
HUD[95] = compassHeading_4; //1
//verticals
HUD[121] = motorValue_1; //1
HUD[122] = motorValue_2; //5
HUD[123] = motorValue_3; //0
HUD[124] = motorValue_4; //
//metal
HUD[151] = metal;
}
// Serial.println(incomingData[4]);
//Serial.flush();
if(last - current > interval)
{
last = current;
osd.display(HUD);
}
Serial.println(incomingData);
}
Thanks!!!
NJS552
A simpler way to do it (IMO) is going with the EasyTransfer library from Bill Porter. All you have to do is define the variables that you want to send, set up the serial port(s) and bob is your uncle. I am using it right now to communicate between a 328P and a 1284P (Serial1). It just works, as long as you don't reverse the TX/RX leads, forget to add a resistor, or make any other number of mistakes like me! =(
system
December 31, 2011, 1:43am
6
Thanks!
Do you have a sketch that works I could base mine off of?
If you want to share of course...
Thanks,
NJS552
system
December 31, 2011, 2:15pm
7
Do you have a sketch that works I could base mine off of?
Did you look at EasyTransfer Arduino Library « The Mind of Bill Porter ? The library comes with examples.
Yep, the included examples are great - the library comes with everything you need to get started. Also, look at Bills pages on how to share the serial bus with other devices - i.e. How to Add Multiple Serial Connections to your Custom Arduino Board or What Those 1k Resistors Do « The Mind of Bill Porter . Very good info. The great thing about Bills library is not only that it simply works but that Bill is also extremely good about supporting it - post a question in his forum and it will get answered.