ModbusRTUTransport#
- class ModbusRTUTransport(
- port: str,
- baudrate: int = 9600,
- parity: Literal['N', 'E', 'O'] = 'N',
- stopbits: Literal[1, 2] = 1,
- bytesize: Literal[5, 6, 7, 8] = 8,
- timeout: float = 3.0,
- framer: Literal['rtu', 'ascii'] = 'rtu',
Bases:
ModbusTransportA Modbus serial line (RTU or ASCII framing). One port, addressed per wire op via
unit_id.Validate and store the serial port’s fields.
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.- check_unit_id(unit_id: int) int#
Validate
unit_idagainst this physical layer’s range (0 to_max_unit_id) and return it.
- 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.
- property is_open: bool#
Whether the client has been opened and not closed. Stays
Trueacross a dropped socket, which the next op reconnects.
- lock() RLock#
Return the reentrant resource lock for atomic multi-step sequences; the holder may write/query/read inside it.
- open(holder: object | None = None) bool#
Open the connection (idempotent); with a holder, admit it and return True only for the first owner.
- 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).