How to realize Ethernet communication between PC and PLC through VB - Database & Sql Blog Articles

Single chip microcomputer STM32L151CCU6

1 Introduction

At all levels of the network, as automation and control engineers need more and more opportunities to deal with the network, the desires and needs of networking are also rising. Moreover, in monitoring and data acquisition applications, most engineers have applied Ethernet protocols to the data acquisition and monitoring control system (SCADA) and factory diagnostics, testing and maintenance. How to easily realize the interaction with plc has become a new competitive battlefield for many manufacturers. This has resulted in a human-machine interface and various configuration software products. The emergence of these products simplifies the control and operation of the PLC, making the application of the PLC more convenient. But there are also their common shortcomings, the price is generally high and the secondary development is poor. As a "raw" programming language, VB combines the advantages of both. This article combines a simple case to discuss how to implement Ethernet communication between PC and PLC through VB.

2, the basic concept of Ethernet communication

2.1 Ethernet standard

Ethernet is a type of local area network. The early standard was IEEE 802.3, the data link layer used CSMA/CD, and the 10Mb/s speed physical layer was:

(1) 10 Base 5 thick coaxial cable, RG-8, a maximum length of 500m.

(2) 10 Base 2 thin coaxial cable, RG-58, a maximum length of 185m.

(3) 10 Base T twisted pair, UTP or STP, a maximum length of 100m.

2.2 The difference between industrial Ethernet and commercial Ethernet

What is Industrial Ethernet? Technically, it is compatible with IEEE802.3, so the commercial network and the industrial network can be logically regarded as an Ethernet, and users can flexibly assemble their own network components according to the site conditions. However, from the harsh and anti-interference requirements of the industrial environment, designers hope to use the Ethernet chips and media found in the market, taking into account the special requirements of the following industrial sites: first consider high temperature, humidity, vibration. Second, there are certain requirements for industrial anti-electromagnetic interference and anti-radiation, such as meeting EN50081-2 and EN50082-2 standards, while office-level products are not tested by these industry standards. Table 1 lists some common industry standards. In order to improve the anti-interference and reduce the radiation, industrial Ethernet products use multi-layer circuit boards or double-sided circuit boards, and the outer casing is shielded by metal such as cast aluminum shielding. The third is the power requirement. Because the hubs, switches, and transceivers are mostly active components, and the quality of the field power supply is poor, it is often powered by dual DC or AC power. In addition, for ease of installation, most industrial Ethernet products use DIN. Rail or panel mounting. The fourth is the choice of communication medium. In the office environment, most wiring uses UTP, while in industrial environments, users are recommended to use STP (with shielded twisted pair) and fiber.

2.3 Reasons for using TCP/IP protocol and its overview

One of the main reasons is that it can be used in a variety of physical network technologies, including LAN and WAN technologies. The success of the TCP/IP protocol depends to a large extent on its ability to accommodate almost all underlying communication technologies. In the early 1980s, the TCP/IP protocol was first run on X.25; then TCP/IP was used on a dial-up voice network (such as a telephone system), and TCP/IP was successfully run on the Token Ring network; Finally, TCP/IP communication between TCP/IP remote packet wireless network and other Internet sites is realized. Therefore, the TCP/IP protocol is extremely flexible and has the ability to connect to different networks. In addition, the use of TCP/IP also simplifies the OSI model because it omits the presentation layer and the session layer. If the physical layer and data link layer of Ethernet are now added to the OSI model, an Ethernet-based TCP/IP network is formed, as shown in Figure 1. Implementing TCP/IP with Ethernet is also an economical way.

IP is the most basic protocol of the Internet. IP is a message-oriented protocol that processes each packet independently, and each packet must contain complete addressing information. The format of an IP packet is shown in Figure 2.

Figure 2 Format of the IP packet


There are 4 types of IP addresses (as shown in Figure 3): Class A is used to process very large networks, up to 16,387,064 hosts (1 to 126); Class B networks can have up to 64,516 hosts (the first segment of network addresses) It is 128 to 191); Class C is used for small networks, and there are up to 254 hosts (the first segment of the network address is 192 to 223); Class D is used for multicast transmission for multi-purpose information transmission. The all zero ("0.0.0.0") address corresponds to the current host, and the all 1 address ("255.255.255.255") is the broadcast address of the current subnet.

TCP is a transport layer-based protocol, and protocol files are available from RFC793 for a wide range of connection-oriented, reliable protocols. It can decompose the message into segments, reassemble the segments at the destination station, support resending the unreceived segments, and provide full-duplex connections between the two devices, allowing them to efficiently exchange large amounts of data. TCP uses the sliding window protocol to efficiently use the network. Since TCP seldom interferes with the operation of the underlying delivery system, it adapts to various delivery systems and provides flow control to enable communication at various rates. A segment is the basic transmission unit used by TCP to transmit data or control information.

3, MODBUS data frame format

It is transmitted over TCP/IP Ethernet and supports both Ethernet II and 802.3 frame formats. As shown in Figure 3, the Modbus TCP data frame contains the message header, function code, and data 3 parts.

4, MODBUS function code

4.1 3 types

(1) Common function code (as shown in Table 2): The defined function code guarantees its uniqueness and is recognized by Modbus.org.

(2) There are two sets of user-defined function codes, 65 to 72 and 100 to 110 respectively, without approval, but the uniqueness of the code is not guaranteed. If it becomes a public code, it must be approved by RFC.

(3) Reserved function codes, codes used by some companies in some traditional devices, are not for public use.

Table 2 Modbus Common Common Code Function

4.2 Function code division: According to the depth of application, it can be divided into 3 categories.

(1) Category 0, the smallest available subset for the client/server: read multiple holding registers (fc.3); write multiple holding registers (fc.16).

(2) Category 1, common code for basic reciprocal operation: read coil (fc.1); read digital input (fc.2); read input register (fc.4); write coil (fc.5) ; Write a single register (fc.6).

(3) Category 2, for routine operations and data transfer functions of the human-machine interface, monitoring system:

(4) Force multiple coils (fc.15); read general-purpose registers (fc.20); write general-purpose registers (fc.21); mask write registers (fc.22); read-write registers (fc.23).

5, the basic concept of VB

VB (Visual Basic) is an object-oriented programming language. It is used to develop applications under the WINOOWs operating system. It provides many interface members. Objects, properties, events, and methods are four important interfaces. VB provides many Commonly used controls.

5.1 Volume Control

As shown in Figure 4(a), the label control is dedicated to displaying text and numbers, and the display is shown in Figure 4(b).

5.2 button controls

The button control is shown in Figure 5(a). The control has a high usage rate and provides a button for the system user to operate, as shown in Figure 5(b).

6. TCP/IP Ethernet communication based on Winsock control

To complete the Ethernet communication between VB and PLC, Winsock control is needed. It is necessary to explain the control in detail. Winsock is an ActiveX control that requires many items to be added manually. The steps are as follows:

(1) Select [Project] of the menu.

(2) Select [Parts].

(3) In the pop-up dialog box, make the selection as shown in Figure 6.

Figure 6 dialog interface

(4) As shown in Figure 7, the Winsock control appears on the Parts tab.

7, the basic properties of the Winsock control

This control is invisible to the user at runtime and provides an easy way to access TCP and UDP web services without having to understand the underlying details. You only need to set the properties and call the methods provided at the appropriate time.

The main attributes 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 native name string, which is not available at design time.

LocalIP: Returns the IP address string expressed in the format of (xxx.xxx.xxx.xxx). Not available at design time, read-only at runtime.

LocalPort: This unit uses the receiving port to read and write, and is available at design time, Long type. For customers, if you do not need to specify a port, send data with port 0. In this case, the control will randomly select a port. After a connection is determined, it is the port of TCP. For servers, 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 get a random port through port 0 to connect to the server with a "callback".

Protocol: The type of socket, either TCP or UDP. The default is TCP. Set to sckTCPProtocol to indicate that the TCP protocol sckUDPProtocol represents the UDP protocol. Use the Close method to close this property before it is reset.

RemoteHost: The host that sends or receives data. You can provide a host name 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 the client program, use the Connect method after the connection is determined. This property contains the IP name string of the remote host. For server programs, this property contains an IP string after the connection requirement is introduced (Connection Request event). When using a UDP socket, this property is the machine IP address string that sends UDP data after the Data Arrival event occurs.

RemotePort: Connects to the socket port value. For example, HTTP applications typically use port 80 and FTP uses 21.

10000mAh LTE Router

MiFi stands for Mobile WiFi and it works in the same way as a WiFi router, with the difference that you can set up a wireless personal area network and get online where ever you are on the globe. 4G-LTE routers belong to the fourth generation routers that use the "long-Term Evolution Technology" (LTE). This LTE technology is the successor of 3G technologies such as UMTS, HSDPA and HSUPA. The latest generation 4 g LTE MiFi routers are very fashional and stylish.

4G WIFI Router, TDD Router, LTE Router, 4G HOTSPOT, WIFI Device, powerbank router

Shenhzhen Tongheng Weichuang Technology Co., Ltd , https://www.thwclte.com