maix.comm.modbus
maix.comm.modbus module
You can use
maix.comm.modbus
to access this module with MaixPy
This module is generated from MaixPy and MaixCDK
Module
No module
Enum
Mode
modbus mode
item | describe |
---|---|
values | RTU: TCP: |
C++ defination code:
enum class Mode : unsigned char { RTU, TCP }
RequestType
Modbus request types enumeration.\nThis enumeration defines the various Modbus request types,\nincluding functions for reading and writing coils, registers,\nas well as diagnostics and identification.
item | describe |
---|---|
values | READ_COILS: < Read Coils READ_DISCRETE_INPUTS: < Read Discrete Inputs READ_HOLDING_REGISTERS: < Read Holding Registers READ_INPUT_REGISTERS: < Read Input Registers WRITE_SINGLE_COIL: < Write Single Coil WRITE_SINGLE_REGISTER: < Write Single Register DIAGNOSTICS: < Diagnostics (Serial Line only) GET_COMM_EVENT_COUNTER: < Get Comm Event Counter (Serial Line only) WRITE_MULTIPLE_COILS: < Write Multiple Coils WRITE_MULTIPLE_REGISTERS: < Write Multiple Registers REPORT_SERVER_ID: < Report Slave ID (Serial Line only) MASK_WRITE_REGISTER: < Mask Write Register READ_WRITE_MULTIPLE_REGISTERS: < Read/Write Multiple Registers READ_DEVICE_IDENTIFICATION: < Read Device Identification UNKNOWN: < Unknown Request Type |
C++ defination code:
enum class RequestType : unsigned char { READ_COILS = 0x01, ///< Read Coils READ_DISCRETE_INPUTS = 0x02, ///< Read Discrete Inputs READ_HOLDING_REGISTERS = 0x03, ///< Read Holding Registers READ_INPUT_REGISTERS = 0x04, ///< Read Input Registers WRITE_SINGLE_COIL = 0x05, ///< Write Single Coil WRITE_SINGLE_REGISTER = 0x06, ///< Write Single Register DIAGNOSTICS = 0x08, ///< Diagnostics (Serial Line only) GET_COMM_EVENT_COUNTER = 0x0B, ///< Get Comm Event Counter (Serial Line only) WRITE_MULTIPLE_COILS = 0x0F, ///< Write Multiple Coils WRITE_MULTIPLE_REGISTERS = 0x10, ///< Write Multiple Registers REPORT_SERVER_ID = 0x11, ///< Report Slave ID (Serial Line only) MASK_WRITE_REGISTER = 0x16, ///< Mask Write Register READ_WRITE_MULTIPLE_REGISTERS = 0x17, ///< Read/Write Multiple Registers READ_DEVICE_IDENTIFICATION = 0x2B, ///< Read Device Identification UNKNOWN = 0xFF ///< Unknown Request Type }
Variable
Function
Class
Slave
Class for modbus Slave
C++ defination code:
class Slave
__init__
def __init__(self, mode: Mode, ip_or_device: str, coils_start: int = 0, coils_size: int = 0, discrete_start: int = 0, discrete_size: int = 0, holding_start: int = 0, holding_size: int = 0, input_start: int = 0, input_size: int = 0, rtu_baud: int = 115200, rtu_slave: int = 1, tcp_port: int = 5020, debug: bool = False) -> None
Modbus Slave constructor.\nThis constructor initializes a Modbus Slave instance. Depending on the mode (RTU or TCP),\nit sets up the necessary parameters for communication and defines the register structure.
item | description |
---|---|
type | func |
param | mode: Specifies the communication mode: RTU or TCP. ip_or_device: The UART device name if using RTU mode. If TCP mode is selected, this parameter is ignored. coils_start: The starting address of the coils register. coils_size: The number of coils to manage. discrete_start: The starting address of the discrete inputs register. discrete_size: The number of discrete inputs to manage. holding_start: The starting address of the holding registers. holding_size: The number of holding registers to manage. input_start: The starting address of the input registers. input_size: The number of input registers to manage. rtu_baud: The baud rate for RTU communication. Supported rates include: 110, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 500000, 576000, 921600, 1000000, 1152000, 1500000, 2500000, 3000000, 3500000, 4000000. Default is 115200. Ensure that the selected baud rate is supported by the underlying hardware and libmodbus. rtu_slave: The RTU slave address. Ignored in TCP mode. Default is 1. tcp_port: The port used for TCP communication. Ignored in RTU mode. Default is 5020. debug: A boolean flag to enable or disable debug mode. Default is false. |
see | modbus.Mode for valid modes. |
static | False |
C++ defination code:
Slave(maix::comm::modbus::Mode mode, const std::string& ip_or_device, uint32_t coils_start=0, uint32_t coils_size=0, uint32_t discrete_start=0, uint32_t discrete_size=0, uint32_t holding_start=0, uint32_t holding_size=0, uint32_t input_start=0, uint32_t input_size=0, int rtu_baud=115200, uint8_t rtu_slave=1, int tcp_port=5020, bool debug=false)
receive
def receive(self, timeout_ms: int = -1) -> maix.err.Err
Receives a Modbus request\nThis function is used to receive a Modbus request from the client. The behavior of the function\ndepends on the parameter timeout_ms
provided, which dictates how long the function will wait\nfor a request before returning.
item | description |
---|---|
type | func |
note | This function gets and parses the request, it does not manipulate the registers. |
param | timeout_ms: Timeout setting -1 Block indefinitely until a request is received 0 Non-blocking mode; function returns immediately, regardless of whether a request is received >0 Blocking mode; function will wait for the specified number of milliseconds for a request |
return | maix::err::Err type, @see maix::err::Err |
static | False |
C++ defination code:
::maix::err::Err receive(const int timeout_ms=-1)
request_type
def request_type(self) -> RequestType
Gets the type of the Modbus request that was successfully received\nThis function can be used to retrieve the type of the request received after a successful\ncall to receive()
. The return value indicates the type of the Modbus request, allowing\nthe user to understand and process the received request appropriately.
item | description |
---|---|
type | func |
return | RequestType The type of the Modbus request that has been received. |
see | modbus.RequestType |
static | False |
C++ defination code:
::maix::comm::modbus::RequestType request_type()
reply
def reply(self) -> maix.err.Err
Processes the request and returns the corresponding data.\nThis function handles the requests received from the client. It retrieves any data that the client\nneeds to write to the registers and updates the registers accordingly. Additionally, it retrieves\nthe requested data from the registers and sends it back to the client in the response.\nThis function is essential for managing read and write operations in a Modbus Slave context.
item | description |
---|---|
type | func |
note | The function will modify the Slave's internal state based on the data received in the request, and ensure that the appropriate data is returned to the client. |
return | maix::err::Err type, @see maix::err::Err |
static | False |
C++ defination code:
::maix::err::Err reply()
receive_and_reply
def receive_and_reply(self, timeout_ms: int = -1) -> RequestType
Receives a request from the client and sends a response.\nThis function combines the operations of receiving a request and sending a corresponding\nresponse in a single call. It waits for a specified duration (defined by the timeout_ms
\nparameter) to receive a request from the client. Upon successful receipt of the request,\nit processes the request and prepares the necessary data to be sent back to the client.
item | description |
---|---|
type | func |
param | timeout_ms: The timeout duration for waiting to receive a request. - A value of -1 makes the function block indefinitely until a request is received. - A value of 0 makes it non-blocking, returning immediately without waiting for a request. - A positive value specifies the maximum time (in milliseconds) to wait for a request before timing out. |
return | RequestType The type of the Modbus request that has been received. |
see | modbus.RequestType |
static | False |
C++ defination code:
::maix::comm::modbus::RequestType receive_and_reply(const int timeout_ms=-1)
coils
def coils(self, data: list[int] = [], index: int = 0) -> list[int]
Reads from or writes to coils.\nThis function can be used to either read data from coils or write data to them.\nIf the data
parameter is empty, the function performs a read operation.\nIf data
is not empty, the function writes the contents of data
to the coils\nstarting at the specified index.
item | description |
---|---|
type | func |
param | data: A vector of data to be written. If empty, a read operation is performed. If not empty, the data will overwrite the coils from index .index: The starting index for writing data. This parameter is ignored during read operations. |
return | std::vector<uint16_t> When the read operation is successful, return all data in the coils as a list. When the write operation is successful, return a non-empty list; when it fails, return an empty list. |
static | False |
C++ defination code:
std::vector<uint8_t> coils(const std::vector<uint8_t>& data = std::vector<uint8_t>{}, const uint32_t index = 0)
discrete_input
def discrete_input(self, data: list[int] = [], index: int = 0) -> list[int]
Reads from or writes to discrete input.\nThis function can be used to either read data from discrete input or write data to them.\nIf the data
parameter is empty, the function performs a read operation.\nIf data
is not empty, the function writes the contents of data
to the discrete input\nstarting at the specified index.
item | description |
---|---|
type | func |
param | data: A vector of data to be written. If empty, a read operation is performed. If not empty, the data will overwrite the discrete input from index .index: The starting index for writing data. This parameter is ignored during read operations. |
return | std::vector<uint16_t> When the read operation is successful, return all data in the discrete input as a list. When the write operation is successful, return a non-empty list; when it fails, return an empty list. |
static | False |
C++ defination code:
std::vector<uint8_t> discrete_input(const std::vector<uint8_t>& data = std::vector<uint8_t>{}, const uint32_t index = 0)
input_registers
def input_registers(self, data: list[int] = [], index: int = 0) -> list[int]
Reads from or writes to input registers.\nThis function can be used to either read data from input registers or write data to them.\nIf the data
parameter is empty, the function performs a read operation.\nIf data
is not empty, the function writes the contents of data
to the input registers\nstarting at the specified index.
item | description |
---|---|
type | func |
param | data: A vector of data to be written. If empty, a read operation is performed. If not empty, the data will overwrite the input registers from index .index: The starting index for writing data. This parameter is ignored during read operations. |
return | std::vector<uint16_t> When the read operation is successful, return all data in the input registers as a list. When the write operation is successful, return a non-empty list; when it fails, return an empty list. |
static | False |
C++ defination code:
std::vector<uint16_t> input_registers(const std::vector<uint16_t>& data = std::vector<uint16_t>{}, const uint32_t index = 0)
holding_registers
def holding_registers(self, data: list[int] = [], index: int = 0) -> list[int]
Reads from or writes to holding registers.\nThis function can be used to either read data from holding registers or write data to them.\nIf the data
parameter is empty, the function performs a read operation.\nIf data
is not empty, the function writes the contents of data
to the holding registers\nstarting at the specified index.
item | description |
---|---|
type | func |
param | data: A vector of data to be written. If empty, a read operation is performed. If not empty, the data will overwrite the holding registers from index .index: The starting index for writing data. This parameter is ignored during read operations. |
return | std::vector<uint16_t> When the read operation is successful, return all data in the holding registers as a list. When the write operation is successful, return a non-empty list; when it fails, return an empty list. |
static | False |
C++ defination code:
std::vector<uint16_t> holding_registers(const std::vector<uint16_t>& data = std::vector<uint16_t>{}, const uint32_t index = 0)