Communication between PC and PLC in a traffic light monitoring system
2026-04-06 07:00:40··#1
Abstract : This paper introduces the implementation process of VB and PLC communication. The system uses a PC with VB installed as the host computer and the PLC as the slave computer. A user-defined protocol is created using the MSComm control in VB and the PLC's free port mode. The PC and PLC are connected via a PC/PPI cable to achieve serial communication between the host and slave computers. A specific application of this method in a real-time traffic light monitoring system at an intersection is also presented. Keywords: VB, PLC, monitoring system, traffic lights Abstract : This paper introduces the communication system between VB and PLC. The system uses a PC (VB) as the main machine and the PLC as the submitting machine. The system utilizes MSComm control components of VB, a free communication port of the PLC using a user-defined protocol, and links the PC and PLC via a PC/PPI cable, achieving serial communication between the main machine and the submitting machine. A concrete application is presented in real-time testing and the control system of traffic lights at a crossroads. Key words: VB, PLC, monitor system, traffic lights 1. Introduction With economic development, the number of cars has increased dramatically, and urban roads are becoming increasingly congested. Traffic congestion has become an international problem. Therefore, designing a reliable, safe, and convenient multi-functional traffic light control system is of great practical necessity. Typically, the main drawback of traffic light control systems is the lack of effective emergency measures, leading to traffic obstruction at intersections and causing unnecessary economic losses. Therefore, we consider designing a monitoring system for crossroads to facilitate convenient and rapid traffic light control. Siemens PLCs are widely used due to their low price, adaptability to industrial environments, and powerful networking capabilities. Considering these factors, this system uses a PLC as the lower-level machine. The upper-level machine can use high-level languages such as VC++, VB, and Delphi. Since the functions implemented in this system are relatively simple, and the data processing and management requirements are not too high, VB, which is inexpensive and easy to communicate with, is chosen as the upper-level machine. 2. System Composition [align=center] Figure 1 System Composition[/align] The system composition is shown in Figure 1. The monitoring system mainly consists of a computer and a PLC. A PC/PPI cable connects the computer's RS-232 port and the PLC's RS-485 port as a data converter (using the DIP switch on the PC/PPI cable to configure the correct baud rate). The computer selected for this system is a Pentium(R) 4 CPU; has 40GB of hard disk space; uses the Microsoft Windows XP operating system; and has one COM port. Siemens provides various CPU models to suit different applications. This system uses a Siemens CPU224 S7-200 PLC, which has one RS-485 port, 14 input ports, and 10 output ports, fully meeting the system's requirements (the system requires 8 output ports). 3. Operation Flow and Communication Principles 3.1 Operation Flow a. In the SETP7-Micro/WIN software's operation bar, click the "system block" icon to display the interface shown in Figure 2. [align=center] Figure 2 Configuring S7-200 CPU[/align] b. Select the station address and baud rate for the PLC. c. Click the "communications" icon in the operation bar to display the interface shown in Figure 3. [align=center] Figure 3 Configuring S7-200 CPU[/align] d. On interface 3, click the "Double-click refresh" button on the right sidebar to search for the correct baud rate. e. Download the program to the PLC. f. Put the PLC into RUN mode. g. Open the VB interface, click the "Run" option in the menu, and the monitoring interface shown in Figure 4 will appear. Enter the data to be transmitted in the monitoring interface. h Click the Start button on the monitoring interface in Figure 4 to start communication. 3.2 Communication Principle When the program starts, the VB interface is initialized and the communication port is opened. When the Start button on the VB interface is pressed, a timer is started to collect communication data. The timer collects communication data every 0.1s. At the same time, the receive and send functions are also opened. At this time, the running time of the north-south and east-west traffic lights can be entered in the VB interface. That is, data is entered in VB and sent to PLC. [align=center] Figure 4 Monitoring Interface[/align] When the PLC program starts, the PLC's communication port is initialized and the data sent from VB is received. The PLC determines whether the end character has been received. If the end character has not been received, the PLC will be in the receiving state. If the end character has been received, the PLC will start sending data to VB after a delay of 0.01s. If the receiving is completed for any other reason, a new receiving is started. VB receives the data "1" or "0" sent from the PLC to control the traffic lights to be "on" or "off", and at the same time controls the corresponding vehicles to be "running" or "stopping". When no stop or end button is pressed, VB sends data to the PLC every 0.1 seconds. After receiving the data, the PLC sends it back to VB if the sending conditions are met. This achieves a simulated interface on the computer to monitor the actual road traffic conditions. If an emergency occurs and the vehicle running time needs to be adjusted, the required time can be entered into the computer. 3.4. Introduction to the MSComm Control in VB 4.1 In the Windows environment, the operating system controls various hardware resources through drivers, preventing users from directly performing low-level operations on the serial port as in the DOS environment. Therefore, Visual Basic 6.0 provides a serial communication control, Microsoft Comm Control, or MSComm control for short. Operators can easily achieve serial communication by simply setting and monitoring the properties and events of the MSComm control. 4.2 MSComm Control Properties. .CommPort: Sets and returns the communication port number. .Settings: Sets and returns the baud rate, parity bits, data bits, and stop bits as a string. The characters n, o, and e represent no parity, odd parity, and even parity, respectively. .PortOpen: Sets and returns the communication port status. Setting it to True opens the port; setting it to False closes the port. .Input: Reads data from the receive buffer, type Variant. .Output: Writes data to the send buffer, type string or byte array. .InputMode: Sets the format of the data read from the buffer; setting it to 0 for string format, setting it to 1 for binary format. .InBufferCount: Sets and returns the number of bytes in the receive buffer; setting it to 0 clears the receive buffer. .OutBufferCount: Sets and returns the number of bytes in the send buffer; setting it to 0 clears the send buffer. .Inputlen: Sets and returns the number of bytes read each time Input is called; setting it to 0 reads the entire contents of the receive buffer. .Rthreshold: Represents the minimum number of bytes received by the receive buffer before the serial port event OnComm occurs. Setting it to 0 disables the OnComm event. Generally, it is set to 1, meaning a receive event occurs when the number of bytes in the receive buffer is greater than or equal to 1. .CommEvent: Returns the corresponding OnComm event constant. 4.3 MSComm control handles received information MSComm control provides two handling methods: (1) Event-driven method: When the Rthreshold property is not 0, a serial port event OnComm will be generated when a character is received or the transmission line changes. These communication events can be captured and processed by querying the CommEvent property. (2) Query method: The received information is processed by querying the InputBufferCount (number of bytes in the receive buffer) property value. This paper adopts the event-driven method. 5. Specific implementation of the scheme 5.1 Program design [align=center] Figure 5 VB monitoring system principle[/align] 5.1.1 VB sends data to PLC MSComm1.Output = Text2.Text & Text3.Text & Text4.Text & Text5.Text & Chr (10) 'Use MSComm control to send data to PLC (the text boxes contain the time for controlling the traffic lights to go straight north-south, turn north-south, go straight east-west, and turn east-west respectively) Explanation: Since PLC recognizes the ASCII code sent from VB, the text boxes contain the calculated ASCII code recognized by PLC. Here we use statements such as p = Val(txt1.Text) and Text2.Text = Chr((p - 3) * 10). 5.1.2 VB receives data sent from PLC Private Sub MSComm1_OnComm() If MSComm1.CommEvent = comEvReceive Then 'If the character is received S1 = MSComm1.Input 'Assign the data in the receive buffer to the temporary variable "S1" If (S1 = Chr(&H1B)) And (Len(SCOMS) > 2) Then 'If the data in the receive buffer of variable "S1" receives the start character '&H1B', and the number of bytes in variable "SCOMS" is greater than 2, then SCOMS = "" Clear the data in variable "SCOMS" Else 'Otherwise If S1 <> Chr(&HD) Then 'If the variable "S1" does not receive the end character '&H0D', then SCOMS = SCOMS&S1 'Add the data in "S1" to "SCOMS" Else 'Otherwise…… 'Data processing End sub 'End Text5.text=MSComm1.input xt5.T ' text5.Text stores the data sent by the PLC End Sub 5.2 Lower-level program design 5.2.1 PLC receives data sent from VB (the allocation of the PLC receive buffer is shown in Figure 5) RCV VB100, 0 [align=center] Figure 6 Data allocation of the PLC receive buffer[/align] 5.2.2 PLC sends data to VB (the allocation of the PLC send buffer is shown in Figure 6) MOVB 8, VB400 // Set the number of bytes to be sent to 8, and the send buffer to VB400 MOVB 16#1B, VB401 // Set the start character of the sent information to "16#1B" MOVB 16#0D, VB408 // Set the end character of the sent information to "16#0D" MOVB '1', VB402 NOT MOVB '0', VB402 // If Q0.0 has output, VB402 is "1", otherwise it is "0" XMT VB400, 0 //Send information back to the user on port 0, the send buffer points to VB400 [align=center] Figure 7 Data allocation of the PLC's send buffer[/align] 6. Conclusion Communication with the PLC is achieved through VB. This brings the PLC's operation under the management of a microcomputer. Experiments have verified that the developed VB program runs smoothly and error-free with the PLC under Microsoft Windows XP, effectively addressing traffic conditions at intersections.