Hello I’m struggling with the modbus RTU communication on serial port.
The device provides a series of 16-bit words, each word reports the status and activation of the relays.
Example:
Bit 1 = 0 Relay 1 (OFF)
Bit 1 = 1 Relay 1 (ON)
Bit 2 = 0 Relay 2 (OFF)
Bit 2 = 1 Relay 2 (ON)
Bit 3 = 0 Relay 3 (OFF)
Bit 3 = 1 Relay 3 (ON)
Bit 4 = 0 Relay 4 (OFF)
Bit 4 = 1 Relay 5 (ON)
Items correctly report status but do not work on board relays.
Only the Item Switch U1_1 sometimes works …
I do not understand where I’m wrong
Actually the length is number of registers, not the number of bits.
From the documentation
number of data items to read. Data items here refers to registers, coils or discrete inputs depending on the slave type. For example, if the goal is to read one item with valuetype=int32, one needs to read two registers (2 * 16bit = 32bit), thus length = 2. If three coils are of interest, one should specify length = 3
It sounds like reading works but writing does not like you expect??. I think you are suffering from this issue
This has been known issue for quite some time. See also docs for more information:
Note: The way Decimal commands are handled currently means that it is probably not useful to try to use Decimal commands with non-16bit valuetypes.
The reason for all of this is that there is no way to write individual bits to registers with modbus (protocol). You can only write whole 16 bit registers.
You can workaround this limitation by keeping the whole register in memory, and combining individual bit with that. It requires rules and additional items. The forum should have some guidance or tips regarding this.
I don’t understand the term “with the same items”? What do expect to achieve. I’m not sure what are “reels” in this case.
Btw, if in the first example you would like to write to modbus coils, but read the status from bits of holding registers – it is possible to configure with the binding.
All these details vary so much between slaves – it’s about conventions after all. Modbus protocol essentially just provides binary level access to data. This means it might be hard to understand what makes sense with your device, unless more context is given. For example, device name, link to manual, etc. If you can describe the interpretation of modbus input registers, holding registers, coils and discrete inputs with our device, it would help.
You have to find out (from the documentation) where to find the status of the relay.
Depending on the device it can be discrete input, input register or even holding register.
In general, no way to tell.
EDIT: for example: with my PLC, I can read the status of the output by reading the coil. I can also control the coil by setting (writing) it. With some devices reading the status is separated from command – this is your case.
EDIT2: I tried searching for “relay” or “reel” in the documentation but found nothing. Have a bit hard time deciphering this device myself.
Commenting here on your comment in the other thread (accidental mixup threads?)
always with modpol I can activate the relays simply by pointing the word and modifying the single bit
I am not aware of such functionality of setting single bit inside a word (=16 bit register) with modpoll. As explained above
The reason for all of this is that there is no way to write individual bits to registers with modbus (protocol). You can only write whole 16 bit registers.
Since modpoll just exposes pure Modbus protocol, it has no way of having that feature.
Can you share your modpoll command, I think there is mixup here still. It’s quite straightforward to convert modpoll read/write commands to relevant modbus binding configuration.
The point is that Modpoll writes the COMPLETE 16-bit register to the slave. So does the OpenHAB binding.
It is not possible to write, say, bit 3 - and then later on write bit 4 while expecting bit 3 to remain the same. How could the slave know that you did not mean to turn bit 3 off?
If you can find a way that the device supports writing to relays as ‘modbus coils’ that would be easiest.
If you are stuck with bits-in-a-register, it is possible to write rules that keep a ‘shadow image’ of the register in OpenHAB, which allows other rules to change individual bits within it. When the shadow image changes, the binding writes the whole register to the slave.