Research on serial communication method between configuration software and lower-level machine using VB
2026-04-06 07:40:01··#1
Abstract: Based on DDE (Dynamic Data Exchange) technology, this paper studies the implementation method of serial communication service program between configuration software and lower-level machine under VB6.0, and improves it to solve the problem that the configuration software does not provide communication drivers for certain field devices. The service program uses the serial communication control MSComm to communicate with the lower-level machine, and simultaneously uses DDE technology to exchange data with the configuration software. Engineering practice shows that this method is highly versatile and easy to implement. 1. Introduction In the field of industrial control, configuration software is being used more and more widely. For example, Fix, InTouch, and KingView are all excellent representatives of configuration software. They provide rich industrial control interfaces, database processing, object connection and other data management and control functions, bringing great convenience to users. Configuration software uses the Windows system as the operating platform and has the characteristics of complete graphical functions, good interface consistency and ease of learning and use. Compared with the industrial control systems developed using dedicated machines in the past, it is more versatile and more convenient for engineering technicians to apply and develop. However, in practical applications, configuration software often fails to provide communication drivers for some field devices, most of which communicate with the PC via serial ports. The MSComm serial communication control provided by VB allows developers to easily and quickly develop serial communication programs. Implementing a DDE-compliant communication program in the Windows environment using VB is also not difficult. Therefore, the data flow diagram of the entire system is shown in Figure 1. This article takes the communication between KingSCADA software and the PHILIPS 51LPC series microcontroller as an example to study the principle and implementation method of developing a serial communication service program using VB6.0. Figure 1 System data flow diagram 2. Implementation methods of serial communication There are two main methods for implementing serial communication using VB on the Windows platform: 1) Using Windows API (Application Program Interface) functions. This method allows for writing highly portable communication programs, but it requires first declaring the dynamic link library (DLL) to be used in VB using Declare. This requires a deep understanding of Windows API functions and is relatively complex to program; 2) Using the ActiveX control MSComm provided by Microsoft. This communication control facilitates serial communication between user applications by modifying object properties, sending messages to objects, and writing response code for object events. It achieves all the functionalities of API functions while improving programming efficiency, enhancing application capabilities, and maintaining program simplicity and clarity. Many documents discuss the operation of the MSComm control for serial communication; the main properties of this control can be found in relevant literature and will not be detailed here. It is important to emphasize that a Variant type variable must be used as an intermediary during both data sending and receiving. When sending data, the data to be sent must first be assigned to a Variant type variable, and then the Variant variable must be assigned to the MSComm's Output property. Similarly, when receiving data, the MSComm's Input property should first be assigned to a Variant variable. Once received, the receiving end must convert the data into other types (such as character or binary) before processing. The main program for sending data via serial port in the VB service program is as follows: Private Sub TxData() Dim Data As Byte Dim Temp1 As Variant Do DoEvents 'Relinquish CPU control Loop Until Not MSComm1.PortOpen If (Not MSComml.PortOpen) then MSComml.PortOpen=True End if MSComml.CommPort=1 ;Use COM1 port MSComml.Setting="9600,N,8,1" ;9600 baud rate, no parity, 8 data bits, 1 stop bit Temp1=Data;Use Temp1 as an intermediary MSComml.OutBufferCount=0 ;Clear the send buffer MSComml.Output=Temp1 ;Send the packaged data…… MSComml.PortOpen=False ;Close the serial port End Sub The lower-level microcontroller uses the PHILIPS 51LPC series P87LPC769, whose basic structure and assembly instructions are compatible with the 80C51 system. It has 128B of internal RAM and 4KB of OTP program memory. The serial port operates in mode 1, with data frame information including 1 start bit, 8 data bits, and 1 stop bit. Typically, to improve system anti-interference, load capacity, and increase communication distance, an RS-485 communication bus in half-duplex mode is used. The PC's serial port uses an RS-232/485 converter, and the microcontroller's RXD and TXD pins are supplemented with SN75175 and SN75174. Due to potential communication interference in the field, if communication fails, the system will resend data and count the number of failures. If the number of failures exceeds a specified limit, communication is considered a failure. Furthermore, while waiting for a response from the lower-level microcontroller, the host needs to use the DoEvents function to relinquish CPU control to avoid affecting the normal operation of the configuration software. 3. DDE Communication with Configuration Software In the Windows environment, applications and the system, as well as applications themselves, can easily exchange data in real time through the clipboard, Dynamic Link Libraries (DLLs), Object Link Embedded (OLEs), and Dynamic Data Exchange (DDE). Among these, the clipboard is a static data exchange method, DLLs are not supported by most configuration software, and OLEs are unsuitable for serial communication. In contrast, DDE is a simple, efficient, and widely supported data exchange method by configuration software. Dynamic data exchange refers to real-time dynamic data exchange between applications within the operating system environment. Once the data provider changes the content of the exchanged data, the receiving client will immediately and automatically update the exchanged data, effectively ensuring data transmission consistency. The party initiating the exchange request is called the client, and the party responding to the exchange request or providing services is called the server. The serial communication service program provides serial port data services to the configuration software; it is the server side, also known as the sender. The configuration software sends requests to the communication service program and receives the data it sends; it is the client. DDE communication between Windows applications is defined by three identifiers: application name, topic, and item. The application name is the name of both parties in the DDE communication; in KingView, the application name is "View," and in VB, it's the name of the executable file. The topic is the data domain being discussed; in KingView, the topic is defined as "TagName," while in VB, the topic is specified by the LinkTopic property value of the form. The item is the specific data object being discussed; in KingView, the item name is specified along with the I/O variables in the data dictionary, while in VB, the item is the name of a specific text box, label, or picture box. To establish a DDE connection between the two, you first need to define the DDE device in KingSCADA, including its service program name, topic name, and data exchange mode. In KingSCADA's data dictionary, create I/O variables according to the number and types of variables that need to be communicated, and define the connection device and item name for these variables. Then, in the VB program, set the LinkMode property of the main form to 1; the LinkTopic can be arbitrarily given, such as FormDDE. Additionally, you need to define the control's properties and methods, setting the LinkTopic, LinkItem, and LinkMode properties. A typical setting is: Control.LinkTopic = Server Program Name | Topic Name; Control.LinkItem = Item Name; Control.LinkMode = 0, 1, 2, 3. Where: 0 = Disable DDE, 1 = Hot Link, 2 = Cold Link, 3 = Notify Link; Control is the name of the text box, label box, or picture box. In this VB service program, the Text box control is used for DDE communication. The main code for setting it is: Text1.LinkMode = 1 - Source; Text1.LinkTopic = View | TagName. After these DDE definitions are set for both sides, running the application in the order of Server first, then Client (i.e., starting the VB service program first, then starting the KingSCADA runtime program) will enable DDE communication between VB and KingSCADA. The following is a routine for sending serial port data to the configuration software: `Private Sub MSComml_OnComm() Dim Temp2 As Variant Select Case MSComm1.CommEvent Case comEvReceive ; Serial port received data MSComml.InputLen=1 ; Read one byte Temp2=MSComml.Input ; Read data Text1.Text=CLng(Temp2) ; Send data to configuration software Case … ; Other event handling…… End Select End Sub` 4. Improvement Method for DDE Communication In practical applications, considering the limited data processing capabilities of industrial control software, the conversion of communication protocols should be handled by the communication service program. The serial communication service program extracts each instruction or data according to the protocol and then uploads these data and instructions to the industrial control software simultaneously. When the industrial control software sends serial port data, it also sends all data or instructions to the communication service program simultaneously, and the communication service program performs data packet transmission. Therefore, multiple Text controls need to be created in VB. Each control should always store one or more bytes at a fixed position in the communication frame data segment, as shown in Figure 2. Figure 2: Schematic diagram of the Text control and its corresponding data frame. To make the communication program portable, a dynamic loading method for controls can be used. That is, when the communication program starts, the corresponding number of Text controls are loaded according to the set frame length. However, when using a variable-length frame communication protocol, the frame length set by the communication service program should not be less than the maximum possible frame length. To achieve dynamic loading of Text controls, a control array needs to be created. This requires adding two Text controls with the Visible property set to False to the main window of the communication service program, named TextRe and TextTr respectively. After setting the Index property of both controls to 0, the Text controls can be dynamically added in the program using the Load method. The program for dynamically loading a Text control is as follows: `Private Sub Form_Load() Dim k As Integer Me.LinkMode=0 ; Disallow DDE communication…… If DataLen>2 Then ; DataLen stores the specified frame length For k=1 To DataLen/2-1 ; Each instruction occupies 2 bytes Load TextRe(k) Load TextTr(k) Next k End If Me.LinkMode=1 ; Allow DDE communication End Sub` 5. Conclusion VB is a mature, object-oriented programming language. Serial communication software between a PC and a lower-level machine in a Windows environment, written using VB, is characterized by its ease of implementation and strong versatility, reducing the workload of software developers. This paper utilizes VB's serial communication control MSComm and employs DDE technology to effectively implement bidirectional communication between the KingSCADA software on the upper PC and the lower-level microcontroller. The upper-level human-machine interface written in the KingSCADA software is intuitive and user-friendly. Actual operation proves that this serial communication service program is convenient, reliable, and has strong practical value.