Go Down

Topic: Trouble Reading InputRegisters From Arduino Modbus Server (Read 101 times) previous topic - next topic

ryangrg

I am using the Arduino Modbus Library on a Nano set-up as a modbus server and am unable to read or write to input registers but can read and write to coils. I have my computer set-up as the Client and I'm using python with the minimalmodbus library. I'm also using this USB to RS485 device and this RS485/TTL device on the arduino Nano end. The code on the Nano is as follows:
Code: [Select]
#include <ArduinoRS485.h>
#include <ArduinoModbus.h>


void setup() {
  Serial.begin(9600);
  ModbusRTUServer.begin(1, 9600);
  
  ModbusRTUServer.configureCoils(0x00, 3);
  ModbusRTUServer.coilWrite(0,1);
  ModbusRTUServer.coilWrite(1,1);
  ModbusRTUServer.coilWrite(2,0);
  
  ModbusRTUServer.configureInputRegisters(0x00, 3);
  ModbusRTUServer.inputRegisterWrite(0, 10);
  ModbusRTUServer.inputRegisterWrite(1, 104);
  ModbusRTUServer.inputRegisterWrite(2, 57759);
}

void loop() {
  ModbusRTUServer.poll();
    if (ModbusRTUServer.coilRead(0)) {
    for (size_t i = 1; i <= 3; i++) {
      digitalWrite(LED_BUILTIN, HIGH);
      delay(500);
      digitalWrite(LED_BUILTIN, LOW);
      delay(500);
    }//for
  ModbusRTUServer.coilWrite(0,0);
  }//if
}

If coil 0 is 1 the built in LED will flash 3 times and then the coil gets set back to 0. I pretty much followed the Arduino Modbusy Library examples to write the coils and registers. I'm a little confused as to why the coils and the input registers start with 0x00? Should the input register start at 40000 and that should be in hex? Clarification on what's happening here would be really appreciated. Trying to figure things out by going through the code, but clearly haven't figured things out yet.

In python I'm using the following to code to make the light blink and read from one of the coils and it works fine.
Code: [Select]
import minimalmodbus

devise0 = minimalmodbus.Instrument('/dev/cu.usbserial-AR0K3VZ7', 1)
devise0.serial.baudrate = 9600
n = devise0.read_bit(1,1)
devise0.write_bit(0,1)  
print(n)


But when I run this line of code in python:
Code: [Select]
m = devise0.read_register(0,1)
print(m)

I get the following error:
Code: [Select]
IllegalRequestError: Slave reported illegal data address
Again, any help would be greatly appreciated.

-RG

ryangrg

I got this to work by changing these lines in the arduino code:
Code: [Select]
  ModbusRTUServer.configureInputRegisters(0x00, 3);[color=#222222][/color]
  ModbusRTUServer.inputRegisterWrite(0, 10);[color=#222222][/color]
  ModbusRTUServer.inputRegisterWrite(1, 104);[color=#222222][/color]
  ModbusRTUServer.inputRegisterWrite(2, 57759);

To:
Code: [Select]
  ModbusRTUServer.configureHoldingRegisters(0x00, 3);[color=#222222][/color]
  ModbusRTUServer.holdingRegisterWrite(0, 10);[color=#222222][/color]
  ModbusRTUServer.holdingRegisterWrite(1, 104);[color=#222222][/color]
  ModbusRTUServer.holdingRegisterWrite(2, 57759);

I had input registers when I should have had holding registers. Also I guess both ArduinoModbus and Minimal Modbus handle the addressing such you don't need to enter an address of 40001. The libraries handle that for you I suppose. I figure when I made the read_register call in python it was addressing the 40000 block and I had input register set on my arduino modbus server that belong to 30000 block of addresses, hence I couldn't communicate.

Hope this helps someone.

Go Up