1. HMI and Communication Protocol
With the development of industrial technology, the application field of HMI (Human-Machine Interface) is becoming increasingly broad. The custom communication protocol implemented by Topway’s intelligent LCD display module can maintain the confidentiality of enterprise data to a certain extent, enhance the diversity of product functions, and solve the problem of incompatible communication protocols.
The serial communication rules for HMI, except for standard Modbus protocol products, are mostly based on the protocol rules set by the original manufacturer. However, many enterprises also have their own communication rules and message formats. When using HMI for application development, enterprises hope to retain or use their own functional characteristics and data interaction rules to match their protocol application development.
Based on this, Topway’s HMI serial screen has a relatively mature design scheme. This article will introduce how the intelligent display module HMT050DTA-D is designed and implemented based on LUA scripts. The library functions provided by the manufacturer include open API for serial control classes, which can realize the design of the enterprise’s custom communication protocol through these API functions.
2. Custom Communication Protocol
This design will achieve the target protocol communication function through the serial port of Topway’s intelligent LCD screen HMI, in conjunction with LUA programs, using serial control class APIs.
The HMI serial communication protocol commonly consists of several parts: “frame header”, “length code”, “function code”, “parameter data information”, “frame tail”, and “checksum”. During the communication process, the system will query the data received from the serial port in real-time, obtain message data that meets the protocol, and execute the specified command functions.
The following “Table 2-1: Custom Protocol Communication Message Format” defines a common communication message format. (Hardware serial port parameter description: Baud rate “115200”; Data bits “8”; Stop bits “1”; Checksum as shown in the table)
Table 2-1: Custom Protocol Communication Message Format
*1. Command length code, calculates the total byte count of all data from “command code” to “checksum”.*2. Command address, data, parameter information, are multi-byte data, with high byte first and low byte last.*3. Command checksum, calculates the ModbusCRC-16 checksum of all data from “frame header” to “frame tail”.
3. Functional Requirements Analysis
1. HMI Original Communication Protocol Masking
The current model HMT050DTA-D defaults to TOPWAY protocol communication, which has a command error code response mechanism. Therefore, during the custom protocol communication process, if the original protocol is not masked, there may be cases of serial port occupation and response data confusion. Therefore, when designing one’s own protocol communication, the original manufacturer’s protocol must first be masked to avoid data conflicts.
2. Receiving Command Data Processing and Parsing Response
After obtaining the serial port data, it is necessary to extract the commands that meet the custom protocol rules for subsequent parsing and execution of command functions. Therefore, it is necessary to define a receiving command array to store the command data that meets the protocol rules in the serial port.
In protocol communication, there is also a command response function, such as the command to read/access address data. After parsing the received command, we need to respond to some commands, so it is necessary to define a response command array to reply to the read/access operation commands.
3. Protocol Communication Command Function Requirements
In LUA program design, the functional modules to be implemented include: ① obtaining commands that meet the specified custom rules from the serial port receiving area and storing them in the receiving command array; ② calling the received command data to parse the command data; ③ calling the corresponding functional processing function to execute the command function. For read/access information commands, it is also necessary to operate the serial port to send data for a reply.
The parsed commands currently mainly implement 4 functional modules. ① Function code “0x01”: Access/read variable data; ② Function code “0x02”: Rewrite variable data; ③ Function code “0x03”: Engineering display page jump; ④ Function code “0x04”: Screen backlight brightness control. The main functional relationships are shown in Figure 3-1.
Figure 3-1 Main Functional Modules of Protocol Communication
4. Lua Implementation of Custom Protocol
1. Masking the Original Manufacturer Communication Protocol
In the LUA library functions provided by Topway, there is a TOPWAY protocol disabling function that can be called during program initialization to mask the original manufacturer’s communication protocol. As shown in Figure 4-1
Call function: hmt.bypass(is) Input: Parameter “is” to enable switch. Usage instruction: When “is=1”, mask the original TOPWAY protocol; when “is=0”, use the TOPWAY protocol.
2. Data Structure of Custom Protocol
2.1 Receiving Command Array
Define array: CustProt_recvcmd = {} Type: Global array, defined during initialization. Explanation: Stores command data that meets protocol rules in the serial port receiving area. During the reception of command data, if it does not meet the defined rules, the receiving array must be cleared.
2.2 Response Command Array
Define array: CustProt_sendcmd = {} Type: Global array, defined during initialization. Explanation: Stores command data that needs to be replied to through the serial port according to protocol rules. Clear the array after sending is complete.
3. Custom Protocol Design Implementation
The main function implementation in LUA program design is shown in flowchart 4-2.
3.1 Check Serial Port Receiving Area Data
Call function: hmt.uartisempty() Output: Returns “1” when the serial port receiving buffer is empty; returns “0” when there is data in the serial port receiving area. Usage instruction: Use this function to determine whether there is data in the serial port receiving area. When it equals 0, execute the command fetching function.
3.2 Obtain Commands that Meet Custom Protocol
Constructor: get_CustProtrecvcmd() Output: Receiving command array CustProt_recvcmd. Explanation: Stores commands that meet the custom protocol in the global receiving command array; during this period, it will check the data formats of the frame header, frame tail, and length code. If it does not meet the custom protocol, the command array will be cleared.
Call function: hmt.getchar() Output: The first byte of data from the serial port receiving queue. Usage instruction: In the function get_CustProtrecvcmd(), this function will be frequently called to filter the data in the serial port receiving area.
3.3 Custom Protocol Data Check and Verification
Constructor: CmdCheck(CustProt_recvcmd) Input: Receiving command array CustProt_recvcmd. Explanation: This function will check the data code and checksum formats in the receiving command array. If it does not meet the custom protocol, the command array will be cleared, an error code will be replied, and the command fetching will restart.
3.4 Command Data Parsing
Constructor: CmdHandle(CustProt_recvcmd) Input: Receiving command array CustProt_recvcmd. Explanation: This function categorizes and processes command data based on the function code in the command, such as numeric data and string data.
3.5 Execute Command Functionality
3.5.1 Read Variable Data Function
Constructor: _ReadvariableReturnCmd (Addr) Call function: hmt.readvp16(Addr)
hmt.readvp32(Addr)
hmt.readvpstr(Addr) etc. Input: Address data of the command in the custom protocol. Explanation: When the function code is “0x01”, this function will be used to process, and the function will call the hmt read variable functions according to the address type in the command, read back the data, and store it in the response command array, and finally send the return through the serial port.
Call function: hmt.putchar(byte) Input: Command byte data. Explanation: When replying to the read command, this function will be frequently called to send the data in the response command array through the serial port.
3.5.2 Write Variable Data Function
Constructor: _Writevariable(addr, value) _Writevariable1(addr, strvalue) Call function: hmt.writevp16(Addr, value)
hmt.writevp32(Addr, value)
hmt.writevpstr(Addr, strvalue) etc. Input: “Addr” custom protocol command address data, “value/strvalue” numeric data or string data to be written in the custom protocol. Explanation: When the function code is “0x02”, this function will be used to process, and the function will call the hmt write variable functions according to the address type in the command to write the numeric or string data into the corresponding variable.
Call function: string.char(byte) Input: Byte from the received custom command. Output: Character data. Usage instruction: When writing string variables, this function needs to convert numeric command data into character data to write into the variable. It can be combined with the string concatenation operator “..” to convert into string data.
3.5.3 Engineering Page Jump Function
Call function: hmt.changepage(pageid) Input: “pageid” custom protocol command page ID information. Usage instruction: When the function code is “0x03”, this function will be used to process, and CmdHandle will handle the double-byte ID information in the custom command as numeric data, and then pass the numeric data to this function to jump to the specified page.
3.5.4 Set Screen Backlight Function
Define array: setBLcmd = {0x5F, 0x00} Type: Local array, defined in the CmdHandle function. Explanation: The CmdHandle function will pass the brightness value from the custom command into the second byte of the setBLcmd array; combined with the hmt library function hmt.runcmd(cmdtable, len) to execute internal dimming commands.
Call function: hmt.runcmd(cmdtable, len) Input: “cmdtable” internal command array, “len” call array length. Usage instruction: When the function code is “0x04”, this function will be used to process, calling the local array setBLcmd to execute the custom protocol’s backlight adjustment function.
5. Conclusion
This article introduces the testing communication protocol using Topway’s intelligent LCD display module, aiming to present an idea for implementing communication protocols, as the functional design at the application layer in fields such as industrial control, IoT smart homes, environmental monitoring, and data security can greatly influence or even determine the data transmission format at the protocol layer.
The implementation of the custom protocol communication for the HMI of the intelligent display module allows users to flexibly design protocol rules according to their needs, controlling the product to achieve expected functions, providing a new solution for functional design of applications to a certain extent.
Recommended Product:
HMT121ATA-D
Parameter Information: Interface RJ45 RS232 Operating Voltage 12V Operating Temperature -20°C ~ 70°C Dimensions 293mm x 232mm x 28.7mm Size 12.1″ Software Protocol TOPWAY Window 245.76mm x 184.32mm Touch Screen CTPProduct Features:• Large cover capacitive touch screen, integrated installation• Supports USB direct upgrade of display projects• Supports remote protocol communication control, remote display project upgrades• Supports capacitive screen multifunctional touch keys• Supports SGTools for convenient development and full widget functionality• Supports Lua script multifunctional development
Leave a Comment
Your email address will not be published. Required fields are marked *