Receive multiple messages from one serial port

I suppose to use the android app ( created by MIT app inventor) to send message to Arduino board.
This is Arduino mega 2560 and serial2 is bluetooth. Serial 3 is first led Matrix Display, serial 1 is second led Matrix Display.
First, I will select the LED from the app, and send text to serial port 2 and it can recognize which port to write.(either serial 1 or serial 3)
My problem currently is I don't really know how to select the serial port 3 or 1 from the app so now both LED will display the same message. Can anybody help me check my code?

This is my code.

void setup()
{
Serial3.begin(9600); // First LED
Serial2.begin(9600);// bluetooth
Serial1.begin(9600); //Second LED
delay(500);
// set scrolling speed Serial1.print(0xF9);
}

void loop()
{
char message[100];
readSerial(message);
Serial1.flush();
Serial1.write( '!');
Serial1.write(message); // Send string to display
Serial1.write( '\r');
delay(500);
Serial1.write(0xFC);

char message3[100];
readSerial(message3);
Serial3.flush();
Serial3.write( '!');
Serial3.write(message3); // Send string to display
Serial3.write( '\r');
delay(500);
Serial3.write(0xFC);
}
}
}

int readSerial(char result[])
{
int i = 0;
while(1)
{
while (Serial2.available() > 0)
{
char inChar = Serial2.read();
if (inChar == '\n')
{
result = '\0';

  • return 0;*
  • }*
  • if(inChar!='\r')*
  • {*
    _ result = inChar;_
    * i++;*
    * }*
    * }*
    * }*
    }

How is your Arduino supposed to know which message is for which LED matrix?

At the moment you seem to have code that sends the first message to one matrix and the second message to the other matrix and repeats this over and over.

What happens if no new message is received?

I think you need a different approach.

You should include with the message something that tells the Arduino which matrix the message is for. Maybe prefix the message with a character 1 or 2. Then your code could check the first character of the message to decide where to send it.

This also means you only need one input buffer for all messages.

You also need code that does nothing (?) if no message is received.

...R

Ya, that's the problem, the first message I sent will go to the first LED, second message will go to the second LED.
if I prefix the message with the character, how can I take out the character before the message display.
Btw, I don't get it what's ur meant by I need one input buffer for all messages.

This is the LED that I bought, it is a led moving message display. I thought if no message is received, the void loop will keep looping the message that I stored in the char message?

shenyuchris:
how can I take out the character before the message display.

Just start the display from message[1] onwards - assuming the number is in message[0]

Btw, I don't get it what's ur meant by I need one input buffer for all messages.
LED Moving Message Display 362x72mm [1113] : Sunrom Electronics
This is the LED that I bought, it is a led moving message display. I thought if no message is received, the void loop will keep looping the message that I stored in the char message?

I was thinking of the buffer where the received message would go rather than the buffer in which the messae being scrolled would be stored. I wasn't thinking of needing to store the message while it is being scrolled - I thought it was just sent to the matrix once.

Another way (which I am less fond of) is to check the value of the first character as it is received and then save the remaining characters in the appropriate buffer. That probably also gets round the problem of not displaying the number.

...R

void setup()
{
Serial3.begin(9600); // First LED
Serial2.begin(9600);// bluetooth
Serial1.begin(9600); //Second LED
delay(500);
// set scrolling speed Serial1.print(0xF9);
}

void loop()
{
int c = 0;
char message[100];
readSerial(message,c);

if ( c = 1 )
{
Serial1.flush();
Serial1.write( '!');
Serial1.write(message); // Send string to display
Serial1.write( '\r');
delay(500);
Serial1.write(0xFC);
c = 0;
}

else
{
Serial3.flush();
Serial3.write( '!');
Serial3.write(message); // Send string to display
Serial3.write( '\r');
delay(500);
Serial3.write(0xFC);
}

}

int readSerial(char result[],int c)
{
int i = 0;
while(1)
{
while (Serial2.available() > 0)
{
char inChar = Serial2.read();
if (inChar == '\n')
{
result = '\0';

  • return 0;*
  • }*
  • if(inChar!='\r')*
  • {*
    _ result = inChar;_
    _ if ( result = 'A' )
    * {
    c = 1 ;
    return c;
    }
    i++;
    }
    }
    }
    }*_

@shenyuchris, you have made another post without any question.

May I assume that your project is now working?

PLEASE put your code in code tags so it looks like this and is easier to read.

...R

opps sorry, I'm new to here. The code still not working. Now it only display the first character(the prefix)

shenyuchris:
opps sorry, I'm new to here. The code still not working. Now it only display the first character(the prefix)

The code in readSerial() seems only to read a single character ... maybe that is where the problem lies?

The Arduino code in this demo illustrates how I would gather serial data.

And, you have not yet modified your earlier post to put the code in code tags.

...R