8.22. modbus 模块 API 文档

8.22.1. API

8.22.1.1. class ModBus(_modbus._ModBus):

A subclass of _modbus._ModBus that provides methods for serializing and sending modbus messages.

def serializeWriteBits(self,addr:int,src:list)->bytes:...

Serialize a write multiple coils request.

Args:

addr (int): The starting address of the coils to be written.

src (list): A list of boolean values (0 or 1) to be written to the coils.

Returns:

bytes: The serialized message as a bytes object.

def serializeWriteRegisters(self,addr:int,src:list)->bytes:...

Serialize a write multiple registers request.

Args:

addr (int): The starting address of the registers to be written.

src (list): A list of integer values (0-65535) to be written to the registers.

Returns:

bytes: The serialized message as a bytes object.

def serializeReadBits(self,addr:int,nb:int)->bytes:...

Serialize a read coils request.

Args:

addr (int): The starting address of the coils to be read.

nb (int): The number of coils to be read.

Returns:

bytes: The serialized message as a bytes object.

def serializeReadInputBits(self,addr:int,nb:int)->bytes:...

Serialize a read discrete inputs request.

Args:

addr (int): The starting address of the discrete inputs to be read.

nb (int): The number of discrete inputs to be read.

Returns:

bytes: The serialized message as a bytes object.

def serializeReadRegisters(self,addr:int,nb:int)->bytes:...

Serialize a read holding registers request.

Args:

addr (int): The starting address of the holding registers to be read.

nb (int): The number of holding registers to be read.

Returns:

bytes: The serialized message as a bytes object.

def serializeReadInputRegisters(self,addr:int,nb:int)->bytes:...

Serialize a read input registers request.

Args:

addr (int): The starting address of the input registers to be read.

nb (int): The number of input registers to be read.

Returns:

bytes: The serialized message as a bytes object.

def serializeWriteBit(self,addr:int,status:int)->bytes:...

Serialize a write single coil request.

Args:

addr (int): The address of the coil to be written.

status (int): The value (0 or 1) to be written to the coil.

Returns:

bytes: The serialized message as a bytes object.

def serializeWriteRegister(self,addr:int,value:int)->bytes:...

Serialize a write single register request.

Args:

addr (int): The address of the register to be written.

value (int): The value (0-65535) to be written to the register.

Returns:

bytes: The serialized message as a bytes object.

def serializeMaskWriteRegister(self,addr:int,andMask:int,orMask:int)->bytes:...

Serialize a mask write register request.

Args:

addr (int): The address of the register to be modified.

andMask (int): The AND mask to be applied to the current value of the register.

orMask (int): The OR mask to be applied to the result of the AND operation.

Returns:

bytes: The serialized message as a bytes object.

def serializeReportSlaveId(self)->int:...

Serialize a report slave ID request.

Returns:

int: The length of the serialized message in bytes.

def deserializeReadRegisters(self,msg:bytes)->list:...

Deserialize a read holding registers response.

Args:

msg (bytes): The received message as a bytes object.

Returns:

list: A list of integer values (0-65535) read from the registers.

def deserializeReadBits(self,msg:bytes)->list:...

Deserialize a read coils response.

Args:

msg (bytes): The received message as a bytes object.

Returns:

list: A list of boolean values (True or False) read from the coils.

def deserializeReadInputBits(self,msg:bytes)->list:...

Deserialize a read discrete inputs response.

Args:

msg (bytes): The received message as a bytes object.

Returns:

list: A list of boolean values (True or False) read from the discrete inputs.

def deserializeReadInputRegisters(self,msg:bytes)->list:...

Deserialize a read input registers response.

Args:

msg (bytes): The received message as a bytes object.

Returns:

list: A list of integer values (0-65535) read from the input registers.

def deserializeWriteAndReadRegisters(self,msg:bytes)->list:...

Deserialize a write and read registers response.

Args:

msg (bytes): The received message as a bytes object.

Returns:

list: A list of integer values (0-65535) written to and read from the registers.

8.22.1.2. class ModBusRTU(ModBus):

def __init__(self,sendBuffSize:int,readBuffSize:int):...

Initialize a Modbus RTU protocol instance.

Args:

sendBuffSize (int): The size of the send buffer in bytes.

readBuffSize (int): The size of the read buffer in bytes.

8.22.1.3. class ModBusTCP(ModBus):

def __init__(self,sendBuffSize:int,readBuffSize:int):...

Initialize a Modbus TCP protocol instance.

Args:

sendBuffSize (int): The size of the send buffer in bytes.

readBuffSize (int): The size of the read buffer in bytes.

8.22.2. Examples

8.22.2.1. rtu_master_err.py

# Import modbus module
import modbus

# Create a ModBusRTU object, specify the send buffer and receive buffer size as 128 bytes
mb = modbus.ModBusRTU(128, 128)

# Set slave address to 1
mb.setSlave(1)

# Generate a request frame for reading registers, specify the start address as 0 and the quantity as 10
send_buff = mb.serializeReadRegisters(0, 10)

# Print the byte string of the request frame
print(send_buff)

# Parse a response frame for reading registers, return a list containing the values of the registers
host_regists = mb.deserializeReadRegisters(
    b'\x01\x03\x14\x00\x00\x04\xD2\x00\x00\x00\x00\x00\x7B\x00\x00\x00\x00\x00\x00\x00\x00\xE5\x0B'
)
print(host_regists)

8.22.2.2. rtu_master.py

# Import modbus module
import modbus

# Create a ModBusRTU object, specify the send buffer and receive buffer size as 128 bytes
mb = modbus.ModBusRTU(128, 128)

# Set slave address to 1
mb.setSlave(1)

# Generate a request frame for reading registers, specify the start address as 0 and the quantity as 10
send_buff = mb.serializeReadRegisters(0, 10)

# Print the byte string of the request frame
print(send_buff)

# Parse a response frame for reading registers, return a list containing the values of the registers
host_regists = mb.deserializeReadRegisters(
    b'\x01\x03\x14\x00\x00\x00\x00\x04\xD2\x00\x00\x00\x00\x00\x7B\x00\x00\x00\x00\x00\x00\x00\x00\xE5\x0B'
)
print(host_regists)


# Generate a request frame for reading input registers, specify the start address as 0 and the quantity as 2
mb.serializeReadInputRegisters(0, 2)

# Parse a response frame for reading input registers, return a list containing the values of the input registers

mb.deserializeReadInputRegisters(b'\x01\x04\x04\x00\x00\x08\xE6\x7D\xCE')


# Generate a request frame for writing a single register, specify the register address as 0 and the value as 0x1234
send_buff = mb.serializeWriteRegister(0, 0x1234)
print(send_buff)

send_buff = mb.serializeWriteBits(0, [1, 1, 1, 0, 1, 0, 1, 0])
print(send_buff)