I don't know what is wrong with your setup, but I suggest you start by
testing with a simpler program.
Below is a program that simply copies bytes from the USB link (the bytes
coming from the Arduino's serial monitor) to the RS-485 bus, or the
other way around depending on a #define
. Both Arduinos should be
connected as follows:
Arduino transceiver
--------------------------------
GND GND
5V VCC
TX1 (18) DI (driver input)
RX1 (19) RO (receiver output)
Additionally, connect:
- on the sending side:
- the USB plug to your computer
- the transceiver's DE (driver output enable) and RE (receiver output
enable) to the Arduino 5V
- on the receiving side:
- the transceiver's DE and RE to the Arduino GND
Make sure both Arduinos are powered and share a common ground.
Optionally, you can connect the receiving Arduino to your computer, if
you can manage to tell the USB links apart.
Then load the program below to both of them. Make sure you comment-out
the line #define SENDER
before programming the receiver.
// The "sender" copies bytes from the USB link (Serial) to the RS-485
// bus (Serial1). The receiver copies them the other way around.
// Comment-out this line for the receiver.
#define SENDER
#ifdef SENDER
HardwareSerial &inputStream = Serial;
HardwareSerial &outputStream = Serial1;
#else
HardwareSerial &inputStream = Serial1;
HardwareSerial &outputStream = Serial;
#endif
void setup()
{
inputStream.begin(9600);
outputStream.begin(9600);
pinMode(LED_BUILTIN, OUTPUT); // used to show activity
}
void loop()
{
// Copy input stream to output stream.
if (inputStream.available()) {
digitalWrite(LED_BUILTIN, HIGH);
outputStream.write(inputStream.read());
outputStream.flush();
digitalWrite(LED_BUILTIN, LOW);
}
}
Now you can test sending strings through the serial monitor and
seeing whether both LEDs blink. If you connect both Arduinos to your
computer, you could use a terminal emulator (screen, putty, minicom...
depending on your OS) to get the characters back from the receiving
Arduino.
If all that fails, get a scope and look at all the signals along the
path.