pdf-icon

Modbus Master

Function Description

Create a Modbus master to encapsulate data using the Modbus protocol and transmit it to the slave via serial communication, with data values ranging from 0 to 65535.

Modbus Master

Init baud tx rx in uart crc Initializes the communication interface, allowing for specification of baud rate, transmit pin, receive pin, UART number, and CRC check endian mode.

Send addr function reg addr value Sends a data packet to a specified slave address, where addr is the slave address, function is the function code, reg addr is the register address, and value is the user data.

Rx buffer cache number The number of bytes read from the buffer.

Read rx data Reads the received data packet, suitable for custom processing.

Get rx addr function data Receives data packets in a callback manner, with variables receiving the parameters that automatically update.

Usage Instructions

Main functionality: (See below for slave code) Connect two M5Stack devices and establish a master-slave relationship through Modbus. The master sends data by pressing the A/B buttons and receives data packets returned by the slave (receiving function code 2). There are two ways to process the data packets:

  1. Process the data packet returned by the slave through LOOP and display it on the screen.

According to the Modbus protocol, a data packet returned by the slave includes at least three valid data (address, function code, data); hence, packets larger than 3 bytes are considered valid and are parsed using a list.

Modbus Master Loop

  1. Process the returned data packet through a callback function. When using callbacks, do not use LOOP as it may block the callback.

Set three variables to respectively receive the address, function code, and data returned by the slave.

Modbus Master Callback

Modbus Slave

Function Description

Create a Modbus slave to receive data packets encapsulated by Modbus, communicating with the master via a serial interface, with data values ranging from 0 to 65535.

Modbus Slave

Init addr baud tx rx in uart Initializes the communication interface, allowing for specification of the slave address, baud rate, transmit pin, receive pin, UART number, with CRC check in big-endian mode.

Init function reg addr value method Defines the Modbus data operation format, with function being the function code, reg addr being the register address, value being the initial default value, and method being the read or write operation mode.

Update function reg addr value Updates the data within the specified register address according to the function code.

Get rx buffer data Reads data from the buffer.

Get reg write function reg addr value Receives data packets sent by the master in a callback manner (function code, register address, data), receiving through variables, requiring custom processing.

Get function reg addr value Gets the content of a specific master data packet, specified through the function code and register address.

send addr function reg addr value Responds to the master by sending back the content of the data packet received from the master.

Usage Instructions

Main functionality: (See above for master code) Connect two M5Stack devices and establish a master-slave relationship through Modbus. When the slave receives a data packet with address code 1, function code 1, and register address 1, it parses the data. If the data is 1, it lights up the LED Bar; if it's 2, it turns off the LED Bar. The slave also updates its corresponding data in real-time and responds to the master (via function code 2). The slave itself can report data to the master in real-time by pressing the A/B buttons. The implementation methods are as follows:

  1. Process the data packet returned by the slave through LOOP, promptly update the data displayed on the screen, and respond.

Receives and parses the specified data packet, makes a decision based on the data, and reports back to the master (via function code 2). Pressing the A/B button responds on its own and sends a data packet to report the status to the master (via function code 2).

Modbus Slave Loop

On This Page