Hi guys,
I'm trying to communicate with a modbus device but I'm having problems, something strange is happening.
In the first construction of the project, communication worked normally. When I went to build other units, communication didn't work anymore. After testing and verifying everything, I see that the MAX485 module was different from the first one. One using MAX485 ESA CI (OK) and the other with MAX485 CSA CI (NOK). Looking at the document, the only difference is about temperature tolerance, but here in my lab I have no problem with that.
As I'm having difficulties to find to buy MAX485 with ESA chip, I decided to perform new tests on the module with CSA chip. To my surprise, I found a way for the module to work, but I can't leave it that way, and I couldn't understand why it worked either.
Strangely the communication works when I parallel the MAX485 with an RS485 USB converter. This USB converter is connected to the computer but it is not doing any kind of communication, just connected. If disconnected, Arduino stops communicating with the device (Result = 224). If connect again, communication returns.
In the image the yellow and black cables are connected to the device.
Brown and gray interconnect the MAX485 with the RS485 USB converter. A-A, B-B.
Does anyone have any idea what might be going on?
I am using an Arduino MEGA 2560.
Follow the code below.
Thank you!
#include <ModbusMaster.h>
#define MAX485_DE 2
#define MAX485_RE_NEG 3
// instantiate ModbusMaster object
ModbusMaster node;
void preTransmission()
{
digitalWrite(MAX485_RE_NEG, 1);
digitalWrite(MAX485_DE, 1);
}
void postTransmission()
{
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
}
void setup()
{
pinMode(MAX485_RE_NEG, OUTPUT);
pinMode(MAX485_DE, OUTPUT);
// Init in receive mode
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
Serial.begin(9600);
Serial1.begin(9600);
// Modbus slave ID 1
node.begin(1, Serial1);
// Callbacks allow us to configure the RS485 transceiver correctly
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
}
void loop()
{
uint8_t result;
result = node.readInputRegisters(4121, 1);
if (result == node.ku8MBSuccess)
{
Serial.print(F("Total Flow: "));
Serial.println(node.getResponseBuffer(0x00));
}
else
{
Serial.print(F("Connect Fail. Result = "));
Serial.println(result);
}
delay(1000);
}