Deciphering serial data in an arduino mega

Hello Guys I would like a bit of help if possible? I am sending from an uno board serial information to a mega 2560 board using a two ciseco srf shields, when the mega receives the info I would like it to read the first byte of information then decide which serial output to use eg Serial1/Serial2 or Serial3. I am outputting the different serial data to three separate displays from the mega. I had this working on two uno's but only sending data to one display, but I would like to send it to three displays. here's my code so far.

int clearvar = 0; char stringa[20]; int i = 0; int transmit = 8; int line = 0;

void setup() { Serial1.begin(9600); pinMode(transmit, OUTPUT); digitalWrite(transmit, HIGH); loop(); }

void line1(){ Serial1.println(stringa); loop(); }

void line2(){ Serial2.println(stringa); loop(); }

void line3(){ Serial3.println(stringa); loop(); }

void loop() { // Read line from Serial line = 0; // set line to zero memset(stringa, 0, sizeof(stringa)); // set string contents to zero Serial1.setTimeout(500); // second input timeout line = Serial1.read() ; i = Serial1.readBytesUntil('\n', stringa, sizeof(stringa)-1); // Read line of input if(i > 0) { // Display line if anything was read if (line = 1){ line1(); } if (line = 2){ line2(); } if (line = 3){ line3(); } } }

The part of the program that im using to send the data is,

Serial.write(1); Serial.println("Hello"); or Serial.write(2); Serial.println("hiya") ; or Serial.write(3); Serial.println("Howdy");

in which I hoped to receive the different messages on different displays. any help would be greatly appreciated. Thanks Gav.

Hello,

You forgot to call Serial2/3.begin

Instead of repeating functions, you could do something like that:

HardwareSerial * serialPorts[] = { &Serial, &Serial1, &Serial2, &Serial3 };

...

if ( line >= 1 && line <= 3 )
{
  serialPorts[ line ] -> println( stringa );
}

By the way, your tests “if ( line = X )” are wrong. Doing this, you assign value X to line. For comparison, use ==.

readBytesUntil() is a blocking function. The examples in serial input basics don't block. They will save all of the received data into a char array which you can then analyse and pass on as required.

There is also a parse example that can be used to divide up the received data.

...R

if (line = 1){

This assigns the value of 1 to line and then tests if it's true. (1 is interpreted as false).

I think you meant to use the == operator.

line = Serial1.read() ;

You never test if there's data available to be read. This will return -1 a lot of the time. Make it wait for serial data to be available before reading line.

The use of readBytesUntil() looks OK to me, so long as you don't have any other tasks that you want the Arduino to do while you're waiting 500ms for the complete line.

Calling loop() from a function that was called from loop() is stupid. When the function ends, control will return to the function that called it. That being loop(). THAT is what should happen.

Hi Guys, just to let you know that with all your help I got it working. I was having a problem with the mega and the srf shield. So I used an uno with softwareserial to gain my extra ports and simplified the program as follows.

int clearvar = 0;
char stringa[20];
int i = 0;
int transmit = 8;
int line = 0;
#include <SoftwareSerial.h>
SoftwareSerial line1Serial(10,11);
SoftwareSerial line2Serial(5,6);
void setup() {
Serial.begin(9600);
line1Serial.begin(9600);
line2Serial.begin(9600);
pinMode(transmit, OUTPUT);
digitalWrite(transmit, HIGH);
}
void loop() {
if (Serial.available() >0){
int line = Serial.read();
Serial.println(line);
memset(stringa, 0, sizeof(stringa)); // set string contents to zero
Serial.setTimeout(500); // second input timeout
i = Serial.readBytesUntil(’\n’, stringa, sizeof(stringa)-1); // Read line of input
if(i > 0) { // Display line if anything was read
if (line == 1){
Serial.println(“on line 1”);
Serial.println(stringa);
line1Serial.println(stringa);
}
if (line == 3){
Serial.println(“on line 2”);
Serial.println(stringa);
line2Serial.println(stringa);
}
}
}
}

To send I used

Serial.write(1); // to write on display 1
Serial.print(temp1);

or
Serial.write(3); // to write on display 2
Serial.print(temp2);

Thanks again for all your help.