Serial communication between two arduino Using TX and RX

Hi there I was wondering if anyone could help me or point in the right direction. I am trying to connect two Arduinos together using the TX and RX using the start/stop protocol. I'm fairly new to the Arduino and I don't know if I'm heading in the right direction.

I know two bits of code are needed for the two Arduinos one transmitter and one receiver and need to be connected to each other properly.

This is the code I have so far for my transmitter:

int rx = 0;
int tx = 1;
int x = 0; 

void setup() {
  Serial.begin (9600, SERIAL_8N2);
  pinMode (rx, OUTPUT);
  pinMode (tx, INPUT);
}

void loop() {
  

if(Serial.available() > 0) {

x = Serial.read();

Serial.println ("I received");
Serial.println (x,BIN);

}
}

The code above prints out a binary number with extras ones at the front.

1: I was wondering what is making my code like this?

2: How would I go about doing the start/stop protocol would serial.read and serial.write be a step in the right direct?

The boards I am using are two Arduino Uno.

Any tip's and help is very much appreciated :slight_smile:

The code above prints out a binary number with extras ones at the front.

You have declared x to be an integer, which means 16 bits.

serial.print(x,BIN) will interpret all 16 bits. If the most significant bit of the 8 bit character returned from serial.read() is set, the character is probably being interpreted as a negative value of x, which will have the top 8 bits set as well.

Solution: declare "x" as char.

Have a look at the examples in Serial Input Basics. Simple reliable ways to receive data. The 3rd example will be most reliable. Get your sending Arduino to match what is required by the receiving device.

I don't understand what you have in mind when you say "the start/stop protocol" - what problem are you trying to address?

...R

I've done it. Take a look A Simple ARDUINO Serial Communication ( DUE & UNO ) - YouTube
pin position Serial - Arduino Reference
maybe helpful.