[SOLVED] Modbus TCP initializing and multiple connections to same slave

Hi,

I am running Openhab 2.1 and Modbus binding 1.10.0.

My setup consist of one raspberry PI that is running Openhab and 2 modbus TCP slaves.

I have 2 problems:

After reboot of PI one of the modbus TCP connections is not comming back online(only reading from this slave), but if I just save my .items file with out any changes everything works fine. No errors in my log file.

When I try to read two registers from the same modbus slave, holding(float32) and coil(bit) only one of them work at a given time. My workaround here is to read coils as float and then use rules. I have a working setup where I can read discrete register and coil at the same time.

Hope some one has some good suggestions here :slight_smile:

Show us your problem config please.

Here is my Modbus configuration, I have also tried without the timer parameters:

modbus:tcp.slave1.connection=192.168.1.101:502:50:500:100:2:200
modbus:tcp.slave1.type=holding
modbus:tcp.slave1.id=1
modbus:tcp.slave1.start=0
modbus:tcp.slave1.length=75
modbus:tcp.slave1.valuetype=float32

modbus:tcp.slave2.connection=192.168.1.166:502:50:500:800:2:200
modbus:tcp.slave2.type=coil
modbus:tcp.slave2.id=1
modbus:tcp.slave2.start=0
tcp.slave2.valuetype=bit

modbus:tcp.slave3.connection=192.168.1.166:502:50:500:1400:2:200
modbus:tcp.slave3.id=1
modbus:tcp.slave3.start=0
modbus:tcp.slave3.length=15
modbus:tcp.slave3.type=discrete

Items:

//MODBUS TORKE
Number MBTCP_Temp1 “Modbus Temp. 1 [%.1f C]” (gAll,rrd4j,gModbus1) {modbus="<[slave1:28]"}
Number MBTCP_RH1 “Modbus RH1 [%.1f %%]” (gAll,rrd4j,gModbus1) {modbus="<[slave1:27]"}
Number MBTCP_AH1 “Modbus AH1 [%.1f g/m3]” (gAll,rrd4j,gModbus1) {modbus="<[slave1:31]"}
Number MBTCP_Temp2 “Modbus Temp. 2 [%.1f C]” (gAll,rrd4j,gModbus) {modbus="<[slave1:30]"}
Number MBTCP_RH2 “Modbus RH2 [%.1f %%]” (gAll,rrd4j,gModbus1) {modbus="<[slave1:29]"}
Number MBTCP_AH2 “Modbus AH2 [%.1f g/m3]” (gAll,rrd4j,gModbus1) {modbus="<[slave1:32]"}

Number F_ALARM “Torke Alarm [%.0f]” (gAll,gModbus1) {modbus="<[slave1:21]"}
Number F_SENSOR1 “Torke Feil Sensor 1 [%.0f]” (gAll,gModbus1) {modbus="<[slave1:22]"}
Number F_SENSOR2 “Torke Feil Sensor 2 [%.0f]” (gAll,gModbus1) {modbus="<[slave1:23]"}

//MODBUS FJOS

Switch Mokkaskrape1_Av “Mokkaskrape Nord av [%s]” (gAll,gMokk,gModbus2) {modbus=“slave2:0”}
Switch Mokkaskrape1_Venstre “Mokkaskrape Nord venstre [%s]” (gAll,gMokk,gModbus2) {modbus=“slave2:1”}
Switch Mokkaskrape1_Hoyre “Mokkaskrape Nord hoyre [%s]” (gAll,gMokk,gModbus2) {modbus=“slave2:2”}
Switch Mokkaskrape2_Av “Mokkaskrape Syd av [%s]” (gAll,gMokk,gModbus2) {modbus=“slave2:3”}
Switch Mokkaskrape2_Venstre “Mokkaskrape Syd venstre [%s]” (gAll,gMokk,gModbus2) {modbus=“slave2:4”}
Switch Mokkaskrape2_Hoyre “Mokkaskrape Syd hoyre [%s]” (gAll,gMokk,gModbus2) {modbus=“slave2:5”}

Switch Inngang1 “Forutlegger induktivgiver [%s]” (gAll,gModbus2) {modbus="<[slave3:0]"}
Switch Inngang2 “Inngang2 [%s]” (gAll,gModbus2) {modbus="<[slave3:1]"}
Switch Inngang3 “Inngang3 [%s]” (gAll,gModbus2) {modbus="<[slave3:2]"}
Switch Inngang4 “Inngang4 [%s]” (gAll,gModbus2) {modbus="<[slave3:3]"}
Switch Inngang5 “Inngang5 [%s]” (gAll,gModbus2) {modbus="<[slave3:4]"}
Switch Inngang6 “Inngang6 [%s]” (gAll,gModbus2) {modbus="<[slave3:5]"}
Switch Inngang7 “Inngang7 [%s]” (gAll,gModbus2) {modbus="<[slave3:6]"}
Switch Inngang8 “Inngang8 [%s]” (gAll,gModbus2) {modbus="<[slave3:7]"}

slave2 is defined with no length (i.e default length 1), but maybe that’s a copy/paste error ?

coil type is by definition bit, so valuetype=bit is redundant here. I think I may have seen that cause issues before, best just leave it out.

There is a kind of config cache, so once you have edited modbus.cfg to your satisfaction, follow -

Hi

It seems weird that you have “modbus:” prefix there as this is openhab2. I think that is only for openhab1

I would try to remove that. Also make note of the cache issue with openhab2 mentioned above. Something you have to deal with when changing the configuration.

Best
Sami

P s. The length is actually mandatory per docs, default might be something unreasonable…

Hi

Thanks for quick reply!

Yes it was a copy fault.

Sadly my modbus communication is still having the same problem after reboot of PI.

My modbus.config now looks like this:
poll=“10000”
service.pid=“org.openhab.modbus”
tcp.slave1.connection=“192.168.1.101:502:50:500:100:2:200”
tcp.slave1.id=“1”
tcp.slave1.length=“75”
tcp.slave1.start=“0”
tcp.slave1.type=“holding”
tcp.slave1.valuetype=“float32”
tcp.slave2.connection=“192.168.1.166:502:50:500:800:2:200”
tcp.slave2.id=“1”
tcp.slave2.length=“15”
tcp.slave2.start=“0”
tcp.slave2.type=“coil”
tcp.slave3.connection=“192.168.1.166:502:50:500:1400:2:200”
tcp.slave3.id=“1”
tcp.slave3.length=“15”
tcp.slave3.start=“0”
tcp.slave3.type=“discrete”

Any suggestions?

Best regards
Jostein

Hi

Did you clean the cache as suggested? Reboot is not enough as far as I know.

Hi

yes I did delete the modbus.config file and then “sudo reboot”.
The pasted modbus config above is copied out of modbus.config after reboot.

Have also tried to remove the timer and max retries parameters without any luck.

This gets posted in my Log after reboot.
2017-09-21 08:17:23.579 [INFO ] [b.core.service.AbstractActiveService] - Modbus Polling Service has been started

All my values start out as “-” and then the value become “0” until I save .items (without changes) file and values get the correct values.

hmm, float32 is made up of register pairs. Thus the length, the number of registers to read, should always be an even number for float32.

EDIT - for test purposes while sorting it out, I would try a lot less ‘length’ (say 10 or so) to reduce chances of timeout. fragmentation issues etc.

Hi,
Thanks, I did not think of that, I have now changed it to 76. But I am still struggeling :frowning:

Isnt it strange that it works after just saving the .item file if it is the length? But I will try

Best regards
Jostein

Hi,

I have done some testing with the following settings:

tcp.slave1.connection="192.168.1.101:502"
tcp.slave1.id="1"
tcp.slave1.length="26"
tcp.slave1.start="40"
tcp.slave1.type="holding"
tcp.slave1.valuetype=“float32”

//MODBUS TORKE
Number MBTCP_Temp1 “Modbus Temp. 1 [%.1f C]” (gAll,rrd4j,gModbus1) {modbus="<[slave1:8]"}
Number MBTCP_RH1 “Modbus RH1 [%.1f %%]” (gAll,rrd4j,gModbus1) {modbus="<[slave1:7]"}
Number MBTCP_AH1 “Modbus AH1 [%.1f g/m3]” (gAll,rrd4j,gModbus1) {modbus="<[slave1:11]"}
Number MBTCP_Temp2 “Modbus Temp. 2 [%.1f C]” (gAll,rrd4j,gModbus) {modbus="<[slave1:10]"}
Number MBTCP_RH2 “Modbus RH2 [%.1f %%]” (gAll,rrd4j,gModbus1) {modbus="<[slave1:9]"}
Number MBTCP_AH2 “Modbus AH2 [%.1f g/m3]” (gAll,rrd4j,gModbus1) {modbus="<[slave1:12]"}

After reboot MBTCP_AH2 (slave1:12)is getting the value that is belonging to MBTCP_RH1 (slave1:7) before I save my .items file. So apparently the modbus communication is working but with wrong address. When I save .items file all values work correctly. Could there be a .items file saved somewhere like the modbus.config file that is loading after reboot?

Best regards

Jostein

Hi,

Now it is working, it seems to be like rossko57 was saying that I was polling to many registers at once.
My setup is now:
modbus.config:
tcp.slave1.connection=192.168.1.101:502
tcp.slave1.type=holding
tcp.slave1.id=1
tcp.slave1.start=42
tcp.slave1.length=24
tcp.slave1.valuetype=float32

default.items:
Number MBTCP_Temp1 “Modbus Temp. 1 [%.1f C]” (gAll,rrd4j,gModbus1) {modbus="<[slave1:7]"}
Number MBTCP_RH1 “Modbus RH1 [%.1f %%]” (gAll,rrd4j,gModbus1) {modbus="<[slave1:6]"}
Number MBTCP_AH1 “Modbus AH1 [%.1f g/m3]” (gAll,rrd4j,gModbus1) {modbus="<[slave1:10]"}
Number MBTCP_Temp2 “Modbus Temp. 2 [%.1f C]” (gAll,rrd4j,gModbus) {modbus="<[slave1:9]"}
Number MBTCP_RH2 “Modbus RH2 [%.1f %%]” (gAll,rrd4j,gModbus1) {modbus="<[slave1:8]"}
Number MBTCP_AH2 “Modbus AH2 [%.1f g/m3]” (gAll,rrd4j,gModbus1) {modbus="<[slave1:11]"}
Number F_ALARM “Torke Alarm [%.0f]” (gAll,gModbus1) {modbus="<[slave1:0]"}
Number F_INN_TEMP_ALARM “Inntemperatur Alarm [%.0f]” (gAll,gModbus1) {modbus="<[slave1:3]"}
Number F_INN_TEMP_KRITISK “Inntemperatur Kritisk [%.0f]” (gAll,gModbus1) {modbus="<[slave1:4]"}
Number F_SENSOR1 “Torke Feil Sensor 1 [%.0f]” (gAll,gModbus1) {modbus="<[slave1:1]"}
Number F_SENSOR2 “Torke Feil Sensor 2 [%.0f]” (gAll,gModbus1) {modbus="<[slave1:2]"}

Thank you for helping me :slight_smile:

It could be a timing or fragmentation (packet size) issue behind that. Or maybe the slave has ‘hole’ in the larger target register block that it doesn’t like. You would have to experiment using error logging to see why.

If it is all doing what you want now though, I guess it is covered :slight_smile: