1 Introduction
In modern control systems, the PC acts as the main computer responsible for system management, status monitoring, data processing, and report generation. Meanwhile, the PLC serves as the lower-level device that handles real-time control. This configuration forms a typical hierarchical structure in automation systems. Therefore, the communication between the PC and the PLC becomes a critical factor in determining whether the system can be successfully implemented. Serial communication is widely used due to its simplicity in wiring and ease of implementation.
Visual Basic (VB) is a programming language known for its user-friendly interface and powerful capabilities. It can efficiently handle serial communication between the PC and the PLC. This article uses Omron's CPM2A PLC as a case study to demonstrate how VB6.0 can be utilized to establish serial communication between the PC and the PLC.
Omron’s CPM2A PLC supports the Host-link communication protocol, which allows it to communicate with the host computer. This protocol enables the upper PC to monitor the lower PC in real time, facilitating efficient control and management within the system.
2 Principle Design
2.1 System Structure
The system architecture is illustrated in Figure 1. In this setup, the PC functions as the host computer, while the PLC acts as the lower-level device. Since the Omron CPM2A PLC has an RS-232 serial port built-in, it can be connected to the PC's COM1 or COM2 port via an RS-232 cable, forming a simple 1:1 communication system.
Figure 1: System Structure
2.2 Host-link Communication Protocol
The Host-link communication system from Omron consists of a host computer (such as an IBM PC or compatible machine) connected to multiple PLCs through Host-link units installed on each PLC. The upper computer manages and monitors all connected PLCs. Through communication with the Host-link unit, users can edit or modify the program of each PLC and monitor its operation in real time, enabling distributed control in automated systems. For smaller PLCs, communication can also be established directly via the RS-232C port.
In this system, the Host-link communication protocol is used. The host computer always takes the initiative to issue commands, initiating the communication process. The Host-link unit receives the command, executes it on the PLC, and then sends back the result to the host computer. Data exchange occurs in a round-robin fashion. A group of transmitted data during communication is referred to as a "block," which is the basic unit of a command or response. The block sent from the host computer to the Host-link unit is called a command block, while the one sent back is called a response block.
In multi-point communication, the maximum number of characters that can be sent in a single frame is 131. If a block contains more than 131 characters, it must be divided into multiple frames. Each data block starts with a device number and a title, ends with a checksum (FCS), and a terminator. Additionally, a response code indicating the execution result is included in the response block. The communication format is shown in Figure 2.
Figure 2: Communication Frame Format
3 Software Implementation
3.1 MSComm Control Introduction
MSComm is a Microsoft Communications Control, an ActiveX control provided by Microsoft to simplify serial communication programming under Windows. It offers a straightforward way for applications to send and receive data over a serial interface.
The MSComm control supports two communication modes: query mode and event-driven mode. This article uses the event-driven mode. There are various properties associated with the MSComm control, some of which need to be configured during programming, while others use default values. Table 1 outlines the common properties of the MSComm control.
3.2 PLC Parameter Settings
The PLC communication parameters can be set to the default Host-link communication mode, which includes 1 start bit, 7 data bits, even parity, 2 stop bits, and a baud rate of 9600. The device number is set to 00.
3.3 PC Software Design
Using the MSComm control in Visual Basic, we can develop a PLC monitoring program that communicates through the RS-232 port. The key code is as follows:
'Program initialization and set serial port parameters
Private Sub Form_Load()
MSComm.CommPort = 1
MSComm.Settings = "9600,e,7,2"
MSComm.InputMode = comInputModeText
MSComm.PortOpen = True
End Sub
'Write data to PLC'
Private Sub ButWrite_Click()
If MSComm.PortOpen = False Then
MSComm.PortOpen = True
End If
PlcEnd = "@00SC00"
MSComm.Output = PlcEnd + FCS(PlcEnd) + "*" + Chr$(13)
MSComm.InBufferCount = 0
Dim OutString As String
OutString = "@00WR0010" & Trim(TxtContent.Text)
OutString = OutString + FCS(OutString) + "*" + Chr$(13)
MSComm.InBufferCount = 0
MSComm.Output = OutString
End Sub
'Read data from PLC'
Private Sub ButRead_Click()
If MSComm.PortOpen = False Then
MSComm.PortOpen = True
End If
Dim OutString, InString As String
OutString = "@00RR00100001"
OutString = OutString + FCS(OutString) + "*" + Chr$(13)
MSComm.InBufferCount = 0
MSComm.Output = OutString
Dim i As Integer
Do While MSComm.InBufferCount <= 14 Or i > 9000
i = i + 1
Loop
If i < 9000 Then
InString = MSComm.Input
TxtContent.Text = "Output 010 channel data is " & Mid(InString, 8, 4)
Else
MsgBox("Read failed")
End If
End Sub
'FCS Check'
Function FCS(ByVal FCSStr As String) As String
Dim SLen, i, XorResult As Integer
Dim TempFCS As String
SLen = Len(FCSStr)
XorResult = 0
For i = 1 To SLen
XorResult = XorResult Xor Asc(Mid$(FCSStr, i, 1))
Next i
TempFCS = Hex$(XorResult)
If Len(TempFCS) = 1 Then TempFCS = "0" & TempFCS
FCS = TempFCS
End Function
4 Conclusion
This paper presents a practical approach to developing communication programs using VB (mainly the MSComm control) to enable communication between a PLC and a PC through a standard serial port. Experimental results show that the program runs stably and reliably, with a simple and intuitive operation. With minor modifications, the program can be adapted for other industrial control devices, making it highly applicable in small-scale industrial control systems.
Lcd Integrated Display,Lcd Panel Indoor Display,Good Angle Lcd Segment Display,Va Lcd Panel Display
Wuxi Ark Technology Electronic Co.,Ltd. , https://www.arkledcn.com