Share this

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

2026-04-06 08:01:58 · · #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 the 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 advancement of technology, more and more enterprises are transforming towards production and management automation. Various intelligent instruments are increasingly being applied to all aspects of production and daily life. In industrial control, the RS-485 bus, due to its balanced differential transmission characteristics, offers good interference resistance, long transmission distance, and significant cascading capability, making it ideal for building industrial-grade multi-machine communication systems. The Modbus RTU protocol, a registered trademark of Modicon, is one of the mainstream communication protocols widely used in international intelligent instruments. In today's widespread use of industrial instruments , the Modbus RTU protocol and RS-485 bus have found the most extensive application. This article mainly introduces a specific solution 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 Inc. It operates using a master-slave query-response mechanism, and its specifications have been published on the internet. It is one of the mainstream communication protocols widely used in international intelligent instruments. Currently, many domestic manufacturers have adopted this protocol standard in their products and systems. This protocol has two transmission modes: RTU mode and ASCII mode. In RTU mode, the 8-bit data in the information frame consists of two 4-bit hexadecimal characters. Compared to ASCII mode, RTU mode requires fewer bits to express the same information and has a larger data throughput at the same communication rate. Therefore, industrial intelligent instruments typically 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, read the information, verify the data, and execute the corresponding functional operation according to the agreed communication protocol. The returned frame structure includes the address code, function code, data, and CRC checksum.

(3) Communication frame convention between upper and lower computers

The instrument address is defined as one byte in the voltage meter used, so the maximum is 256. The communication baud rate between the lower-level machine and the upper-level machine can generally be set freely, but it must be ensured that the communication baud rates of the upper and lower machines correspond and match.

3. Host Computer Communication Software Design Scheme

The host computer's communication software uses the VB6.0 development environment. In VB6.0, serial port programming can utilize the Mscomm control to accelerate development. This control is primarily designed for the general-purpose RS-232 serial port. Therefore, to utilize the existing RS-232 interface on the PC, an RS232/485 converter is typically used. These converters generally control the RXD, TXD, and GND pins through logic gates, thereby automatically controlling the half-duplex or full-duplex 485 serial port. Using this converter, we can quickly develop host computer communication software based on the RS-485 serial port, just like developing software for a general-purpose RS-232 serial port.

(1) Initialization program design

The initialization process mainly involves setting the command array for the entire frame structure, the transmission baud rate, and some necessary initial serial port settings. The specific operations are explained in the code below.

Dim Tcommand(7) As Byte 'Allocate an array of command frame lengths'

MSComm.CommPort = 1 'Sets the serial port to be used. Of course, you can flexibly set it here using the input method. Temporarily set to port 1#.

MSComm.Settings="2400,n,8,1" 'Sets the transmission baud rate and parity method.

MSComm.InBufferSize = 1024 'Allocate a data buffer

MSComm.InputMode = comInputModeBinary 'Sets the data stream mode to binary.

MSComm.InputLen = 0 'Read all the required data at once'

...

The code above generally represents the necessary settings for serial port initialization. Depending on individual needs, an initialization interface can be created in VB6.0 to freely set and change various parameters (such as baud rate, serial port usage, etc.) to improve the flexibility and versatility of the initialization settings.

(2) Sending command word

Command word transmission should strictly adhere to the frame format designed by the Modbus protocol and the communication protocol defined by the upper and lower computers based on this protocol. Referring to the communication protocol sequence in Table 1, write the following VB code to implement command transmission.

Tcommand (0) = address 'address is a variable set to accept address input'

Tcommand(1) = Val("&h" + "03")

... (Other communication protocols, and calculate CRC checksums)

Tcommand (6) = CRC (1)

Tcommand (7) = CRC (0)

frmcontrol.MSComm.Output = Tcommand

...

(3) Host computer receiving and data processing

According to the feedback communication protocol in Table 2, the host computer parses the received data and performs necessary processing. Generally, the data sent from the slave device has a certain frame length, especially for some pre-defined smart meters. Therefore, the best approach is to allocate two arrays of the same length in the host computer program, based on the frame length of the feedback communication. One array serves as the receive array, used to receive data from the serial port buffer all at once; the other is a security array, used to copy the data from the receive array and parse it. This improves the fault tolerance of the entire system.

LoopUntil frmcontrol.MSComm.InBufferCount = 9

InByte = frmcontrol.MSComm.Input

For i = 0 To (count - 1)

InSafeArray(i) = InByte(i)

Next i

The inbyte() function in the code receives the array, while InSafeArray() is the safe array that we allocate.

(4) Implementation method of CRC check

According to the Modbus protocol, the verification method in RTU mode is CRC checksum, while in ASCII mode it is LRC checksum. Most smart meters use the CRC checksum of RTU mode. Depending on the generator polynomial, CRC checks typically include: CRC-12 (6 bits); CRC-16 (US standard, 8 bits); CRC-CCITT (European standard, 8 bits); and CRC-32 (used in point-to-point synchronous transmission). Currently, CRC-16 is the most common checksum in meters. Its generator polynomial is X16 + X15 + X2 + 1. In terms of algorithm implementation, we can first preset a 16-bit register FFFF (all 1s), then XOR the 8-bit 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 to the right, filling the MSB with 0, and checking the shifted-out LSB. If the LSB is 0, continue shifting to the 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 with the same processing for the next data byte, 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 high 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) ' XOR each data 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 'Shift the least significant byte to the right and pad with 1 at the beginning.'

End If

If ((UseLo And &H1) = &H1) Then ''If LSB 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 the Design of the Lower-Level Machine Working System

The lower-level machine typically uses a microcontroller 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 unique. Furthermore, it's crucial that the communication baud rate matches that of the upper-level machine. In lower-level machine software development, the Modbus protocol's communication frame structure must be followed to parse commands transmitted from the upper-level machine: non-calling objects should promptly resume their waiting-to-receive state; responding lower-level machines should parse the command code and perform corresponding functional processing, and should be able to send error messages for illegal command operations.

In specific engineering development, the 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 part that every industrial system designer should not 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 must be carefully considered. If conditions permit, chips with shock resistance and large cascading drive capabilities should be selected. Theoretically, the cascading capability of RS-485 chips 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; 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 selected as much as possible to improve drive capability and stability. In addition, the appropriateness of noise reduction, isolation, wiring, shielding, and other measures in the industrial environment will also affect the final stability of the system.

5. Conclusion

In the development of a practical measurement and control system project, the author developed upper-level 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 lower-level 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 development 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; Metrology Technology; 2002. No. 4; P35-36

Read next

CATDOLL 123CM Milana TPE

Height: 123cm Weight: 23kg Shoulder Width: 32cm Bust/Waist/Hip: 61/54/70cm Oral Depth: 3-5cm Vaginal Depth: 3-15cm Anal...

Articles 2026-02-22