Modbus-Master-Slave-for-Arduino library, to connect Openhab ModBus 2.3 binding

Good day!

I am new to arduino and I need a little bit of help. I have arduino UNO. I am using Modbus-Master-Slave-for-Arduino library to make my arduino slave using simple slave example code:

/**
 *  Modbus slave example 1:
 *  The purpose of this example is to link a data array
 *  from the Arduino to an external device.
 *
 *  Recommended Modbus Master: QModbus
 *  http://qmodbus.sourceforge.net/
 */

#include <ModbusRtu.h>

// data array for modbus network sharing
uint16_t au16data[16] = {
  3, 1415, 9265, 4, 2, 7182, 28182, 8, 0, 0, 0, 0, 0, 0, 1, -1 };

/**
 *  Modbus object declaration
 *  u8id : node id = 0 for master, = 1..247 for slave
 *  u8serno : serial port (use 0 for Serial)
 *  u8txenpin : 0 for RS-232 and USB-FTDI 
 *               or any pin number > 1 for RS-485
 */
Modbus slave(1,0,0); // this is slave @1 and RS-232 or USB-FTDI

void setup() {
  slave.begin( 9600 ); // baud-rate at 9600
}

void loop() {
  slave.poll( au16data, 16 );
}

When I set up OpenHab connection with ModBus2.3 binding it just does not connect. But I am able to connect to it using master simulation software. When trying to connect using openhab my arduino uno led is blinking periodically. Is there something wrong with the library? which is the best library for adruino ModBus slave?
Thank you!

When I set up OpenHab connection with ModBus2.3 binding it just does not connect.

In this case I strongly suspect your OpenHab configuration to be wrong. There is a small chance that the hardware connection to the Arduino is wrong but the probability is on the configuration side.

Is there something wrong with the library?

Might be but judging from your description I would not debug the library first. There are dozens of configuration parameters in the OpenHab module, if you got one wrong it might not work. Because you didn't post the OpenHab configuration I guess you haven't done any configuration there so that's the first thing to do.

which is the best library for adruino ModBus slave?

There's not a single best library for ModBus on the Arduino, it depends on your needs. As you haven't specified them I suggest that you stay with your current library until you find a reason to change.

Thank you very much for your reply!

I will try to explain my setup in details.
My OpenHab files ar set up in this way -

modbus.things:

Bridge modbus:serial:localhostTCP3 [ port="COM3", baud=9600, id=1, stopBits="1.0", parity="none",dataBits=8, echo=false, encoding="rtu", flowControlIn="none", flowControlOut="none", receiveTimeoutMillis=1500 ] {
    Bridge poller holdingPoller [ start=1, length=1, refresh=5000, type="holding" ] {
        Thing data holding5Scaled [ readStart="1", readValueType="int16", writeStart="1", writeValueType="int16", writeType="holding" ]
    }
}

modbus.items:

Number Holding5Scaled            "Holding index 5  [%.1f]"   { channel="modbus:data:localhostTCP3:holdingPoller:holding5Scaled:number" }

modbus.sitemap:

sitemap modbus label="modbus_ex_scaling"
{
    Frame {
        Text item=Holding5Scaled
        Setpoint item=Holding5Scaled minValue=0 maxValue=100 step=20
    }
}

This configuration works well with the slave simulator (for simulator I use COM4 port in modbus.things and I have virtual port pair COM4 and COM5), I am able to change holding register values (see attachement). Modbus 2.4 binding for OpenHab - link

For arduino I use Modbus-Master-Slave-for-Arduino library (link) using its example code:

/**
 *  Modbus slave example 1:
 *  The purpose of this example is to link a data array
 *  from the Arduino to an external device.
 *
 *  Recommended Modbus Master: QModbus
 *  http://qmodbus.sourceforge.net/
 */

#include <ModbusRtu.h>

// data array for modbus network sharing
uint16_t au16data[16] = {
  3, 1415, 9265, 4, 2, 7182, 28182, 8, 0, 0, 0, 0, 0, 0, 1, 1 };

/**
 *  Modbus object declaration
 *  u8id : node id = 0 for master, = 1..247 for slave
 *  u8serno : serial port (use 0 for Serial)
 *  u8txenpin : 0 for RS-232 and USB-FTDI 
 *               or any pin number > 1 for RS-485
 */
Modbus slave(1,0,0); // this is slave @1 and RS-232 or USB-FTDI

void setup() {
  slave.begin( 9600 ); // baud-rate at 9600
}

void loop() {
  slave.poll( au16data, 16 );
}

With this code I can communicate with ModBus master simulator (see attachement).

When in modbus.things I use COM3 port then I get errors with connection timeout - see OpenHab.log file in the attachment.

I think that there should be some configuration issue, but I can not figure it out. I am new fro openhab and arduino.

Thank you very much for help.

openhab.txt (30.9 KB)

Does your UNO show up as COM3 on your PC?

2018-07-30 19:59:10.371 [ERROR] [t.wimpi.modbus.io.ModbusRTUTransport] - Last request: 01 03 00 01 00 01 d5 ca
2018-07-30 19:59:10.372 [ERROR] [t.wimpi.modbus.io.ModbusRTUTransport] - failed to read: Error reading response (EOF)

Within 1ms the call fails with an EOF error code. I don't know what that means for Java running on Windows but it seems like there is either the wrong interface selected or the program doesn't have permissions for this interface. There is also a slight chance that the ModBus implementation is bad in OpenHab as many implementations on the PC suffer the correct pausing in between ModBus messages.

pylon:
Does your UNO show up as COM3 on your PC?

Yes. Arduino UNO is on COM3. You can see in the screenshot I provided in my previous message from the ModBus master simulator where it is connected and works.

I run openhab with administrator rights. Is it sufficient to get permision for using USB port for this programm? Or there is something else I sould do?

Is it better to use Linux with OpenHab?

Thank you very much for help!

UPDATE: I installed my setup on Ubuntu and everything works now. I can read and write registers to arduino as a slave. I do not know what was the problem with windows. It was suggested that it was a timing issue.

For anyone trying to use OpenHab modbus binding on ubuntu it is necessary to give openhab permision to use serial:

Add openhab to some groups:

sudo adduser openhab dialout
sudo adduser openhab tty   # ?

You need to edit openhab2 file aswell:

sudo nano /etc/default/openhab2

EXTRA_JAVA_OPTS="-Dgnu.io.rxtx.SerialPorts=/dev/ttyUSB0:/dev/ttyS0:/dev/ttyS2:/dev/ttyACM0:/dev/ttyAMA0"

I did all the OpenHab bindings using paper UI which is pretty simple step. Arduino sketch and library were the same.

Thank you!