ModbusTransport#
- class ModbusTransport[source]#
Bases:
TransportBase,ABCAbstract Modbus line: owns the session, locking, and every wire op, addressed per call via
unit_id. Annotate against it; construct a concrete subclass.Initialize holder tracking, the lock, and the (not yet connected) client slot.
Attributes
Whether the client has been opened and not closed.
Methods
Validate
unit_idagainst this physical layer's range (0 to_max_unit_id) and return it.Close and teardown; with a holder, remove it and tear down only when the last owner leaves.
Return the reentrant resource lock for atomic multi-step sequences; the holder may write/query/read inside it.
Open the connection (idempotent); with a holder, admit it and return True only for the first owner.
Read coils by address (FC01).
Read discrete inputs by address (FC02).
Read holding registers by address (FC03).
Read input registers by address (FC04).
Read
addressasdata_type, dispatching byregister_typeand decoding across registers.Write a single coil by address (FC05).
Write multiple coils by address (FC15).
Write a single holding register by address (FC06).
Write multiple holding registers by address (FC16).
Encode
valueasdata_typeand write it toaddress, dispatching byregister_type.- property is_open: bool#
Whether the client has been opened and not closed. Stays
Trueacross a dropped socket, which the next op reconnects.
- check_unit_id(unit_id: int) int[source]#
Validate
unit_idagainst this physical layer’s range (0 to_max_unit_id) and return it.
- read_typed(
- register_type: Literal['holding', 'input', 'coil', 'discrete'],
- address: int,
- data_type: Literal['uint16', 'int16', 'uint32', 'int32', 'uint64', 'int64', 'float32', 'float64', 'bool'],
- *,
- unit_id: int,
- byte_swap: bool = False,
- word_swap: bool = False,
- long_swap: bool = False,
Read
addressasdata_type, dispatching byregister_typeand decoding across registers.Coils and discrete inputs are single-bit, so
data_typemust be"bool"and the read returns abool.
- write_typed(
- register_type: Literal['holding', 'input', 'coil', 'discrete'],
- address: int,
- value: int | float | bool,
- data_type: Literal['uint16', 'int16', 'uint32', 'int32', 'uint64', 'int64', 'float32', 'float64', 'bool'],
- *,
- unit_id: int,
- byte_swap: bool = False,
- word_swap: bool = False,
- long_swap: bool = False,
Encode
valueasdata_typeand write it toaddress, dispatching byregister_type.Coils are single-bit, so
data_typemust be"bool"andvaluemust be abool(no numeric coercion).
- close(holder: object | None = None) None#
Close and teardown; with a holder, remove it and tear down only when the last owner leaves.
A holder that needs to talk to the instrument before the connection goes (releasing a remote lock, say) does that in its own
closebefore calling this, while the session is still up.Raises
UnknownHolderErrorwhen an unrecognized holder closes while others still own the connection, because that is the case where ignoring it silently strands the real owners and leaves the connection open forever. Once the last owner has left there is nothing to strand, so a repeat close is a no-op and close stays idempotent.