Connecting 2 arduino megas Viera rs485

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.

Here is a photo of my connections

here are my codes

master code

// master node

#define RS_DE_RE 12



void setup () {
  Serial.begin(9600);
  Serial2.begin(9600);
  pinMode(RS_DE_RE, OUTPUT);
  // master need to be high to transment
  digitalWrite(RS_DE_RE, LOW);
  Serial.println("master node is ready.....");
}

void loop () {
  if (Serial.available()){
    digitalWrite(RS_DE_RE, HIGH);
    Serial2.write(Serial.read());
    digitalWrite(RS_DE_RE, LOW);
  } 
  if (Serial2.available()){
    Serial.write(Serial2.read());
  }
}

slave

// slave node


#define RS_DE_RE 12



void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  pinMode(RS_DE_RE, OUTPUT);
  // enabeal pin low to actvate reseve fucion
  digitalWrite(RS_DE_RE,LOW);
  Serial.println("slave node is ready");
}

void loop() {
  if (Serial1.available())
  {
    Serial.write(Serial1.read());
  }
 if (Serial.available()){
    digitalWrite(RS_DE_RE,HIGH);
    Serial1.write(Serial.read());
    digitalWrite(RS_DE_RE,LOW);
  }
}

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);
  }
}

AFAIR this does not help, it also only waits for an empty buffer.

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

That's not a clear statement. I'm waiting for a report on the actual functionality.

when programming RS485 on PIC24 micros I remember waiting for the Tx buffer to empty, e.g.

// RS485 transmit character
void RS485_UART_Write(uint8_t writeChar){
     RS_485_Tx_enable_SetHigh();    // enable transmit
     RS_485_Rx_enable_SetHigh();    // disable receive
     while(RS485_UART_Read() != -1); // clear receiver
     H_RS485_UART_Write(writeChar);   // transmit character
     while(!H_RS485_IsTxDone);        // <<<<<<<<<<<<< wait for Tx done
    // while(!H_RS485_UART_is_tx_done());     // wait until data is transmitted
     RS_485_Tx_enable_SetLow();      // disable transmitter
     RS_485_Rx_enable_SetLow();      // enable receive
}

I assumed from the documentation Serial.flush() did the same thing (I know the method name does not make sense)

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.