send data by serial

Hi there i try to send an recive data by tx an rx.

On the first board i send data by Serial.print (CODE: Serial.print(123, DEC)), at the other board i want to read this data by Serial.read();. so far it works i recive data, but it is not the value i aspekted. At the Serial Monitor at the first screen arduino printed "123", but at the Monitor of the second board i get a totaly diffrent value.

Also if i try to send an read a binary, but value there are no similar values in the string.

Can anybody help me?

PS: Sorry for my english. :slight_smile:

Can you post your code?

Serial.print(123, DEC) sends three bytes of serial data: the characters '1', '2', '3' (e.g. 49, 50, 51). To receive the value on the other board, you'll need to collect these bytes and reassemble them into a value. E.g. each time you get a byte, you want to do something like:

int c = Serial.read(); 
int digit = c - '0';  // '0' == 48: the ASCII value of the digit 0
value = value * 10 + d;

You'll need to send some sort of terminating character (like a newline) and check for that on the receiving end.

Alternatively, you can just send binary bytes (remember a byte only goes from 0-255) or multiple bytes, but then you won't be able to read the data in the serial monitor.

Ok, i undersand, thanks so far. And i could encode the data. But at least i have one error. If i start to send just one byte nothing happend. To start the communication i had to send 2 or 3 bytes in one Serial.print();. When ist is runnig i can refresh my sending script to one byte only. :-?

Is there any reason why i had to do this, it is a not so good if i had to change the input to start the conversation. Any meanings?

That's strange. Can you post the code that has problems? Do you have trouble reading the data with the computer or another Arduino board?

So i watch this problem during my test and seems it alway needs be pushed. After this it works fine.

TRANSMITTER CODE:

void setup() {
// open the serial port at 9600 bps:
Serial.begin(9600);
}

void loop() {
// print it out
Serial.print(9215, DEC); // print as an ASCII-encoded decimal
delay(70);
}

REVEIVER CODE:

int incomingByte = 0; // for incoming serial data
int empfaenger = 0;
int sender = 0;
int eigenschaft = 0;
int zaehler = 0;

void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {

incomingByte = Serial.read();
Serial.print(incomingByte);

}

So if i start this two Codes on diffrent boards nothing will happend. i have to chang my value (9215) in 215. When this script is started, the other board begins to read data.

But it is a little bit strange, because if the transmitter-board send or not, the receiver board must print something, for example -1. If i cut the connetion the receiver-boards print -1, but just after i give him a little kick.

Any ideas?

Hmm, strange, I'll try to take a look (when I get a moment of free time from my thesis).