PLC network communication control based on Winsock control
2026-04-06 02:42:01··#1
Abstract: This paper introduces a communication program between the Socket control of Visual Basic and OMRON PLC, and successfully applies it to the automatic control system. It mainly realizes the Ethernet communication between the host computer and OMRON PLC, and describes the communication protocol between them. The design method of the communication program introduced in this paper has certain reference value. Keywords: Programmable logic controller; Ethernet; FINS; Winsock control In industrial control, PLC-controlled projects generally use RS-232/RS-485 serial communication for communication between the host and slave computers. This method is difficult to meet the communication needs of control systems with large data volume, long communication distance and high real-time requirements. In recent years, with the rapid development of computer network technology, networked numerical control has become an inevitable trend in the development of modern manufacturing industry. Control systems are developing towards virtualization, networking, integration, distribution and node intelligence. [1] Many large PLC manufacturers have equipped their PLCs with corresponding Ethernet communication modules. This paper discusses the Ethernet communication architecture of OMRON PLC and takes the ENT2l Ethernet module of CP1H PLC as an example to realize communication with the computer. 1. Winsock Network Communication Control: The Winsock control is a non-visual control that provides a very simple way to access TCP and UDP network services. Programmers developing client/server applications do not need to understand the details of TCP or call low-level Winsock API functions; they can directly connect to a remote computer and achieve bidirectional data exchange simply by setting the Winsock control's properties and calling its methods. Winsock primarily supports two types of sockets: ① Stream Socket (also known as connection-oriented): This method corresponds to the TCP protocol. Its transmission characteristics are high reliability, ensuring reliable, ordered, and non-duplicated data transmission. It provides bidirectional data streams, and data is treated as a byte stream with no length limit. ② Datagram Socket (also known as connectionless): This method corresponds to the UDP protocol. This method does not provide correct, ordered, or non-duplicated data transmission because it supports record-oriented data streams. Therefore, transmitted data may be lost or duplicated, and the receiving order may be disordered; the message length is limited. Considering the high requirements for communication reliability and correctness in this system, a stream socket method is selected. The stream socket communication process based on the Client/Server mode is shown in Figure 1. [align=center] Figure 1 Timing diagram of stream socket process communication[/align] 2. Setting up the Ethernet network communication unit When building a network, depending on the network type, each node in the network needs to install a corresponding communication unit. An Ethernet network communication module, such as the OMRON CJ1W-ETN21 Ethernet module, needs to be installed on the PLC. Before application, necessary network settings must be performed, including switch settings and CPU bus unit system settings. The switch settings mainly include the following: Determining the memory working area (CIO area, DM area) allocated to the CJ1W-ETN21 unit. This address is located in the CPU bus area, and the unit number of the ETN unit is determined by the UNIT No. switch, ranging from 0 to F; the NODE No. rotary switch sets two sets of hexadecimal numbers as the node number of the ETN unit in the network, ranging from 01 to 7E; the IP address sets the network number and host node number, consisting of 32-bit binary numbers, represented in four segments using decimal numbers. CPU bus unit settings are mainly configured through programming devices such as CX-Programmer software or a programmer, setting items such as network unit mode, local IP address, subnet mask, FINS port number, FTP login name and password, and IP router table. If using the FINS/TCP protocol, some parameters of the FINS/TCP item also need to be modified in the Ethernet unit settings, such as the automatically assigned FINS node number and whether to keep it active. 3. Communication Protocols for Host Computers As shown in Figure 2, the Ethernet layered model consists of the Physical Layer, Internet Layer, Transport Layer, and Application Layer. The Transport Layer can use connectionless UDP or connection-oriented TCP; the Application Layer uses the FINS (Factory Interface Network Service) protocol, a command response system developed by OMRON for factory automation control networks. It mainly specifies the methods for reading and writing data to PLC storage space. The communication method where the Application Layer uses the FINS protocol and the Transport Layer uses the TCP protocol is called the FINS/TCP method. [align=center] Figure 2 Layered Network Structure[/align] The FINS protocol includes a command system and a response system. Its command frame consists of a FINS header, command code, response code, and body. Commands and responses issued from the host computer must conform to the following frame format requirements and provide appropriate FINS header information. [3-4] FINS communication service is implemented through the exchange of FINS command frames and their corresponding response frames. The FINS command/response frame format is shown in Figure 3. The FINS/TCP header specifies five commands for communication between the client (host computer) and the server (PLC): (1) Send client node address; (2) Send server node address; (3) Send Fins frame; (4) Send error notification via Fins frame; (5) Client and server confirm connection. [align=center] Figure 3 FINS command/response frame format[/align] 4. Specific Implementation of the Communication Program After creating a new VB project, you need to execute the "Project/Components" command in the VB toolbar to add the Winsock control to the project and name it "WskClient". The program uses the TCP/IP protocol for communication, and its main attributes are set as follows: With WskClient .Protocol = sckTCPProtocol 'Uses TCP/IP protocol.LocalPort = 9600 'Local computer port number.RemoteHost = txtIP.Text 'Get the IP address of the remote PLC.RemotePort = txtPort.Text 'Remote PLC port number.Bind 9600 'Specify the local port to use End With After initialization, a connection request is sent to the PLC. After the PLC accepts the request and sends a response, the client program establishes the information frames to be sent according to various frame formats, and can then conduct bidirectional data exchange with the PLC. During this process, a retransmission mechanism can be established to enhance communication reliability in case of transmission failure. First, a "handshake information" instruction (20 bytes) is established and sent, specifying the client node number; when the computer receives the PLC return frame (24 bytes), it checks whether the PLC has received the command and obtains the server and client node numbers. When the computer receives data returned by the PLC, a DataArrival event is generated. The BytesTotal parameter contains the number of bytes of data received. Within the DataArrival event, the GetData method can be called to receive data. If a Close event is received, the Close method is used to close the connection. Additionally, the Winsock State property can be used to reflect the current TCP/IP connection status. The main procedures are listed below: 'Request a connection to the server WskClient.Connect TimeDelay 100 Do DoEvents Loop Until WskClient.state=sckConnected 'Establish and send the FINS command frame Private Sub SendData_Click() ReDim SendData (19) As Byte SendData (0) = &H46 'The first byte of the FINS command frame header... WskClient.SendData SendData() 'Send the FINS command frame End Sub 'Receive the PLC response frame and analyze the data Private Sub WskClient_DataArrival(ByVal bytesTotal As Long) Dim i As Integer ReDim ArriveData(bytesTotal) As Byte wsk.GetData ArriveData, vbArray + vbByte, bytesTotal 'Receive the data and store it in the ArriveData array For i = 0 To bytesTotal - 1 txtArData.Text = txtArData.Text & " " & ArriveData (i) Next i ……'Other data processing If ArriveData(7) <> 16 Then MsgBox "Received information lost" ElseIf SendData(19) = ArriveData(bytesTotal-5) Then MsgBox "Node address error" End If End If After receiving the information, when the PLC receives the transmitted information, it will send back the corresponding command feedback value. The program in this event program will display it in the text box and can be further processed. The main program flowchart is shown in Figure 4. [align=center] Figure 4 Program Flowchart[/align] If the UDP protocol is used, the basic communication process is the same as TCP, except that a connection does not need to be established. In addition, the UDP application can be a client or a server, and it is not necessary to establish client and server programs separately like the TCP application. Since UDP does not need to establish a connection between the client and the server before transmitting datagrams, and there is no timeout retransmission mechanism, the transmission speed is very fast. Therefore, if there are not many devices in the network and the amount of data sent is not large, the UDP protocol, which consumes less computer "resources", can be selected for communication. 5. Conclusion The upper computer Ethernet communication program implemented with Winsock control has been successfully applied to the digital sample drawing machine monitoring system. This method is simple and practical. Without additional investment, it can realize the function of real-time network data monitoring of workshop equipment and achieve the ideal effect. Moreover, with VB as the development platform for monitoring software, the secondary development of the software is unrestricted, saving costs and the program can be upgraded at any time as needed. In order to realize effective information management and monitoring of the control system, the PLC control system based on Ethernet will have a wider range of applications. The discussion in this paper provides a certain reference value for solving such problems. References: [1] Li Bingyu, Xiao Yunshi, Application and Development Trend of Ethernet in Network Control System, Microcomputer and Application, No. 11, 2002, pp. 35-37, 56 [2] CJ1W-ETN21 (100Base-TX) Ethernet Units Construction of Applications OPERATION MANUAL, OMRON, 2003. [3] SYSMAC cs/cJ Series Communications Commands REFERENCE MANUL, OMRON, 2003.