Modbus¤
Prerequisites¤
See this section if you are communicating with a Modbus device on Linux or macOS via the Serial interface. Otherwise, there are no prerequisites to follow on Windows or if using a network Socket for the interface.
Modbus
(Interface)
¤
Modbus(equipment: Equipment)
Base class for the Modbus protocol (specification v1.1b3).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
equipment
|
Equipment
|
An Equipment instance. |
required |
A Connection instance supports the same properties as either Serial or Socket, depending on which underlying interface is used for the connection.
Source code in src/msl/equipment/interfaces/modbus.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
interface
property
¤
disconnect
¤
disconnect() -> None
Disconnect from the Modbus equipment.
Source code in src/msl/equipment/interfaces/modbus.py
90 91 92 93 94 | |
mask_write_register
¤
mask_write_register(
address: int, *, and_mask: int = 65535, or_mask: int = 0, device_id: int = 1
) -> ModbusResponse
Mask Write Register (function code 0x016).
Modifies the contents of the specified holding-register address using a combination of an AND mask, an OR mask and the register's current contents. This method can be used to set or clear individual bits in the holding register.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address
|
int
|
Holding-register address. Must be in the range [0, 65535]. |
required |
and_mask
|
int
|
The AND bitmask to apply to the register address. Must be in the range [0, 65535]. |
65535
|
or_mask
|
int
|
The OR bitmask to apply to the register address. Must be in the range [0, 65535]. |
0
|
device_id
|
int
|
Modbus device ID. |
1
|
Returns:
| Type | Description |
|---|---|
ModbusResponse
|
Modbus response. The response data is the result after the masks have been written. |
Source code in src/msl/equipment/interfaces/modbus.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |
read
¤
Read a Modbus message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
int | None
|
The number of bytes to read, i.e., the size of the Application Data Unit (ADU).
Only used with RTU frames. If the third byte in the response message specifies the
Byte Count (e.g., first: Device ID, second: Function Code, third: Byte Count),
then the |
None
|
Returns:
| Type | Description |
|---|---|
tuple[int, bytes]
|
The Modbus device ID and the Protocol Data Unit of the response, i.e., |
Source code in src/msl/equipment/interfaces/modbus.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
read_coils
¤
read_coils(
address: int, *, count: int = 1, device_id: int = 1
) -> ModbusResponse
Read coils (function code 0x01).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address
|
int
|
Starting register address to read from. Must be in the range [0, 65535]. |
required |
count
|
int
|
The number of coils to read. Must be in the range [1, 2000]. |
1
|
device_id
|
int
|
Modbus device ID. |
1
|
Returns:
| Type | Description |
|---|---|
ModbusResponse
|
Modbus response. Call the bits method to get the ON/OFF state of each coil. |
Source code in src/msl/equipment/interfaces/modbus.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
read_device_identification
¤
read_device_identification(
*, code_id: Literal[1, 2, 3, 4] = 1, object_id: int = 0, device_id: int = 1
) -> ModbusIdentification
Read device Identification (function code 0x2B, Modbus Encapsulated Interface type 0x0E).
The read device identification interface is modelled as an address space composed of a set of addressable data elements. The data elements are called objects and an object ID identifies them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code_id
|
Literal[1, 2, 3, 4]
|
Read device ID code.
|
1
|
object_id
|
int
|
The object ID to read.
|
0
|
device_id
|
int
|
Modbus device ID. |
1
|
Returns:
| Type | Description |
|---|---|
ModbusIdentification
|
Modbus device identification. |
Source code in src/msl/equipment/interfaces/modbus.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
read_discrete_inputs
¤
read_discrete_inputs(
address: int, *, count: int = 1, device_id: int = 1
) -> ModbusResponse
Read discrete inputs (function code 0x02).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address
|
int
|
Starting register address to read from. Must be in the range [0, 65535]. |
required |
count
|
int
|
The number of discrete inputs to read. Must be in the range [1, 2000]. |
1
|
device_id
|
int
|
Modbus device ID. |
1
|
Returns:
| Type | Description |
|---|---|
ModbusResponse
|
Modbus response. Call the bits method to get the ON/OFF state of each discrete input. |
Source code in src/msl/equipment/interfaces/modbus.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
read_exception_status
¤
read_exception_status(*, device_id: int = 1) -> ModbusResponse
Read exception status (function code 0x07).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device_id
|
int
|
Modbus device ID. |
1
|
Returns:
| Type | Description |
|---|---|
ModbusResponse
|
Modbus response. Call the bits method to get the state of each exception-status bit. |
Source code in src/msl/equipment/interfaces/modbus.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
read_holding_registers
¤
read_holding_registers(
address: int, *, count: int = 1, device_id: int = 1
) -> ModbusResponse
Read holding registers (function code 0x03).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address
|
int
|
Starting register address to read from. Must be in the range [0, 65535]. |
required |
count
|
int
|
The number of 16-bit registers to read. Must be in the range [1, 125]. |
1
|
device_id
|
int
|
Modbus device ID. |
1
|
Returns:
| Type | Description |
|---|---|
ModbusResponse
|
Modbus response. |
Source code in src/msl/equipment/interfaces/modbus.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
read_input_registers
¤
read_input_registers(
address: int, *, count: int = 1, device_id: int = 1
) -> ModbusResponse
Read input registers (function code 0x04).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address
|
int
|
Starting register address to read from. Must be in the range [0, 65535]. |
required |
count
|
int
|
The number of 16-bit registers to read. Must be in the range [1, 125]. |
1
|
device_id
|
int
|
Modbus device ID. |
1
|
Returns:
| Type | Description |
|---|---|
ModbusResponse
|
Modbus response. |
Source code in src/msl/equipment/interfaces/modbus.py
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | |
read_write_registers
¤
read_write_registers(
*,
read_address: int = 0,
read_count: int = 0,
write_address: int = 0,
address: int | None = None,
values: int | Sequence[int] | NDArray[uint16] | None = None,
device_id: int = 1
) -> ModbusResponse
Read/Write registers (function code 0x17).
Performs a combination of one read operation and one write operation in a single Modbus transaction. The write operation is performed before the read operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
read_address
|
int
|
Starting holding-register address to read from. Must be in the range [0, 65535]. |
0
|
read_count
|
int
|
The number of 16-bit registers to read. Must be in the range [1, 125]. |
0
|
write_address
|
int
|
Starting holding-register address to write to. Must be in the range [0, 65535]. |
0
|
address
|
int | None
|
Use as both the read and write address. Must be in the range [0, 65535]. |
None
|
values
|
int | Sequence[int] | NDArray[uint16] | None
|
A single value to write or a sequence of values to write. The maximum sequence length is 121. Each value must be in the range [0, 65535]. See also to_register_values. |
None
|
device_id
|
int
|
Modbus device ID. |
1
|
Returns:
| Type | Description |
|---|---|
ModbusResponse
|
Modbus response. |
Source code in src/msl/equipment/interfaces/modbus.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | |
reconnect
¤
reconnect(max_attempts: int = 1) -> None
Reconnect to the Modbus equipment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_attempts
|
int
|
The maximum number of attempts to try to reconnect with the equipment. If ≤ 0, keep trying until a connection is successful. If the maximum number of attempts has been reached then an exception is raised. |
1
|
Source code in src/msl/equipment/interfaces/modbus.py
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | |
to_register_values
staticmethod
¤
to_register_values(
data: float | Sequence[float] | NDArray[number], dtype: DTypeLike = uint16
) -> NDArray[uint16]
Convert a value or a sequence of values to an unsigned, big-endian, 16-bit integer array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
float | Sequence[float] | NDArray[number]
|
The value(s) to convert. If a numpy array, the data type must be the same that the Modbus register address(es) require the value(s) to be in. |
required |
dtype
|
DTypeLike
|
The numpy data type to use to initially create a numpy array. This should be the
same data type that the Modbus register address(es) require the value(s) to be in.
Only used if |
uint16
|
Returns:
| Type | Description |
|---|---|
NDArray[uint16]
|
An array that can be passed to write_registers or read_write_registers. |
Source code in src/msl/equipment/interfaces/modbus.py
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | |
write
¤
Write a Modbus message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function_code
|
int
|
Modbus function code. |
required |
data
|
bytes | None
|
The data associated with the |
None
|
device_id
|
int
|
Modbus device ID. |
1
|
Returns:
| Type | Description |
|---|---|
int
|
The number of bytes written. |
Source code in src/msl/equipment/interfaces/modbus.py
411 412 413 414 415 416 417 418 419 420 421 422 | |
write_coil
¤
write_coil(address: int, value: bool, *, device_id: int = 1) -> ModbusResponse
Write single coil (function code 0x05).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address
|
int
|
Register address to write to. Must be in the range [0, 65535]. |
required |
value
|
bool
|
Boolean to write. Sets the ON/OFF state of a single coil in the device. |
required |
device_id
|
int
|
Modbus device ID. |
1
|
Returns:
| Type | Description |
|---|---|
ModbusResponse
|
An echo of the request, after the register contents have been written. |
Source code in src/msl/equipment/interfaces/modbus.py
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 | |
write_coils
¤
write_coils(
address: int, values: Sequence[bool] | NDArray[bool], *, device_id: int = 1
) -> ModbusResponse
Write multiple coils (function code 0x0F).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address
|
int
|
Starting register address to write to. Must be in the range [0, 65535]. |
required |
values
|
Sequence[bool] | NDArray[bool]
|
A sequence of booleans to write. Sets the ON/OFF state of multiple coils in the device. The maximum sequence length is 1968. |
required |
device_id
|
int
|
Modbus device ID. |
1
|
Returns:
| Type | Description |
|---|---|
ModbusResponse
|
The response. The data attribute is composed of the starting register address and the number of registers that were written to. |
Source code in src/msl/equipment/interfaces/modbus.py
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 | |
write_register
¤
write_register(
address: int, value: int, *, device_id: int = 1
) -> ModbusResponse
Write a single holding-register value (function code 0x06).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address
|
int
|
Register address to write to. Must be in the range [0, 65535]. |
required |
value
|
int
|
Value to write. Must be in the range [0, 65535]. |
required |
device_id
|
int
|
Modbus device ID. |
1
|
Returns:
| Type | Description |
|---|---|
ModbusResponse
|
An echo of the request, after the register contents have been written. |
Source code in src/msl/equipment/interfaces/modbus.py
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | |
write_registers
¤
write_registers(
address: int, values: Sequence[int] | NDArray[uint16], *, device_id: int = 1
) -> ModbusResponse
Write to a contiguous block of holding registers (function code 0x10).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address
|
int
|
Starting register address to write to. Must be in the range [0, 65535]. |
required |
values
|
Sequence[int] | NDArray[uint16]
|
A sequence of values to write. The maximum sequence length is 123. Each value must be in the range [0, 65535]. See also to_register_values. |
required |
device_id
|
int
|
Modbus device ID. |
1
|
Returns:
| Type | Description |
|---|---|
ModbusResponse
|
The response. The data attribute is composed of the starting register address and the number of registers that were written to. |
Source code in src/msl/equipment/interfaces/modbus.py
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 | |
ModbusIdentification
¤
Modbus device identification.
Do not instantiate directly. This class is returned by the read_device_identification method.
Examples:
>>> identification
ModbusIdentification(code_id=1, conformity=0x83, more_follows=False, next_object_id=0, ids=[0, 1, 2])
>>> identification.objects
[ModbusObject(id=0, value=b'MSL'), ModbusObject(id=1, value=b'NZ'), ModbusObject(id=2, value=b'5.16')]
>>> for obj in identification:
... print(f"{obj.id}: {obj.value}")
0: b'MSL'
1: b'NZ'
2: b'5.16'
>>> identification[0] # object ID = 0, Manufacturer
b'MSL'
>>> identification[1] # object ID = 1, Product code
b'NZ'
>>> identification[2] # object ID = 2, Revision
b'5.16'
>>> identification[3]
Traceback (most recent call last):
...
KeyError: 'A device-identification object with id 3 is not in the Modbus response'
>>> assert identification.get(3) is None # returns None instead of raising an error
Source code in src/msl/equipment/interfaces/modbus.py
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 | |
conformity
instance-attribute
¤
conformity: int
int — Identification conformity level of the device and type of supported access.
more_follows
instance-attribute
¤
more_follows: bool
bool — Whether the identification data does not fit into a single response and several request/response transactions are required.
next_object_id
instance-attribute
¤
next_object_id: int
int — If more_follows
is True, the identification of the next object to be asked.
objects
instance-attribute
¤
objects: list[ModbusObject]
list[ModbusObject] — The device-identification objects.
get
¤
Get the value of an object ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
object_id
|
int
|
The ID of a device-identification object. |
required |
Returns:
| Type | Description |
|---|---|
bytes | None
|
The corresponding value or |
Source code in src/msl/equipment/interfaces/modbus.py
792 793 794 795 796 797 798 799 800 801 802 803 804 805 | |
ModbusObject
(NamedTuple)
¤
ModbusResponse
¤
Modbus response.
Do not instantiate directly. This class is returned by most Modbus methods.
Examples:
>>> mr = device.read_holding_registers(1, count=4)
>>> mr.array("uint16")
array([ 1000, 16, 52100, 4325], dtype='>u2')
>>> mr = device.read_input_registers(780, count=2)
>>> mr.float32()
21.5
>>> mr = device.read_coils(1250, count=6)
>>> mr.bits()
array([False, True, False, False, False, True])
Source code in src/msl/equipment/interfaces/modbus.py
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 | |
count
instance-attribute
¤
count: int | None
int — The number of registers/coils that were requested to read.
array
¤
numpy.ndarray — Returns the register data as a numpy.ndarray of the specified dtype.
Source code in src/msl/equipment/interfaces/modbus.py
602 603 604 605 606 | |
bits
¤
numpy.ndarray — Returns the states of the register bits for the specified bit_order.
Source code in src/msl/equipment/interfaces/modbus.py
608 609 610 611 | |
decode
¤
str — Returns the decoded response data using the encoding codec.
Source code in src/msl/equipment/interfaces/modbus.py
613 614 615 | |
float32
¤
float — Returns the register data as a 32-bit, floating-point number for the specified byte_order.
Source code in src/msl/equipment/interfaces/modbus.py
617 618 619 620 621 | |
float64
¤
float — Returns the register data as a 64-bit, floating-point number for the specified byte_order.
Source code in src/msl/equipment/interfaces/modbus.py
623 624 625 626 627 | |
int16
¤
int — Returns the register data as a signed, 16-bit integer for the specified byte_order.
Source code in src/msl/equipment/interfaces/modbus.py
629 630 631 632 633 | |
int32
¤
int — Returns the register data as a signed, 32-bit integer for the specified byte_order.
Source code in src/msl/equipment/interfaces/modbus.py
635 636 637 638 639 | |
int64
¤
int — Returns the register data as a signed, 64-bit integer for the specified byte_order.
Source code in src/msl/equipment/interfaces/modbus.py
641 642 643 644 645 | |
uint16
¤
int — Returns the register data as an unsigned, 16-bit integer for the specified byte_order.
Source code in src/msl/equipment/interfaces/modbus.py
647 648 649 650 651 | |
uint32
¤
int — Returns the register data as an unsigned, 32-bit integer for the specified byte_order.
Source code in src/msl/equipment/interfaces/modbus.py
653 654 655 656 657 | |
uint64
¤
int — Returns the register data as an unsigned, 64-bit integer for the specified byte_order.
Source code in src/msl/equipment/interfaces/modbus.py
659 660 661 662 663 | |
unpack
¤
tuple — Returns a tuple containing the register data unpacked according to the format string.
See Byte Order, Size, and Alignment and Format Characters
for more details about the format parameter. Modbus data is typically in big-endian byte order.
Source code in src/msl/equipment/interfaces/modbus.py
665 666 667 668 669 670 671 | |