I'm trying to make two arduino mega2560 communicate between them by using Serial and Serial1 in this way:
Arduino1:
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
Serial1.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
Serial1.println("hello world");
delay(500);
}
and Arduino 2:
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
Serial1.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial1.available() > 0) {
Serial.print("I received: ");
Serial.println(Serial1.read());
}
else {
Serial.println("Nothing received at all!");
}
delay(500);
}
The problem is that I only receive this:
I received: 104
I received: 101
I received: 108
I received: 108
I received: 111
I received: 32
I received: 119
I received: 111
I received: 114
I received: 108
I received: 100
I received: 13
I received: 10
which I guess it is the conversion from string to DEC for "hello world\n". Is it correct?
What should i do in order to prints the ASCII string?
I can receive the data in the second board, but they are not in ASCII, but in DEC.
This happens only when i use the keyboard to send the strings to the second board. If I hard code the value of the string, then I'm able to receive the full string in ASCII on the second board.
For example, if I send the data in this way:
Serial1.println("hello world");
then I receive it in ASCII on the second board while if I send the same string by typing it in the serial monitor as
A string is printed as such, but Serial.read() returns single characters that should be passed on by Serial.write() without translation of the character codes into decimal numbers.
Yes, it works!
Obviously, it reads just a single char, so now I'm going to use a buffer to store more characters and prints the final string all in once.