I've got an uno and a mega hooked up to two MAX485 chips connected across an ethernet cable. I've been able to get 1-way communication to work in either direction but when I try to implement 2-way communication, the mega isn't receiving any information. This is probably irrelevant but I've tried both SoftwareSerial and hardware serial.
Here is some test code I've mostly copied from internet snippets:
#include <SoftwareSerial.h>
#define SSerialRX 2 //Serial Receive pin - RO
#define SSerialTX 7 //Serial Transmit pin - DI
#define SSerialTxControl 4 //RS485 Direction control - RE/DE
#define RS485Transmit HIGH
#define RS485Receive LOW
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX
void setup()
{
pinMode(SSerialTxControl, OUTPUT);
digitalWrite(SSerialTxControl, RS485Receive);
Serial.begin(9600);
RS485Serial.begin(9600);
}
void loop()
{
if (Serial.available()) //look for data from pc
{
digitalWrite(SSerialTxControl, RS485Transmit);
RS485Serial.write(Serial.read());
delay(10);
digitalWrite(SSerialTxControl, RS485Receive);
}
if (RS485Serial.available()) //Look for data from other Arduino
{
Serial.write(RS485Serial.read()); // Show on Serial Monitor
}
}