Skip to content

struct.error: required argument is not an integer #2368

@MAKOMO

Description

@MAKOMO

Versions

  • Python: 3.12
  • OS: macOS 15
  • Pymodbus: 3.7.3
  • Modbus Hardware (if used):

Description

The type of the Client methods write_register (and write_registers) was changed in client/mixin.py (#2309). Now the value to be sent should be of type bytes instead of int as in the previous releases.

Converting the int value to bytes using value.to_bytes(2, 'big') makes mypy happy, but results in

  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/pymodbus/client/base.py", line 104, in async_execute
    packet = self.ctx.framer.buildPacket(request)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/pymodbus/framer/base.py", line 85, in buildPacket
    data = message.function_code.to_bytes(1,'big') + message.encode()
                                                     ^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/pymodbus/pdu/register_write_message.py", line 42, in encode
    packet += struct.pack(">H", self.value)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
struct.error: required argument is not an integer

Sending the int value as before works, but makes mypy sad.

What to do?

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