Today is...
Monday, August 19, 2019
Welcome to the Modbus Community, about
the world's leading automation protocol.
Querying a Modbus Slave with an Arduino
Having trouble trying to query a Modbus slave with an Arduino through RS485

Having some troubles trying to query a modbus slave with an Arduino through RS485. I've already succeeded in querying a software modbus slave running on my PC through the USB/COM port using the ModbusMaster library, hence it shouldn't be a software issue.

I read about TTL and level conversions, and I put on a circuit like this on a breadboard:
https://i.stack.imgur.com/Ff8Vo.png

Using the same firmware/sketch that worked for the software slave, I connected the arduino pin TX and RX to the max485, and A and B to the Modbus slave and I issued several requests.

I can see the signals converted by the MAX485(http://www.kynix.com/Detail/240679/MAX485.html) (CPA1114) though the oscilloscope and it seems to be right. The led on the Modbus slave lights on as it sees a modbus transaction. Still, what I read as result of the request is always 0xE0 (invalid slave id) or 0xE2 (timeout).

I queried the slave with the same equal request using another tool (a RS485/USB converter and CAS Modbus Scanner), and it gives the expected results, that is data 0x01.

This is the code I'm running on an Arduino Ethernet (with a display for debug purpose):


#include <ModbusMaster.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 4, 5, 6, 7);

ModbusMaster node(1);

void setup() {

pinMode(3, OUTPUT);
node.begin(19200);
lcd.begin(16, 2);

}

void loop() {

uint16_t m_startAddress=1;
uint8_t m_length=1;
uint8_t result;

digitalWrite(3, HIGH); // TX
result = node.readHoldingRegisters(m_startAddress, m_length);

lcd.clear();

if (result == node.ku8MBSuccess) {
lcd.print("DATA:");
digitalWrite(3, LOW); // RX
for (uint8_t j = 0; j < m_length; j++) lcd.print( node.getResponseBuffer(j), HEX );
} else {
lcd.print("ERR ");
lcd.print(result, HEX);
}

delay(500);

}


Arduino request signal:https://i.stack.imgur.com/xzL0s.png
USB/RS485 converter signal:https://i.stack.imgur.com/kqkRW.png
Overlap of the two signals:https://i.stack.imgur.com/ydwQd.png

#include <ModbusMaster.h>


#define MAX485_RE_NEG 9
#define MAX485_DE 7

//int myInts[6];

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


digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);


Serial.begin(9600);

node.begin(10, Serial);

node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
}

void loop()
{
//node.writeSingleRegister(40016,5);
// node.clearResponseBuffer();
float finalresult;
//unsigned long long int finalresult;
// uint32_t finalresulta;


// uint16_t result;
// uint16_t resulta;

finalresult = node.readInputRegisters(1,1);


if (finalresult == node.ku8MBSuccess)

{

Serial.print("\nPV0: ");
float dat=node.getResponseBuffer(0);
// uint64_t data1=node.getResponseBuffer(1);
Serial.println(dat);

// Serial.println(data1);
delay(1000);

}
else {
Serial.print("\nError ");

delay(1000);
}

delay(1000);
}