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