Share this

Implementing microcomputer monitoring functions through communication between Siemens PLC and PC based on VB6.0.

2026-04-06 06:00:15 · · #1
1. Introduction A Programmable Controller (PLC) is a new type of control device based on a microprocessor, representing a novel product that applies computer technology to the field of industrial control. It integrates logic operations, sequential control, timing, counting, and arithmetic operations. Furthermore, it can communicate and network with other computers, including data exchange between different PLCs and communication between the PLC and the computer. This article describes how the author uses VB6.0 to implement communication between the PLC and a host computer, enabling operators to monitor and control the operation of remote equipment through the generator distribution panel display. 2. System Composition This system uses a PLC to acquire analog signals and process data, uploading it to the computer via a serial communication port. The computer analyzes and classifies the transmitted data, records it in a database, displays it in real-time on the distribution panel, and provides data query services. The PLC is an S7-224, and the serial communication interface uses RS-485 signal lines. This signal line is half-duplex, meaning it cannot receive and transmit simultaneously. By placing the PLC in run mode, the data and signals stored in the PLC's transmit buffer can be sent to the host computer's serial port. The host computer receives this data via a serial communication program, displays it, and records the data to the database through the binding of the ADODC database control, thus completing data sampling. The data to be collected in this project includes daily quantities, fault quantities, and equipment action quantities. For daily quantities, the data needs to be displayed on the main control panel of the power distribution panel every moment so that staff can observe real-time data. However, it is not necessary to record all daily quantities into the database; only periodic recording is required (here, recording every half hour). For fault quantities and equipment action quantities, it is required that whenever a fault or action occurs in the equipment, it be recorded in the database for staff to query. 3. S7-200 Communication Methods and Parameter Settings: The S7-200 has powerful communication functions, offering multiple communication modes for users to choose from, including single-master mode, multi-master mode, and remote communication using a modem. This project selects the PC/PPI cable, a communication hardware supported by STEP7-Micro/Win32, which supports baud rates of 9.6kbps and 19.2kbps and the PPI protocol (Point-to-Point Interface Protocol) based on the Open Systems Interconnection (OSI) model. PPI is a master/slave protocol; S7-200 CPUs on the network are slaves, while other CPUs, SIMATIC programmers, or TD200s are masters. The S7-200 CPU's communication port uses the RS-485 standard connection. The PC/PPI cable connects the S7-200 computers. Using commands such as receive interrupts, transmit interrupts, and character interrupts, the free port communication can control the operating mode of the S7-200 CPU's communication port. This project uses free port mode, allowing communication port 0 to use free port mode when the CPU is in run mode. By setting the SMB30 value, we can select the baud rate, parity, data bits per character, and protocol. When the CPU is in stop mode, free port communication is stopped, and the communication port is forcibly switched to PPI protocol mode. At this time, PLC programs can be input into the CPU, enabling programming and control of the PLC by the programming software. The communication process here uses a master-slave method, with the computer as the master and the PLC as the slave. Only after the master computer sends a request message will the slave PLC return the data from its transmit buffer to the PC. 4. PLC Communication in Free Port Mode Since this project uses a PC/PPI cable, the cable switching time must be considered in the PLC program. The delay time from when the PLC receives a request message to when it returns the data from its transmit buffer must not be less than the cable switching time. At a baud rate of 9600bps, the cable switching time is 2ms. In this project's program, a timer interrupt is used to implement the switching delay. Timer interrupt subroutine: network title 'Timer interrupt subroutine int_2 ld sm0.0 dtch 10 xmt vb200, 0' When the program reaches the statement "Start timer interrupt" (i.e., `atch int_2, 10`), it will jump to the interrupt subroutine shown above. `sm0.0` is a special memory location; this bit is always 1, meaning this switch is active as long as the PLC is in run mode. The statement `xmt vb200, 0` is used to send data to the serial port. The data sent is stored in a contiguous memory space starting at address `vb200`, and is sent through port 0. Furthermore, to improve communication reliability, we need to use XOR verification. Both communicating parties perform an XOR check on each character of each frame and compare the two. If they are different, a communication error can be determined. network1 network title 'Subroutine for XOR checksum fcs ld sm0.0 movb 0, #xorc bti #numb, #numi for #temp1, +1, #numi movb 0, #xorc 'Used to clear the XOR checksum, and use bti#numb, #numi to convert the number of bytes to an integer network2 ld sm0.0 xorb *#pnt, #xorc incd #pnt network3 next The for to next statement specifies a loop that performs XOR operations from the first character of this frame (excluding the start character) to the last character of the frame. By calling this subroutine, the XOR checksum of the characters in the specified data storage space can be calculated. If there is also a subroutine for calculating the checksum in the VB program on the host computer, then the checksums calculated by these two subroutines can be compared to see if they are the same. This subroutine can be called using the statement (call fcs, &vb102, vb99, vb90), where &vb102 specifies the starting address for the checksum verification performed by the subroutine fcs, vb99 stores the number of bytes to be XORed, and vb90 stores the calculated checksum. Assuming the checksum received from the host computer is stored in vb91, comparing the data in vb90 and vb91 verifies the correctness of the communication. Similarly, we can use this subroutine to calculate the checksum of the data sent from the PLC, send it to a specific address on the host computer, and compare the two sets of data on the host computer (the VB program for calculating the XOR checksum on the host computer will be given later). Because the start and end characters of a message are only 8 bits long, there's a high probability that the received message data area will contain characters identical to the start and end characters. This could lead to confusion between the data characters and the start or end characters. To avoid this, the data can be processed before sending by converting the data characters to BCD code. The S7-200 provides a dedicated instruction for converting integers to BCD code. The statement `ibcd vw200` converts the input integer to BCD code, and the result is sent to `vw200`. The host computer then needs to convert it back to an integer. Additionally, during initialization, pay attention to the settings of the free port communication protocol. Ensure that the baud rate, data bits, parity, and stop bits during communication are the same as the parameters set on the host computer's VB and the communication line. Otherwise, communication will fail; either no data will be received, or an "index out of bounds" error will occur. 5. The communication part of the host computer is programmed using VB language. The communication interface of the host computer is programmed using VB language. VB has a dedicated control, Microsoft Comm Control (MSComm control), for providing serial communication. During programming, serial communication can be achieved by simply setting the properties of the MSComm control. The MSComm control is generally not included in the commonly used toolbar. Right-click in the blank area of ​​the right-hand toolbar, select the "Components" option, and the Components dialog box will appear. Under the "Controls" tab, you will find Microsoft Comm Control 6.0. Select it and click "OK" to add this control to the right-hand toolbar. The icon is [icon image missing]. Drag this icon onto the form to set its properties. Among the many properties of this control, the only ones that need to be redefined are: commport (the communication port number for data transmission), settings (setting the baud rate, parity, data bits, and stop bits for data transmission; note that this parameter must be consistent with the parameters set in the PLC communication program), portopen (setting the status of the communication port), and inputmode (the format for reading the receive buffer). This project uses an event-driven approach to handle received information, that is, the rthreshold attribute is set to a non-zero value. At this time, a serial port event oncomm will be generated whenever the received character or transmission line changes. These communication events can be captured by querying the commevent attribute. The program and comments for implementing XOR calculation in VB are as follows: nbyte(1) = ubound(sdata) + 1 'Number of bytes to send data area fcs = nbyte(1) 'Check code initialization for i = 2 to ubound(sdata) + 2 nbyte(i) = sdata(i - 2) 'Data area of ​​the data packet array to be sent fcs = fcs xor nbyte(i) 'XOR operation, calculate check code next nbyte(i) = fcs 'Send check code Where sdata is the variable name of the data array to be sent. The for-next statement implements the XOR operation on each bit of the data to be sent. In this way, the XOR check code of the sent data can be sent to the PLC. The PLC compares whether the two calculated check codes are the same, thereby improving the reliability of communication. In this project, the part where the computer sends control commands downwards is written as a user-defined function `send(xi)` (where `i` ranges from 1 to 6). This function is called at each button that needs to send a control command, with the parameters in `send` set to different control numbers. [align=center] Figure 1 Equipment Action Status Receiver Table[/align] Figure 1 shows a simulated control panel for a generator power distribution system. This software can collect, display, and store real-time signals such as three-phase output voltage, current, and power factor, and collect the equipment's action status and fault conditions into a database. Above this control panel is a series of control buttons. Clicking a button sends a corresponding control command to the lower-level computer, controlling its actions. During communication, since the computer is the master and the PLC is the slave, the PLC only sends data after receiving information from the computer. The number of times the computer sends data does not correspond one-to-one with the number of times it receives data. Each time the computer clicks the button to send a control command (see Figure 1), the PLC transmits all the data stored in its buffer to the computer. Each `oncomm` event can only transmit 8 bytes. Therefore, if the PLC's send buffer contains 50 bytes of data, each data transmission request will trigger 7 `oncomm` events. Furthermore, each `oncomm` event will continue executing until `end sub`. This can be verified by adding the test statement "debug.print 'start'" to the data sending program and then adding the test statement "debug.print 'end'" before `end sub`. We can clearly see 7 `end` statements after each `start` in the immediate window. 6. Controls Bound to the Database The ultimate goal of data collection is not only to display the data (e.g., daily quantities) on the interface but also to record the data in the database for future queries. Displaying the data is easy to implement; it simply involves retrieving the contents of the receive buffer in a pre-defined order. To record data into the database, a database binding control is needed. In this project, the ADODC control is used. This control is not included in the commonly used toolbar. Following the method for adding the MSComm control, the Microsoft ADO Data Control 6.0 (SP4) (OLEDB) toolbar should be added to the toolbar. Then, this control can be dragged into the form (in this work, the ADODC control is placed in the query screen that is not displayed during program execution, so that the recording and querying can be completed simultaneously). By setting the connectionstring (required to connect to the address stored in the pre-created database), commandtype (since data will be written to the database, it should be set to 2-adcmdtable), and recordsource (if the specified database contains multiple tables, select the table corresponding to this ADODC1) properties (other properties can use default values), the binding is complete. When programming, the following statements can be used to organize the passed information and write it into the database: if rcv(5) then with adodc1 .recordset.addnew .recordset(“device name”) = “circuit breaker 1” .recordset(“action status”) = “closed” .recordset(“action time”) = now .recordset.update end with end if Among them, “device name”, “action status” and “action time” are the field names of the bound database. The value after the equal sign is the field value added to the corresponding field of the bound database when the condition statement is met. In this project, all the data received from the receiving buffer is temporarily placed in the array rv, and rv(5) contains the action information of whether circuit breaker 1 is closed. It is agreed that rv(5) = 1 in the data sent by the PLC means that circuit breaker 1 is closed. Therefore, the above statement is given. Other action status records, fault status records and daily quantity records are almost the same as the above program. However, since the daily quantity does not need to be recorded in the database every moment, the static variable count is used to time when the daily quantity is written to the database. If the value of count increases by 1 for every 1 second, and it is required to be recorded once every half hour, then count mod 1800 = 0 can be used to achieve this time control. 7 Conclusion The article lists the issues that the author thinks need to pay attention to in the process of writing the PLC and computer communication program, and gives the program code that the author thinks is difficult. This project has successfully passed the test and has been delivered for use. References [1] Luo Chaosheng et al. Visual Basic 6.0 Programming Tutorial [M]. Beijing: People's Posts and Telecommunications Press, 2002. [2] Liao Changchu. PLC Programming and Application [M]. Beijing: Machinery Industry Press, 2002. About the Author Guo Chunchun (1982-) Female is currently a master's student in the School of Electrical Engineering, Wuhan University, specializing in power electronics and automatic control.
Read next

CATDOLL Tami 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