I am stuck at a strange problem. My MEGA is connected to my UNO using serial connection.
TX connected to RX.
From MEGA I transfer a string to UNO.
But UNO prints it with NEWLINE after every character.
This is my code for UNO
// Number max of characters in your message
#define MAX_CHAR 30
// Variables for bluetooth message
char message[MAX_CHAR]; // stores you message // = "S|R149|W3|O19.1|T299|A220";
byte index;
void loadMessageWithNull()
{
//Serial.println(F("Resetting message array with NULL"));
for( index=0; index<MAX_CHAR; index++ )
{
message[index] = '\0';
}
}
void setup()
{
Serial.begin(9600);
Serial.println("Serial Monitor Connected"); // Is not an integer so it won't interfere
}
void loop()
{
// Check if serial is avaible and before reading a new message delete's the old message
if( Serial.available() )
{
// Load message array with NULL data
loadMessageWithNull();
index = 0;
} // if ends here //
while ( Serial.available() > 0 )
{
// The message can have up to 100 characters
if( index < (MAX_CHAR-1) )
{
char inChar = Serial.read(); // Reads a character
[color=red]if ( inChar != '\n' && inChar != '\r' )[/color]
{
message[index] = inChar; // Stores the character in message array
index++; // Increment position
message[index] = '\0'; // Delete the last position
}
//Serial.print(inChar);
}
}
Serial.print(message);
}
My output looks like this:
S
|
R
1
9
0
|
W
5
|
O
3
1
.
0
|
T
0
|
A
2
4
|
0
Can you please tell me what is going wrong?
My MEGA sends string simply by following code:
Serial1.begin(9600);
Serial1.print("STA");
BUT it is received as
S
T
A
Please advise.
I am clueless. I even tried to remove the \n or \r
BulldogLowell:
the while loop exits because the arduino is faster than your 9600 baud rate... there isn't a char yet to read.
I used delay(10); also with no help.
while ( Serial.available() > 0 )
{
// The message can have up to 100 characters
if( index < (MAX_CHAR-1) )
{
char inChar = Serial.read(); // Reads a character
if ( inChar != '\n' && inChar != '\r' )
{
message[index] = inChar; // Stores the character in message array
index++; // Increment position
message[index] = '\0'; // Delete the last position
}
delay(10);
}
}
Nonsense. It was as-best a bandaid. The problem still exists. You need to send data with delimiters, so you know when a complete packet has been received.
statbat:
I am clueless. I even tried to remove the \n or \r
Serial is providing point-to-point connections between TWO serial devices only.
You CANNOT establish multi-point-to-point connections between many serial devices.
On your UNO the two devices already on the board are:
Atmega328 Pin-0/1
cabled with
USB-Serial adapter Atmega16U2
On your MEGA the two devices are:
Atmega2560 Pin-0/1
cabled with
USB-Serial adapter Atmega16U2
As soon as you call "Serial.begin()" in your sketch it is NOT ALLOWED to use pin-0/1 for something different than for the serial connection to your PC. I.e. you are not allowed to connect any other serial device to pin-0/1 of that board.
If you want to establish serial connections between two Arduino boards, you need boards which have MORE THAN ONE serial port.
The MEGA has 4 of them, so no problem. On a MEGA2560 you can use the RX1/TX1 for serial connection to another board (or RX2/TX2 or RX3/TX3).
But the UNO has only one serial port, so you need another serial port, emulated in Software, to connect your UNO to another Arduino board.
So use SoftwareSerial or the AltSoftSerial library to create an additional (virtual, software-emulated) serial port on your UNO at DIFFERENT pins than pin-0/1. Then you can connect the software-emulated serial port of the UNO with the physical Serial1 port of the MEGA and transfer data between the boards.