pdf-icon

Modbus Master

Function

Create a Modbus host, encapsulate the data by ModBus protocol, and send the data to the slave through serial communication, the data value range is 0-65535.

Init baud tx rx in uart crc Initialise the communication interface, specify the baud rate, transmit pins, receive pins, serial port number and CRC check size end mode.

Send addr function reg addr value Send the packet to the specified slave address, 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 Number of bytes read from the buffer

Read rx data Read incoming packets, for customised processing

Get rx addr function data Receive packets with callbacks, receive parameters through variables, parameters are updated automatically.

Usage Description

Main Function: (Slave code see below) Connect two M5Stacks, establish the host and slave through Modbus, the host presses A/B button to send data, and receives the packet returned from the slave (Receive Function Code 2), there are two ways to process the packet as follows.

  1. The packet returned from the slave is processed through LOOP and displayed on the screen.

According to the definition of Modbus protocol, the packet returned from the slave contains at least 3 valid data (address, function code, data), so more than 3 bytes are regarded as valid data, and the data is parsed and processed through the list.

  1. Process the returned packets through the callback function, when using the callback function, do not use LOOP or it will block the callback.

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

Modbus Slave

Function

Create a Modbus slave to receive ModBus encapsulated data packets and communicate with the host via the serial interface, the data value range is 0-65535.

Init addr baud tx rx in uart Initialise the communication interface, specify the slave address, baud rate, transmit pins, receive pins, serial port number, CRC checksum for big end mode.

Init function reg addr value method Define Modbus data operation format, function is function code, reg addr is register address, value is initial default value, method is read or write operation mode.

Update function reg addr value Updates the data in the specified register address by function code.

Get rx buffer data Read buffer data

Get reg write function reg addr value Get the packet (function code, register address, data) from the host by callback, and receive it through variables, which need to be handled by yourself.

Get function reg addr value Get the contents of the specified host packet, specified by function code and register address.

send addr function reg addr value Contents of the packet sent in response to the host after receiving the host packet

Usage Description

Main function: (host code see above) connect two M5Stack, through Modbus to establish the host and the slave, the slave receives the address code 1, function code 1, register address 1 packet after parsing the data, the data for the 1 LED Bar, the data for the 2 LED Bar, while the slave real-time update of their own corresponding data, and the host to respond to the function code 2, the slave itself through the A/B button operation will also report the data to the host in real time. Code 2, the slave itself through the A/B button operation will also real-time data reported to the host. There are two ways to achieve this.

  1. Process the packet returned from the slave through LOOP, update the data on the screen in time, and respond to it.

Receive the specified packet and parse the data, judge the data to make a response and report to the host (via function code 2), press the A/B key to respond to itself and send the packet to the host to report the status (via function code 2)

  1. Process the returned data through the callback function, specify the packet to join the judgement processing of fun and reg_addr, when using the callback function must not use LOOP otherwise it will block the callback.

Set three variables to receive the address, function code and data from the host, use the list to get the data, judge the data and report the status to the host through (function code 2). Pressing A/B key manually will also report the status to the host (via function code 2) when it responds.

On This Page