Modbus binding issues with both serial and tcp connections

Hi,

I am running OH2 on a RPi3 with Rasbian Jessie. I would like to use the Modbus binding.

First I tried the Modbus binding 1.9.0 with a serial connection but the binding is not able to open the serial port.

2017-04-21 10:54:05.574 [ERROR] [et.wimpi.modbus.net.SerialConnection] - Could not get port identifier, maybe insufficient permissions. null
2017-04-21 10:54:05.576 [ERROR] [ing.ModbusSlaveConnectionFactoryImpl] - connect try 1/3 error: Could not get port identifier, maybe insufficient permissions. null. Connection SerialConnection@19862c[portName=/dev/ttyAMA0,port=<null>]. Endpoint ModbusSerialSlaveEndpoint@74355a[portName=/dev/ttyAMA0]
2017-04-21 10:54:05.615 [ERROR] [et.wimpi.modbus.net.SerialConnection] - Could not get port identifier, maybe insufficient permissions. null
2017-04-21 10:54:05.616 [ERROR] [ing.ModbusSlaveConnectionFactoryImpl] - connect try 2/3 error: Could not get port identifier, maybe insufficient permissions. null. Connection SerialConnection@19862c[portName=/dev/ttyAMA0,port=<null>]. Endpoint ModbusSerialSlaveEndpoint@74355a[portName=/dev/ttyAMA0]
2017-04-21 10:54:05.655 [ERROR] [et.wimpi.modbus.net.SerialConnection] - Could not get port identifier, maybe insufficient permissions. null
2017-04-21 10:54:05.657 [ERROR] [ing.ModbusSlaveConnectionFactoryImpl] - connect try 3/3 error: Could not get port identifier, maybe insufficient permissions. null. Connection SerialConnection@19862c[portName=/dev/ttyAMA0,port=<null>]. Endpoint ModbusSerialSlaveEndpoint@74355a[portName=/dev/ttyAMA0]

It suggests that this is a permission problem. The default settings for the serial port are as follows:

$ ls -l /dev/ttyAMA0
crw-rw---- 1 root dialout 204, 64 apr 20 15:43 /dev/ttyAMA0 

So I tried to add user openhab to the dialout group, so I get:

$ groups openhab
openhab : openhab dialout

As this didn’t make any difference I changed the ownership of the serial port to openhab :

$ ls -l /dev/ttyAMA0 
crw-rw---- 1 openhab openhab 204, 64 apr 21 10:35 /dev/ttyAMA0

I restarted OpenHAB but I still get the above error messages. What am I doing wrong?


Rather frustrated with this experience I thought I’d I try a tcp connection but I get an error here as well:

2017-04-21 11:07:28.205 [ERROR] [inding.modbus.internal.ModbusBinding] - Exception when parsing configuration parameter tcp.slave1.connection = 192.168.0.158:5001:60:0:0:3:100  --  java.lang.IllegalArgumentException invalid baud rate: 5001
2017-04-21 11:07:28.210 [ERROR] [org.apache.felix.configadmin        ] - [org.osgi.service.cm.ManagedService, org.osgi.service.event.EventHandler, id=294, bundle=189/mvn:org.openhab.binding/org.openhab.binding.modbus/1.9.0.RC1]: Updating property tcp.slave1.connection of configuration org.openhab.modbus caused a problem: Exception when parsing configuration parameter tcp.slave1.connection = 192.168.0.158:5001:60:0:0:3:100  --  java.lang.IllegalArgumentException invalid baud rate: 5001
org.osgi.service.cm.ConfigurationException: tcp.slave1.connection : Exception when parsing configuration parameter tcp.slave1.connection = 192.168.0.158:5001:60:0:0:3:100  --  java.lang.IllegalArgumentException invalid baud rate: 5001

Why do I get “invalid baud rate: 5001” error when I have defined a tcp connection ?
The configuration on modbus.cfg seems correct:

tcp.slave1.connection=192.168.0.158:5001:60:0:0:3:100

Can anyone here help me to get serial or tcp Modbus connections to work?

For serial connection:
delete /var/lib/openhab2/config/org/openhab/modbus.config
make sure that the port you’re using is listed in /etc/default/openhab2
restart openhab and check the log

Thanks for your help. Deleting modbus.config and restarting solved the tcp connection issue. But the serial port error message is still there. The openhab2 file contains just 1 line:

$ cat /etc/default/openhab2
EXTRA_JAVA_OPTS=""

How do I add the serial port there ?

Never mind, found it in the docs (reading everything is important :wink:)

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

Now the serial port can be accessed. I get a read error though, have to debug some more.

Hey @ondrej1024 I had a hard start with Modbus too so I would like to give you this handy Pyhton based Tool http://mblogic.sourceforge.net/ it helped me so much figuring out what my devices are doing and how to configure openHAB correctly.
I hope this can help you too with further struggles :slight_smile:

Thanks, I’ll have a look.

Serial port access is solved now but there is still no response received to the Modbus request:

2017-04-21 15:44:04.361 [ERROR] [t.wimpi.modbus.io.ModbusRTUTransport] - Last request: 01 03 00 00 00 01 84 0a
2017-04-21 15:44:04.362 [ERROR] [t.wimpi.modbus.io.ModbusRTUTransport] - failed to read: Error reading response (EOF)
2017-04-21 15:44:04.364 [ERROR] [pi.modbus.io.ModbusSerialTransaction] - execute try 1/3 error: I/O exception - failed to read. Request: net.wimpi.modbus.msg.ReadMultipleRegistersRequest@e8b6b9 (unit id 1 & transaction 63). Serial parameters: SerialParameters@762df8[portName=/dev/ttyAMA0,baudRate=9600,flowControlIn=none,flowControlOut=none,databits=8,stopbits=1,parity=none,encoding=rtu,echo=false,receiveTimeoutMillis=1500]

I don’t know the implementation details of the OpenHAB Modbus binding but at this point my guess is that it does not support the RS485 direction control. In fact, I don’t see any settings for that in the docs.

I am using a SN65HVD72 RS485 transceiver chip that needs the direction control. With the libmodbus C library this can be done with the UARTs RTS line.

So before further trying to get this working, can someone please confirm that this operating mode is NOT supported with the Modbus binding? Are there any plans to support this in the future?

@ondrej1024 check this discussion

Do not know exactly the details here but I wonder if flowControlIn or flowControlOut is something you are looking for?

See the docs

http://docs.openhab.org/addons/bindings/modbus1/readme.html

The EOF can be due to many issues though. Does it succeed eventually with the 3 tries (your log just lists one try)?

Best,
Sami

Hi Sami,

in fact I was wondering if the flowControlIn or flowControlOut parameters could be used but I think they are related to RS232 flow control via RTS/CTS lines. The RS485 is different because it doesn’t have seperate RX and TX lines and therefore is only half duplex. You can put the transceiver either in RX or TX mode, but not both at the same time. Therefore direction control is needed. This is how libmodbus handles it, using the serial ports RTS line:

To enable the RTS mode, the values MODBUS_RTU_RTS_UP or MODBUS_RTU_RTS_DOWN
must be used, these modes enable the RTS mode and set the polarity at the same
time. When MODBUS_RTU_RTS_UP is used, an ioctl call is made with RTS flag
enabled then data is written on the bus after a delay of 1 ms, then another
ioctl call is made with the RTS flag disabled and again a delay of 1 ms occurs.
The MODBUS_RTU_RTS_DOWN mode applies the same procedure but with an inverted
RTS flag.

With a USB to RS485 adapter this is done by the HW circuit so the software or driver doesn’t have to worry about it. But in my case I need the software support.

Anyway there is a plan B. I wrote a software Modbus TCP to Modbus RTU gateway which handles the RS485 communication just fine. So I can use Modbus TCP in the OH binding to talk to the gateway.

Ondrej

Hi @ondrej1024,

I’m also using a RS485 shield that is attached to my raspberry pi.
Do you mind sharing you software solution?
It would be a big help for me since I have exact the same issue.

Thanks in advance!

Greetings,
Frederic

Of course, no problem.
Originally I wrote this software Modbus TCP to Modbus RTU gateway for the Telegea project. It is a program written in C and it uses libmodbus for the Modbus implementation. You can find the source code and documentation in GitHub.

Ondrej