Pages: [1]   Go Down
Author Topic: Serial communication between two Arduino Uno's  (Read 1592 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 - downloaded 27 times.)
* HUD_RECIEVE_TEST.h (1.16 KB - downloaded 24 times.)
Logged

France
Offline Offline
Sr. Member
****
Karma: 2
Posts: 380
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 :
Code:
for (int i=0; i<=Serial.available(); i++)
is not the right way of doing that.

I prefer :

Code:

int i=0;

while (Serial.available()>0)
 incomingdata[i++]=Serial.read();


Considering the code with the '#' and '*' delimiters you could write something like that :

Code:

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
  }
}

Logged

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

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
Logged

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

Nevermind.

I put the # and * designators in bytes and sent them before and after the main string.

The FINAL sending code is here:
Code:
//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:
Code:
//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
Logged

Peoples Republic of Cantabrigia
Offline Offline
God Member
*****
Karma: 6
Posts: 722
Arduino happiness
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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!  smiley-cry
Logged

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

Thanks!

Do you have a sketch that works I could base mine off of?

If you want to share of course...

Thanks,
NJS552
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49270
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Do you have a sketch that works I could base mine off of?
Did you look at http://www.billporter.info/easytransfer-arduino-library/? The library comes with examples.
Logged

Peoples Republic of Cantabrigia
Offline Offline
God Member
*****
Karma: 6
Posts: 722
Arduino happiness
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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. http://www.billporter.info/how-to-add-multiple-uart-connections/. 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.
Logged

Pages: [1]   Go Up
Jump to: