Share this

Implementation of intelligent meter and PC communication technology based on Modbus protocol

2026-04-06 05:50:18 · · #1
Abstract: This paper mainly introduces the Modbus RTU protocol and the specific implementation method of communication between the main control PC and the intelligent instrument based on this protocol through RS-485 serial port in the VB6.0 programming environment. Keywords: Modbus RTU; communication protocol; RS-485; CRC check 1 Introduction With the progress of the times, more and more enterprises are beginning to transform towards production and management automation. Various intelligent instruments are constantly being applied to various fields of production and life. In industrial control, the RS-485 bus, due to its balanced differential transmission characteristics, has good interference resistance, long transmission distance, and large cascading capability, making it very suitable for forming industrial-grade multi-machine communication systems. The Modbus RTU protocol is a registered trademark of Modicon and is one of the mainstream communication protocols widely used in international intelligent instruments. With the widespread use of various industrial instruments today, the Modbus RTU protocol and RS-485 bus have been most widely applied. This paper mainly introduces the specific scheme for implementing this technology in an industrial control system from an application perspective. 2 Introduction to Modbus RTU Communication Protocol (1) Introduction to Modbus RTU Protocol The Modbus RTU communication protocol is a registered trademark of Modicon. It works in a master-slave query-response mode. Its specifications have been published on the Internet and it is one of the mainstream communication protocols commonly used in international intelligent instruments. Currently, many domestic manufacturers have followed this protocol standard in their products and systems. The protocol has two transmission modes: RTU mode and ASCII mode. The 8-bit data in the RTU mode information frame includes two 4-bit hexadecimal characters. Compared with ASCII mode, RTU mode requires fewer bits to express the same information and has a larger data flow at the same communication rate. Therefore, under normal circumstances, general industrial intelligent instruments and meters use the Modbus protocol in RTU mode. (2) Modbus Communication Format When a communication command is sent from the sending device (host computer) to the receiving device (slave computer), the slave computer that matches the corresponding address code will respond to the command and read the information, verify the data, and perform the corresponding function operation according to the agreed communication protocol. The returned frame structure includes the address code, function code, data, and CRC check code. (3) Communication Frame Convention Format of Upper and Lower Computers The instrument address in the voltage meter used is defined as one byte, so the maximum is 256. The communication baud rate between the lower and upper computers can generally be set freely, but it must be ensured that the communication baud rates of the upper and lower computers correspond and match. 3 Upper Computer Communication Software Design Scheme The communication software of the upper computer adopts the VB6.0 development environment. In VB6.0, the Mscomm control can be used to speed up the development process when programming the serial port. This control is mainly designed for the general serial port of RS-232. Therefore, in order to utilize the existing RS-232 interface on the PC, an RS232/485 converter is usually used. These converters generally control the three-pin signals of RXD, TXD and GND through logic gate circuits, thereby automatically controlling the half-duplex or full-duplex 485 serial port. Through the converter, we can quickly develop upper computer communication software based on RS-485 serial port just like developing a general RS-232 serial port. (1) Initialization program design In the initialization program, the main task is to set the command array, transmission baud rate, and some necessary serial port initial settings for the entire frame structure. The specific operation is explained in the following code: Dim Tcommand (7) As Byte 'Open the frame length array of the command frame MSComm.CommPort = 1 'Set the serial port to be used. Of course, the input mode can be flexibly set here. It is temporarily set to port 1# MSComm.Settings="2400,n,8,1" 'Set the transmission baud rate and parity mode MSComm.InBufferSize = 1024 'Open the data buffer MSComm.InputMode =comInputModeBinary 'Set to binary data stream mode MSComm.InputLen = 0 'Read all the required data at once... The above code is generally the part that must be set for serial port initialization. According to personal needs, an initialization interface can be made in VB6.0 to freely set and change various parameters (such as baud rate, serial port used, etc.) to improve the flexibility and universality of initialization settings. (2) Command word transmission The command word transmission should strictly follow the frame format designed by the Modbus protocol and the communication protocol defined by the upper and lower computers based on the protocol. Refer to the communication protocol sequence in Table 1 and write the following VB code to implement the command transmission. Tcommand (0) = address ' address is the variable input by the set receiving address Tcommand (1) = Val("&h" + "03") …… (other communication protocols, and calculate the CRC check code) Tcommand (6) = CRC (1) Tcommand (7) = CRC (0) frmcontrol.MSComm.Output = Tcommand …… (3) Upper computer reception and data processing According to the feedback communication protocol in Table 2, the upper computer parses the received data and performs necessary processing. Generally speaking, the data sent from the lower computer has a certain frame length. Especially for some fixed smart instruments. Therefore, the best method is to open two arrays of the same length in the upper computer program according to the frame length of the communication feedback. One is used as the receive array to receive data from the serial port buffer at once; the other is the safety array to copy the data in the receive array and parse it. This can improve the fault tolerance of the whole system. LoopUntil frmcontrol.MSComm.InBufferCount = 9 InByte = frmcontrol.MSComm.Input For i = 0 To (count - 1) InSafeArray(i) = InByte(i) Next i The inbyte() in its code is the receive array, and InSafeArray() is the safety array we open up. (4) CRC check implementation method According to the Modbus protocol check method, the check method of RTU mode is CRC check method; while the ASCII mode is LRC check. Most smart meters use the CRC check method of RTU. According to the different generator polynomials, CRC check usually has the following types: CRC-12 (transmit 6 bits); CRC-16 (American standard, transmit 8 bits); CRC-CCITT (European standard, transmit 8 bits); CRC-32 (used in point-to-point synchronous transmission). Currently, the most common checksum used in instruments is CRC-16. Its generator polynomial is X¹⁶ + X¹⁵ + X² + 1. In the algorithm implementation, we can first preset a 16-bit register FFFF (all 1s), then XOR 8 bits of binary data (one byte) with the lower 8 bits of the 16-bit CRC register, and put the result back into the CRC register while shifting the register content right, filling the MSB with 0, and checking the shifted-out LSB. If the LSB is 0, continue shifting right; if it is 1, XOR the CRC register with the polynomial. Repeat the above operation until one 8-bit byte is completed, then continue the same process for the next data, until all data is processed. At this point, the data in the CRC register is the CRC code we need. The implementation code is as follows: CRC16Lo = &HFF 'CRC16Lo is the lower 8 bits of the CRC register CRC16Hi = &HFF 'CRC16Hi is the higher 8 bits of the CRC register CL = &H1 CH = &HA0 'A001 H is the CRC-16 polynomial code For i = 0 To UBound(Data) CRC16Lo = CRC16Lo Xor Data(i) ' Each data is XORed with the CRC register For index = 0 To 7 UseHi = CRC16Hi UseLo = CRC16Lo CRC16Hi = CRC16Hi 2 CRC16Lo = CRC16Lo 2 ' Shift right by one bit If ((UseHi And &H1) = &H1) Then ' If the last bit of the high-order byte is 1, CRC16Lo = CRC16Lo Or &H80 ' After shifting the low-order byte right, add 1 to the front End If If ((UseLo And &H1) = &H1) Then ' If LSB If the result is 1, then XOR it with the polynomial: CRC16Hi = CRC16Hi Xor CH CRC16Lo = CRC16Lo Xor CL End If Next index Next i 4 Introduction to Lower-Level Machine Working System Design Lower-level machines generally use microcontrollers with pre-defined communication protocols and operating instructions. Because measurement and control systems often employ a single-transmitter, multiple-receiver communication mechanism, the lower-level machine must allow users to pre-set its own address code, ensuring that it is not duplicated. Furthermore, it is important to ensure that the communication baud rate matches the upper-level machine's baud rate. In lower-level machine software development, the communication frame structure of the Modbus protocol must be followed to parse the commands transmitted by the upper-level machine: non-calling objects should be able to promptly resume the waiting-to-receive state; lower-level machines responding to calls should parse the command code and perform corresponding functional processing, and should be able to send back error messages for illegal command operations. In specific engineering development, lower-level machines often use intelligent instruments based on the Modbus protocol from various manufacturers. While this simplifies our development workload, the proper arrangement and design of the lower-level machines can reduce the system error rate and improve reliability. Therefore, it is an important aspect that no industrial system designer should overlook. In industrial control, due to the presence of various interference sources and the generally large number of lower-level machines, the selection of RS-485 chips requires careful consideration. If conditions permit, chips with shock resistance and high cascading drive capability should be selected. Theoretically, the cascading capability of a RS-485 chip is at least 32 gates, and many chip manufacturers can currently achieve industrial-grade cascading numbers of 128 or more. In practical applications, the system should have a certain margin of safety; generally, the number of cascaded gates should not exceed 70% of the device's full load. When selecting RS232 to RS485 converters, active converters should be used whenever possible to improve drive capability and stability. Furthermore, the appropriateness of noise reduction, isolation, wiring, shielding, and other measures in the industrial environment will also affect the final system stability. 5. Conclusion In the development of a practical measurement and control system project, the author developed a host computer-based measurement and control management software based on this communication protocol using VB6.0 combined with SQL database programming. The software also utilized the RS-485 bus to monitor and process data from various slave devices based on this protocol. In today's era of automated production activities, standardized communication protocols, and internationalized information exchange, fully utilizing existing mature and standardized communication protocols can maximize cost savings, reduce development risks, and improve system compatibility and portability. As a widely used international communication protocol in the field of intelligent instruments and meters, the Modbus RTU protocol will undoubtedly see wider promotion and application. 6 References [1] Visual Basic 6.0 Programmer's Guide; by Microsoft Corporation; Beijing Hope Electronic Press; 1999.2; pp. 9-22, 304-394 [2] Practical Visual Basic 6 Tutorial; by Bob Reselman and Richard Peasley; Tsinghua University Press; 2001.3; pp. 489-500 [3] Design and Implementation of Communication Based on Modbus Protocol; Pan Hongyue; Measurement Technology; 2002. No. 4; pp. 35-36
Read next

CATDOLL Rosie Hard Silicone Head

The head made from hard silicone does not have a usable oral cavity. You can choose the skin tone, eye color, and wig, ...

Articles 2026-02-22
CATDOLL 136CM Vivian

CATDOLL 136CM Vivian

Articles
2026-02-22
CATDOLL Nonoka Soft Silicone Head

CATDOLL Nonoka Soft Silicone Head

Articles
2026-02-22