RS485/Modbus project, strange behaviour

Hi,
I am currently working on a project where some Nano´s should communicate over RS485 with the Modbus protocoll.

After a lot of try and error I now have a working setup but encountered some strange problems which I can´t figure out by myself. I would be glad if someone could help me out.

I am using some of these common RS485 converters and wired them up like in this example: https://arduino-info.wikispaces.com/SoftwareSerialRS485Example . (I hope the link is ok).

In the sketch I am using the SimpleModbus library.
My testing sketch:

#include <SimpleModbusSlave.h>
#define LED 13 //LED L on board

enum
{
ADC_VAL,
PWM_VAL,
LED_STATE,
HOLDING_REGS_SIZE
};

unsigned int holdingRegs[HOLDING_REGS_SIZE];

void setup()
{
modbus_configure(&Serial,9600, SERIAL_8N2, 1, 6, HOLDING_REGS_SIZE, holdingRegs);
pinMode(LED, OUTPUT);
}

void loop()
{
holdingRegs[ADC_VAL] = 10; //test values
holdingRegs[PWM_VAL] = 20; //test values

modbus_update();

if(holdingRegs[LED_STATE] >= 40 ){ //LED controlled over modbus register
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW); }
}

I tried to connect via a FTDI USB<->RS485 converter (these with the four jumpers), but I have absolute no succes. Either I get a Time Out or an invalid response/CRC error or Slave error. Tested with Mtester, qModbus and a simple python terminal connection.
(If someone have some experience with these converters and could help me make that thing work that would be great.)

Currently I use a work around with another nano which is connectet via USB and which sends the serial data via SoftSerial to some other pins.

The Sketch:
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);

// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
pinMode(6, OUTPUT); // TX enable pin
digitalWrite(6, LOW);
}

void loop() { // run over and over
if (mySerial.available()) {
Serial.write(mySerial.read());
}
if (Serial.available()) {
digitalWrite(6, HIGH);
//delay(3); //some time for the RS485 converter to get ready
mySerial.write(Serial.read());
Serial.flush(); //wait until everything is send
digitalWrite(6, LOW);
}
}

This method workes fine with Mtester, qModbus and the python terminal and leads me to the strange behaviour of the RS485 converters.

Like in the link above I had jumpered the RE and DE together and connectet them to the TX enable pin.
The problem is that when I do so the RX led on the Modbus slave nano will light up constantly and it is impossible to send or receive valid data.
When I connect the DI pin all time to +5V and keep the RE on the TX enable pin everything works fine.

What I figured out so far : As soon as i connect (manualy) the DI pin to 0V (with RE on 0V) the RX will light up (?) and furthermore sending of data is not possible(what makes sense). If i connect it to +5V it does not and the connection will work fine.

What confuses me because I thought that it would not be possible for another RS485 converter to transmit if some other one is enabled(DI).

Interesting is that on the Serial coverter nano it does not mater if i connect the RE and DE to the TX enable or if I connect the DE to +5V constantly.

I hope you understand what I try to explain and would be glad if someone has some ideas. I am sorry if I made some mistakes, english is not my native language but I try my best.

Regards
Timo

Please enclose code in code tags </>.

In half duplex mode the sender turns on its transmitter for sending data only, then off again to allow other stations to transmit over the shared line. I.e. normally all stations should be listening. I'm not sure what your library does, which pins it uses to select the transmission direction.

I'm not sure about the DE/RE signals. According to some example code both should be LOW to receive, and HIGH only while transmitting data. This is what your code does on pin 6, should be okay so far.

Also note that the SoftwareSerial library is blocking, what can cause timing problems in your code. AltSoftSerial is non-blocking, but also is restricted to low baudrates (9600 should work, though).

I'd suggest that you make the RS-485 communication work first, before using the Modbus protocol. Try a simple loop-back (echo) sketch, with your FTDI converter and Serial Monitor or some other terminal program.

Es gibt auch eine Deutsch Sektion im Forum, aber Dein Englisch ist ja nicht schlecht :slight_smile:

Hi,
thank you for your response.

Ok, i will try that the next time.

Yes, I understand it the same way and the library is using pin 6 for this purpose.
Like you mentioned and according to the MAX485 datasheet RE must be LOW for receive enable and DE must be HIGH for write enable.

I now have tried to connect two nano´s as slaves to the RS485 bus. The result is that for some reason one of them is ok to have DE and RE connected to the TXenable pin, while at the other one the DE have to be open(otherwise after some seconds the RX led of the nano goes on and no communication is possible over the RS485 line, error invalid CRC).

When I connect only one nano the DE pin has to be open/high for a working communication.

I have absolutely no idea what is causing this behaviour, maybe a faulty RS485 line termination / undefined state ?

Ok, I will try that library and the loop-back later, at least sometimes the RS485 line is working so for the moment my work around will be fine.

Ja das habe ich gesehen, ich dachte mir nur das man im englisch sprachigen Raum vielleicht mehr Rückmeldungen erhält.

Regards
Timo