Implement PC monitoring of multiple PLCs using VB6.0
2026-04-06 06:58:55··#1
1 Introduction With the increasing maturity of computer communication technology and the increasing demands of enterprises for industrial automation, automatic control systems are evolving from traditional centralized control to multi-level distributed control. Therefore, the PLC constituting the control system must possess communication and networking capabilities. In practical applications, while human-machine interfaces or configuration software on the market offer rich functionality for monitoring the internal data and operating status of the PLC, they are generally expensive, especially in small and medium-sized production settings. Therefore, many enterprises hope to develop a simple and practical communication program using a high-level language. Distributed monitoring can be easily achieved through the object-oriented visual programming language VB6.0. Lower-level computers such as PLCs control the production process, while local computers perform real-time monitoring or participate in controlling parameters on the production site. This paper studies the communication principles and methods between the OMRON CPM1A minicomputer and the upper-level computer, introducing how to use VB6.0 to achieve real-time monitoring of the PLC by the upper-level computer, achieving good results. 2 Monitoring Principles 2.1 Communication Method For the upper-level computer, as the master station, to monitor the status of lower-level devices through the PLC, communication between the upper-level computer and the PLC must first be achieved. Serial communication is commonly used between PCs and PLCs due to its advantages such as simple wiring, flexible application, and high reliability. Since most computers have serial ports, serial communication is the preferred method. Serial communication can be implemented using Windows API functions or serial communication controls, but the latter is easier. This article uses VB communication controls. To monitor only one CPM1 APLC, a CPM1-CIFO1 (a dedicated RS232 adapter provided by OMRON) is needed to create an RS232C port for communication with the host computer. To monitor multiple PLCs, an RS232-RS422/RS485 converter (ADAM-4520) and a CPM1-CIF11 (a dedicated RS422 adapter provided by OMRON) are required for communication with the host computer. Up to 32 PLCs can be connected, as shown in Figure 1. [align=center] Figure 1 System Communication Principle[/align] During the communication process, the host computer is always the active party, and the PLC is the passive party. All data exchange can only be carried out between the master station and the slave station. The slave stations cannot directly exchange data. If the slave stations need to exchange data, they must go through the master station as an intermediary. First, the host computer sends an instruction frame to the PLC. After receiving the instruction from the host computer, the PLC performs an FCS check to see if it is correct. If it is correct, it receives and sends a response frame (including the first and last check bytes) to the host computer. Otherwise, the PLC refuses to send data to the host computer. It should be noted that since the PLC has a communication mechanism, it is generally not necessary to program the PLC during the entire communication process. The host computer also needs to judge whether the data sent by the PLC is correct. If it is correct, it receives it and completes a normal communication. Otherwise, the communication needs to be restarted. 2.2 Communication Protocol (1) Communication Condition Design. To ensure that the host computer and the slave computer can exchange data correctly, the following points must be ensured: ● Both parties must keep the baud rate, data bits, stop bits, and parity check consistent during initialization; ● The communication program of the PC must be written strictly in accordance with the communication protocol and frame format of the PLC. (2) Communication command frame design. The general format of the transmission protocol of OMRON's CPMIA small PLC is: (3) Communication response frame design Where @ is the preamble character, indicating the start; the device number is 00-31 (limited to 10 in this example), and a total of 32 station numbers can be set to identify the connected PLC. It can be set through the lower 8 bits of the PLC's DM6653, and the default station number is 00; the command code is the command code specified by the CPMIA small PLC for its operation; the body is the data address and data to be read or written; FCS is the frame check code, used to detect errors in the communication process in time for processing; * and CR indicate the end of the command [2]. The command frame is the frame format transmitted from the PC to the PLC, and the response frame is the frame format transmitted from the PLC to the PC. Except for the data area, the meaning of each part in the command frame and response frame is the same. 2.3 Introduction to MSComm control The MSComm control in VB has a complete serial port data sending and receiving function. The MSComm control has two communication modes: event-driven mode and polling mode [3]. Because the polling mode takes up too much CPU time, we adopt the event-driven mode. To complete the communication, it is necessary to correctly set the relevant properties of the MSComm control. In this design, it is initialized in the form, mainly including port setting, baud rate setting, parity setting, etc. The main properties are shown in Table 1. [align=center]Table 1 MSComm control properties[/align] [b]2 Communication program design[/b] This system takes the monitoring of the traffic light control system based on PLC by the PC as an example. Under the premise that the host computer and PLC communicate normally, the following interface is designed. The content of the form mainly includes four areas: port and station number selection area, time setting input area, monitoring display area, and control button area. By selecting different station numbers (i.e., different PLCs) from the station number selection area, real-time monitoring of multiple PLCs can be achieved. 3.1 Serial port initialization program If MSComm1.PortOpen <> True Then MSComm1.PortOpen=True End If MSComm1.Settings="9600,E,7,2" MSComm1.InputLen=0 MSComm1.InBufferCount=0 MSComm1.InputMode=comInputModeText MSComm1.Handshaking=comNone 3.2 VB program for communication between computer and PLC Public Function ReadData (ByVal InputStr As String, ByVal Num1 As Integer, ByVal Num2 As Integer) As String Dim OutputStr As String Dim InString As String Dim ReturnStr As String Dim EndString As String Dim FCSString As String Dim ReturnFCSString As String Form1.MSComm1.InBufferCount=0 OutputStr=InputStr+FCS(InputStr)+“*” If Form1.MSComm1.PortOpen=True Then Form1.MSComm1.Output=OutputStr+Chr$(13)'Output according to PLC frame format End If Do DoEvents Loop Until Form1.MSComm1.InBufferCount >=Num2'Return frame length InString=Form1.MSComm1.Input EndString=Mid$(InString,6,2)'Get the response code of the returned frame If EndString <> “00”Then ReadData="Error" Exit Function End If EndString=Mid$(InString,1,Len(InString)-4) ReturnFCSString=Mid$(InString, Len(InString) -3,2)'Get the checksum of the returned frame FCSString=FCS(EndString) If FCSString <> ReturnFCSString Then ReadData="Error" Exit Function End If If Len(InString)>=Num2 Then ReturnStr=Mid$(InString, Len(InString)-Num1-3, Num1) 'Retrieves the data from the returned frame, its value is Num1 ReadData = ReturnStr End If End Function 4 Conclusion This paper presents a method for implementing communication between a host computer and multiple PLCs using the MSCOMM control in VB, enabling real-time monitoring of multiple PLCs by the host computer. This significantly reduces communication costs for distributed control systems and has considerable potential for wider application.