Hi all. I have a project i am working on that requires me to send data between two arduinos so i am trying to use the rs485 modules and the extra hardware serials available on the mega.
I have found that it works perfectly when i uses it with software serial but when i change the code to work with hardware serial it doesn't work.
you need to wait for the RS485 serial transmission to complete (all bits transmitted) before switching the DE/RE lines low, e.g.
void loop() {
if (Serial1.available())
{
Serial.write(Serial1.read());
}
if (Serial.available()){
digitalWrite(RS_DE_RE,HIGH);
Serial1.write(Serial.read());
Serial1.flush(); // <<<<< wait for byte to be transmitted
digitalWrite(RS_DE_RE,LOW);
}
}
the Serial.flush() documentation states Waits for the transmission of outgoing serial data to complete. (Prior to Arduino 1.0, this instead removed any buffered incoming serial data.)
found when using RS485 shields this is required before switching the transmitter off and receiver on
I now use this RS485 shield which does not have the DE/RE pins but handles the switching of transmit/receive on board