Skip to content

Modbus data conversion error #4

@Karsak4747

Description

@Karsak4747

Hello, I have encountered a problem related to a modbus data conversion error.

At first I used:

  • pymodbus==2.5.3;
  • pyscada==0.8.0;
  • pyscada-modbus==0.8.0.

I have a modbus TCP server in which the temperature variable changes. When reading from a register with the float32 (real) data type, the bit order is set to 0-1-2-3, the value is read without problems. When I start sending data to the same register, but for SP, I get an error:

[20/Apr/2024 20:25:45] DEBUG [pyscada.models:2468] 17
[20/Apr/2024 20:25:45] ERROR [pyscada.utils.scheduler:927] pyscada.modbus-17-192.168.16.83:502-3, unhandled exception
Traceback (most recent call last):
File "/home/pyscada/.venv/lib/python3.9/site-packages/pyscada/utils/scheduler.py", line 818, in run
status, data = self.loop()
File "/home/pyscada/.venv/lib/python3.9/site-packages/pyscada/utils/scheduler.py", line 1713, in loop
tmp_data = device.write_data(var_id, task.value, task)
File "/home/pyscada/.venv/lib/python3.9/site-packages/pyscada/modbus/device.py", line 374, in write_data
self.slave.write_registers(self.variables[variable_id].modbusvariable.address,
File "/home/pyscada/.venv/lib/python3.9/site-packages/pymodbus/client/common.py", line 103, in write_registers
return self.execute(request)
File "/home/pyscada/.venv/lib/python3.9/site-packages/pymodbus/client/sync.py", line 109, in execute
return self.transaction.execute(request)
File "/home/pyscada/.venv/lib/python3.9/site-packages/pymodbus/transaction.py", line 174, in execute
response, last_exception = self._transact(
File "/home/pyscada/.venv/lib/python3.9/site-packages/pymodbus/transaction.py", line 271, in _transact
packet = self.client.framer.buildPacket(packet)
File "/home/pyscada/.venv/lib/python3.9/site-packages/pymodbus/framer/socket_framer.py", line 206, in buildPacket
data = message.encode()
File "/home/pyscada/.venv/lib/python3.9/site-packages/pymodbus/register_write_message.py", line 164, in encode
packet += struct.pack('>H', value)
struct.error: required argument is not an integer
[20/Apr/2024 20:25:45] DEBUG [pyscada.utils.scheduler:1049] Process pyscada.modbus-17-192.168.16.83:502-3(36470) is stopped

I tried to write data to a register by writing a program with pymodbus==2.5.3. The code looked like this:

from pymodbus.client.sync import ModbusTcpClient
import struct

SERVER_HOST = '192.168.16.83'
SERVER_PORT = 502

REGISTER_ADDRESS = 4111
REGISTER_COUNT = 2

client = ModbusTcpClient(SERVER_HOST, port=SERVER_PORT)
client.connect()

float_value = 93.0
int_value = struct.unpack('>I', struct.pack('<f', float_value))[0]
register_values = [(int_value >> 16) & 0xFFFF, int_value & 0xFFFF]
client.write_registers(REGISTER_ADDRESS, register_values, unit=3)
client.close()

Thanks to this program, it was possible to write the value to the register. So the problem is not with the modbus server.

At this stage, I have updated pyscada and pyscada-modbus to the latest version. The pymodbus version has been downgraded to 1.2.

At this stage, for some reason, the current value of the variable in the control element has stopped being displayed, everything works in the display value.

I experimented with the bit order in the variable settings in pyscada, found that at 1-0-3-2 the data is written, of course the value does not correspond to reality, it is being written to the register. I also tried to change the data type of the variable in pyscada to int32, leaving the bit order 0-1-2-3. The recording did not occur, but the error changed to:

[22/Apr/2024 11:53:52] ERROR [pyscada.utils.scheduler:923] pyscada.modbus-28-192.168.16.83:502-3, unhandled exception
Traceback (most recent call last):
File "/home/pyscada/.venv/lib/python3.9/site-packages/pyscada/utils/scheduler.py", line 822, in run
status, data = self.loop()
File "/home/pyscada/.venv/lib/python3.9/site-packages/pyscada/utils/scheduler.py", line 1711, in loop
tmp_data = device.write_data(var_id, task.value, task)
File "/home/pyscada/.venv/lib/python3.9/site-packages/pyscada/modbus/device.py", line 421, in write_data
list(self.variables[variable_id].encode_value(value)),
File "/home/pyscada/.venv/lib/python3.9/site-packages/pyscada/models.py", line 2164, in encode_value
output = unpack(target_format, pack(source_format, value))
struct.error: required argument is not an integer

Can you tell me what these errors are and how i can fix them?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions