Modbus Master

Function Description

Create a Modbus Master, encapsulate the data with Modbus protocol, send the data to the Slave through serial communication, and the data value range is 0-65535.

  • Init baud tx rx in uart crc Initialize the communication interface, and specify the Baudrate, TX, RX, Serial-port and CRC check with BIG or LITTLE mode

  • Send addr function reg addr value Send the packet to the specified Slave address. addr is the Slave address, function is the function number, reg_addr is the register address, and value is the user data.

  • Rx buffer cache number Number of bytes read from buffer

  • Read rx data Read the received packets, suitable for custom processing

  • Get rx addr function data Receive packets by callback, receive parameters by variables, and update parameters automatically

Instructions

Main functions: (used together with the following Slave code) connect two M5Stack Fire, establish the Master and Slave through Modbus, the Master press the A / B button to send data, and receive the data packet returned from the Slave (receive function code 2). There are two ways to process the data packet:

1.The data package returned from the Slave is processed by loop-cycle and displayed on the screen.

According to the definition of Modbus protocol, the data package returned from the Slave contains at least 3 valid data (address, function code, data), so more than 3 bytes is regarded as valid data, and the data is parsed through the List-block.

  1. Handle the returned packets through the callback. When using the callback function, do not use loop-cycle, otherwise the callback will be blocked.

Set three variables to receive the address, function number and data returned from the Slave.

Modbus Slave

Function Description

Create a Modbus Slave, receive the data package encapsulated by Modbus, communicate with the Master through the serial interface, and the data value range is 0-65535.

  • Init addr baud tx rx in uart Initialize the communication interface, specify the Slave address, Baudrate, TX, RX and Serial-port,CRC is verified as BIG mode.

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

  • Update function reg addr value Update the data in the specified register address according to the function number.

  • Get rx buffer data Read buffer data

  • Get reg write function reg addr value Obtain the data packets (function number, register address, data) sent by the Master by callback, and receive them by variables.

  • Get function reg addr value Get the content of the specified Master data package, and specify it by function number and register address.

  • send addr function reg addr value Respond to the content of the packet sent to the Master after receiving the Master packet.

Instructions

Main functions: (used together with the Master code above) connect two M5Stack Fire, and establish the Master and Slave through Modbus. The Slave receives the data package of address code 1, function number 1 and register address 1, and then parses the data. if the data is 1 to light the LED bar, and if the data is 2 to turn off the LED Bar, at the same time, the Slave updates its own corresponding data in real time and responds to the Master (through function code 2). The Slave will also report the data to theMaster in real time through pressed A / B button. There are two ways to achieve this:

1.Process the data package returned from the Slave through loop-cycle, update the data on the screen in time and respond.

Receive the specified packet and analyze the data, judge the data to respond and report to the Master (via function number 2), press A / B button to respond to itself, and send the packet report status to the Master (via function number 2)

  1. Through the callback to process the returned data, specify that the packet should add judgment processing to "fun" and "reg_addr". When using the callback function, loop should not be used, otherwise the callback will be blocked.

Set three variables to receive the address, function number and data sent by the Master, use the List-block to obtain data, judge and process the data, and report the status to the Master through (function number 2). Press the A / B button manually to report to the Master (via function number 2) while responding.

On This Page