RS485 Serial communication (SOLVED)

I am sending and receiving data to a Modbus slave using main Serial port of Arduino uno through TTL RS 485 converters.

The data is successfully received by the Slave and returns a reply.
The reply reaches the RX pin (D0) but the Serial.available() is always Zero. Serial.Read() returns only Zeros.

Simultaneously connected the Rx pin to another TTL to USB converter and monitored the data and it shows correct data.
What can be the mistake.
Durgaidass

You drive the Receiver Output Enable (pin 2) AND Driver Output Enable (pin 3) correct? To send both high and to receive both low.

In a half-duplex communication a sender has to be selected and allowed to put its output on the bus. AFAIK a Modbus slave will turn on its output driver in response of a message to its Modbus address. For that time you have to disable your RS-485 line driver and enable the receiver.

Hi DrDiettric, Thanks for the response.
RS485 is working OK. Because the desired output goes to the Modbus simulator and the response is reaching the RX (D0) Pin of UNO.
But the Serial.available() is always ZERO.

It's not good practice to use the Uno UART (SerialMonitor) for comms with peripherals.
Use SoftwareSerial for that.

  result = node.readCoils(1, 16);
        //  delay(100);
  
    ASerial.print(Serial.available());
    ASerial.print(result); 
    /////////////////////////////////////////////////////
    if (result == node.ku8MBSuccess)
    {

      for (j = 0; j <= 6; j++)
      {
        
       // Serial.flush();
        if (Serial.available()) {
          //delay(1);
          rec_buf[j] = Serial.read();
          ASerial.println( Serial.read());
        }

              }
     // ASerial.write((char)rec_buf[6]);
              ASerial.write('K');
    }
  

Hi noiasca
In this ASerial.print(result); prints the correct response.
When I connect the Rx Pin(D0) of UNO to a serial monitor through TTL -RS485-USB in a PC, the desired data is shown. But in my code the Serial.read() buf is always ZERO as if Receiver is disabled.
Thanks

Hi

I have tried both.
I use AltSoftSerial.h for software serial.
But the end result is same.
Thanks.

You have yet to "post your code".

1 Like

Hi @durgaidass ,
Welcome to the forum..

ouch, you read twice on each loop..

replace with..

 for (j = 0; j <= 6; j++)
  {
    // Serial.flush();
    if (Serial.available()) {
      //delay(1);
      rec_buf[j] = Serial.read();
      ASerial.println( rec_buf[j]);
    }
  }

good luck.. ~q

Hi
When in actual code code I will use only the rec_buf[j] only. Now for testing purpose I used direct print. with this it should give correct response at least for 3 itterartions.
Thanks

ok, think i see your issue..
be nice to see the whole sketch..
thinking you got the Serial object linked to a modbus object (node)..
not sure what modbus lib your using but you get the response from the node not the attached Serial object..
something like..

node.getResponseBuffer(0);

but again, this really depends on the lib used..
but yeah, the Serial object should always be emptied by the modbus object..

maybe.. ~q

Post you're whole code. Especially the code where the RS485 enable pins are set or released.

An schematic is a good info for lots of forum users.

2 Likes

Hi Qubits-us Thanks for the information.
Actually that was the mistake I was doing.
Since the expected reply was reaching the Rx PIN, I was trying to read the Serial object.
Now I am getting the correct information from node object.

Thank you once again
V Durgaidass

1 Like

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