Share this

Implementing Ethernet communication between VB and PLC using Winscok controls

2026-04-06 06:38:13 · · #1
Abstract: Industrial Ethernet, with its unique advantages of low cost, high efficiency, high scalability, and high intelligence, is attracting more and more manufacturers in the manufacturing industry. Therefore, implementing I/O modules such as PLCs and gateway services connecting other simple domain buses or I/O modules through the MODBUS/TCP protocol is of great significance. This article discusses the communication problem between PLCs and monitoring computers based on VB. Keywords: VB, MODBUS/TCP protocol, programmable controller, Ethernet 1. Introduction At all layers of the network, as automation and control engineers have more and more opportunities to interact with the network, the desire and need for networking are gradually increasing. Moreover, in monitoring and data acquisition applications, most engineers have applied the Ethernet protocol to Supervisory Control and Data Acquisition (SCADA) systems and factory diagnostics, testing, and maintenance. How to easily achieve interaction with PLCs has become a new battleground for many manufacturers. This has led to the development of human-machine interfaces and various configuration software products. These products simplify the control and operation of PLCs, making PLC applications more convenient. However, they also have common drawbacks: generally high prices and poor secondary development capabilities. VB, as a "primitive" programming language, combines the advantages of both of the above. This article will use a simple case to roughly discuss how to implement Ethernet communication between a PC and a PLC through VB. 2. Basic Concepts of Ethernet Communication 2.1 Ethernet Standard Ethernet is a local area network. The early standard was IEEE 802.3, which uses CSMA/CD at the data link layer and has a speed of 10Mb/s. The physical layer has: (1) 10 Base 5 thick coaxial cable, RG-8, with a maximum length of 500m. (2) 10 Base 2 thin coaxial cable, RG-58, with a maximum length of 185m. (3) 10 Base T twisted pair cable, UTP or STP, with a maximum length of 100m. 2.2 Differences between Industrial Ethernet and Commercial Ethernet What is Industrial Ethernet? Technically, it is compatible with IEEE 802.3, so logically, commercial networks and industrial networks can be regarded as one Ethernet, and users can flexibly assemble their own network components according to the site conditions. However, given the harshness of industrial environments and the requirements for interference resistance, designers prefer to use commercially available Ethernet chips and media, while also considering the specific requirements of industrial settings: First, high temperature, humidity, and vibration must be considered. Second, there are certain requirements for industrial electromagnetic interference and radiation resistance, such as meeting EN50081-2 and EN50082-2 standards, while office-grade products have not been tested according to these industrial standards. Table 1 lists some commonly used industrial standards. To improve interference resistance and reduce radiation, industrial Ethernet products often use multi-layer circuit boards or double-sided circuit boards, and the casing uses metal such as cast aluminum to shield against interference. Third, power requirements are important. Since hubs, switches, and transceivers are mostly active components, and the quality of power supplies in the field is often poor, dual-channel DC or AC power is often used to power them. In addition, for ease of installation, most industrial Ethernet products use DIN rail or panel mounting. Fourth, regarding the choice of communication media, UTP is mostly used for wiring in office environments, while STP (shielded twisted pair) and fiber optics are recommended for industrial environments. 2.3 Reasons for Using TCP/IP and its Overview The primary reason for using TCP/IP is its compatibility with various physical network technologies, including LANs and WANs. The success of TCP/IP largely depends on its adaptability to almost all underlying communication technologies. In the early 1980s, TCP/IP was first implemented on X.25; then on a dial-up voice network (such as a telephone system); and later on Token Ring; finally, TCP/IP communication between remote packet wireless points and other Internet points was realized. Therefore, TCP/IP is extremely flexible and capable of connecting different networks. Furthermore, using TCP/IP simplifies the OSI model because it omits the presentation and session layers. If we now add the physical and data link layers of Ethernet to the OSI model, we have an Ethernet-based TCP/IP network, as shown in Figure 1. Implementing TCP/IP using Ethernet is also an economical approach. IP is the most fundamental protocol of the Internet. IP is a message-oriented protocol; it processes each packet independently, and each packet must contain complete addressing information. The format of an IP packet is shown in Figure 2. [align=center] Figure 2 IP packet format[/align] There are four types of IP addresses (as shown in Figure 3): Class A is used for very large networks, with a maximum of 16,387,064 hosts (1-126); Class B networks can have a maximum of 64,516 hosts (the first segment of the network address is 128-191); Class C is used for small networks, with a maximum of 254 hosts (the first segment of the network address is 192-223); Class D is used for multicast, for transmitting multi-purpose information. An all-zero address ("0.0.0.0") corresponds to the current host, and an all-one address ("255.255.255.255") is the broadcast address of the current subnet. TCP is a transport layer-based protocol; the protocol file can be obtained from RFC 793. It is widely used and is a connection-oriented, reliable protocol. It can decompose messages into several segments, reassemble these segments at the destination station, support the retransmission of unreceived segments, provide full-duplex connections between two devices, and allow them to exchange large amounts of data efficiently. TCP uses the sliding window protocol to make efficient use of the network. Since TCP rarely interferes with the operation of the underlying delivery system, it is adaptable to various delivery systems and provides flow control, enabling communication between systems of different speeds. A message segment is the basic transmission unit used by TCP to transmit data or control information. 3. MODBUS Data Frame Format Transmitted over TCP/IP Ethernet, it supports both Ethernet II and 802.3 frame formats. As shown in Figure 3, a Modbus TCP data frame contains three parts: a header, a function code, and data. 4. MODBUS Function Codes 4.1 Three Types (1) Public Function Codes (as shown in Table 2): Predefined function codes, ensuring their uniqueness, and approved by Modbus.org. (2) User-defined Function Codes There are two groups, 65-72 and 100-110, which do not require approval, but do not guarantee the uniqueness of the code usage. If it becomes a public code, it needs to be approved by RFC. (3) Reserved function codes are codes used by certain companies on certain traditional equipment and cannot be used for public purposes. [align=center] Table 2 Commonly Used Common Code Functions of Modbus[/align] 4.2 Function Code Classification: According to the depth of application, it can be divided into 3 categories. (1) Category 0, for the smallest available subset of clients/servers: read multiple holding registers (fc.3); write multiple holding registers (fc.16). (2) Category 1, commonly used codes that can realize basic interoperability: read coil (fc.1); read switch input (fc.2); read input register (fc.4); write coil (fc.5); write single register (fc.6). (3) Category 2, used for routine operations and data transmission functions of human-machine interface and monitoring system: (4) force multiple coils (fc.15); read general-purpose register (fc.20); write general-purpose register (fc.21); mask write register (fc.22); read and write register (fc.23). 5. Basic Concepts of VB VB (Visual Basic) is an object-oriented programming language. It is used to develop applications under the Windows operating system. It provides many interface members. Object, property, event, and method are four important interfaces. VB provides many commonly used controls. 5.1 Label Control The label control is shown in Figure 4(a). This control is used to display text and numbers. The display is shown in Figure 4(b). 5.2 Button Control The button control is shown in Figure 5(a). This control is used frequently and provides a button for system users to operate. The display is shown in Figure 5(b). 6. TCP/IP Ethernet Communication Based on Winsock Control To complete the Ethernet communication between VB and devices such as PLC, the Winsock control is used. It is necessary to give a more detailed explanation of this control. Winsock is an ActiveX control. Many items need to be added manually. The steps are as follows: (1) Select the [Project] menu. (2) Select [Components]. (3) Make the selection shown in Figure 6 in the pop-up dialog box. (4) As shown in Figure 7, the Winsock control will appear in the Components tab. 7. Basic Properties of the Winsock Control This control is invisible to the user at runtime, providing a simple way to access TCP and UDP network services without needing to understand the underlying details. When using it, you only need to set the properties and call its provided methods when appropriate. The main properties are: BytesReceived: Returns the number of bytes in the current buffer. We can use the GetData method to receive data. Read-only and not available at design time. LocalHostName: Returns the local machine name string. Not available at design time. LocalIP: Returns the IP address string expressed in (xxx.xxx.xxx.xxx) format. Not available at design time, read-only at runtime. LocalPort: The local machine's receive port is readable and writable. Available at design time, Long type. For clients, if no port needs to be specified, port 0 is used to send data. In this case, the control will randomly select a port. After a connection is established, it is the TCP port. For servers, it refers to the port used for listening. If set to 0, a random number is used. This property automatically includes the port used after calling the Listen method. Port 0 is always used to establish a dynamic connection between two computers. The client wants to obtain a random port through port 0 to connect to the server via a "callback". Protocol: Socket type, either TCP or UDP, defaults to TCP. Setting it to sckTCPProtocol indicates TCP protocol, sckUDPProtocol indicates UDP protocol. This property must be closed using the Close method before it is reset. RemoteHost: The host sending or receiving data. You can provide a hostname such as FTP://ftp.microsoft.com, or an IP address string such as "100.0.1.1". RemoteHostIP: The IP address of the remote host. For client programs, this property contains the IP name string of the remote host after the connection is established using the Connect method. For server programs, this property contains the IP string after a connection request is introduced (Connection Request event). When using a UDP socket, this property is the IP address string of the machine sending UDP data after the Data Arrival event occurs. RemotePort: The connection socket port value. For example, HTTP applications typically use port 80, while FTP uses port 21. State: The state of the control, read-only and unavailable at design time. The following values ​​are possible: Constant Description sckClosed 0 Default value, closes the socket sckOpen 1 Opens the socket sckListening 2 Listening on the port sckConnectionPending 3 In progress on an undecided connection sckResolvingHost 4 Resolving the host address sckHostResolved 5 The host address has been resolved sckConnecting 6 Connecting sckConnected 7 Connected sckClosing 8 Connection closed sckError 9 Error 8. Implementation of Ethernet Communication between VB and PLC based on MODBUS TCP The following example illustrates how to implement Ethernet communication between VB and PLC. (1) Control requirements: Use alternating buttons to control Y0 and use indicator lights to display the status of Y0 (green for running, red for stopping); be able to write and read values ​​from the D100 and D200 registers. (2) Implementation idea: Similarly, the address of Y0 is H0500. Write FF00 to ON, 0000 to OFF, and operate according to function code 05; D100 and D200 addresses are H1064 and H10C8 respectively, and the register can be read by operating according to function code 03, and the register can be written by operating according to function code 10H. (3) The design of the VB interface is shown in Figure 8. [align=center] Figure 8 Monitoring program interface diagram[/align] In the interface, buttons are used to control the ON/OFF of Y0 and the writing and reading of D100 and D200 data; the shape component is used as an indicator light to indicate the ON/OFF status of Y0; the Winsock control is used to realize the communication between PLC and VB. (1) Code structure for implementing the function (2) Establishing a network connection Private Sub Command1_Click() Winsock1.Close Winsock1.Connect Text1, 502 Label2.Caption = "Version: 1.0 : " + Format(Now, "mmm dd yyyy Hh:Nn:Ss") Command1.Enabled = False Command1.Enabled = False Text1.Enabled = False End Sub (3) ON/OFF control of Y0 Private Sub Command_send_Click() On Error GoTo ErrProc Dim sendstr(14) As Byte sendstr(0) = &H0 'Swap the high byte of the identifier, usually 0 sendstr(1) = &H0 'Swap the low byte of the identifier, usually 0 sendstr(2) = &H0 'Protocol identifier high byte, 0 sendstr(3) = &H0 'Protocol identifier low byte, 0 sendstr(4) = &H0 'Byte length high byte sendstr(5) = &H9 'Below byte length low byte sendstr(6) = &HFF 'Unit identification number, default is 255 sendstr(7) = &H5 'Write a coil command code sendstr(8) = &H0 'Write the starting address of the coil high byte sendstr(9) = &H1 'Write the starting address of the coil low byte sendstr(10) = a '=FF to open the coil,=00 to close the coil sendstr(11) = &H0 'Dim aStr As String Dim i, j As Integer For i = 0 To 11 aStr = aStr & Chr(sendstr(i)) Next Winsock1.SendData aStr Exit Sub ErrProc: MsgBox "Data transmission failed", vbCritical, "Network transmission" End Sub (4) Coil status display and register read data display Private Sub winsock1_DataArrival(ByVal bytesTotal As Long) Dim strdata () As Byte Dim i, j As Integer Dim s As String Dim s1 As String Dim IO As Byte i = Winsock1.BytesReceived ReDim strdata (i) Winsock1.GetData strdata, vbByte, i For j = 0 To i - 1 s = s + Hex (strdata (j)) Next If s Like "*5*" Then s1 = Mid (s, 12, 2) If s1 = "FF" Then IO = 1 a = &H0 Else IO = 0 a = &HFF End If If IO = 1 Then Shape1.FillColor = RGB (0, 255, 0) 'green Labl3.Caption = "IO point connected" Else Shape1.FillColor = RGB (255, 0, 0) 'red Labl3.Caption = "IO point disconnected" End If End If If s Like "*3*" Then Text3.Text = Text3.Text + Mid(s, 11) + "" End If (5) Read multiple register data End Sub Private Sub Command4_Click() On Error GoTo ErrProc Dim str(12) As Byte str(0) = &H0 'Swap the high byte of the identification number, usually 0 str(1) = &H0 'Swap the low byte of the identification number, usually 0 str(2) = &H0 'Protocol identification number high byte, is 0 str(3) = &H0 'Protocol identification number low byte, is 0 str(4) = &H0 'Byte length high byte str(5) = &H6 'Below byte length low byte str(6) = &HFF 'Unit identification number, default is 255 str(7) = &H3 'Read multiple register command code str(8) = &H10 'Read data starting address high byte str(9) = &HC8 'Read data starting address low byte str(10) = &H0 'Data length high byte str(11) = &H1 'Data length low byte Dim bStr As String Dim i As Integer For i = 0 To 11 bStr = bStr & Chr(str(i)) Next Winsock1.SendData bStr Exit Sub ErrProc: MsgBox "Data transmission failed", vbCritical, "Network transmission" End Sub The write operation of the D100 register is similar. Due to space limitations, other code will not be described here. 9. Conclusion Industrial Ethernet is an inevitable trend in the development of industrial automation networks. In order to manage and monitor industrial equipment in real time, most industrial Ethernet devices support remote management, and each factory has its own network management software to provide a user-friendly graphical interface for operating network devices. Administrators can also directly debug the devices through remote telnet or web interface. The superior compatibility, reliable stability, adaptability to harsh environments, strong redundancy, and convenient manageability are the reasons why industrial Ethernet is increasingly adopted in network design. This also presents a significant opportunity for industrial Ethernet to demonstrate its capabilities in a growing number of network environments. This article is written on the occasion of Delta's upcoming launch of its Ethernet module, hoping to provide some assistance in promoting its adoption. Due to my limited expertise, errors and omissions are inevitable in this article; I welcome readers' criticism and corrections.
Read next

CATDOLL 128CM Lola

Height: 128cm Weight: 19kg Shoulder Width: 30cm Bust/Waist/Hip: 57/52/63cm Oral Depth: 3-5cm Vaginal Depth: 3-15cm Anal...

Articles 2026-02-22
CATDOLL Ya Soft Silicone Head

CATDOLL Ya Soft Silicone Head

Articles
2026-02-22
CATDOLL Kara TPE Head

CATDOLL Kara TPE Head

Articles
2026-02-22