This article will use a GitHub open source component technology to read and write Siemens PLC data, using the Ethernet-based TCP/IP implementation, no additional components, read operations as long as the background thread will not be stuck thread, this component supports super convenient high-performance read and write operation
GitHub Address: Https://github.com/dathlin/HslCommunication If you like to start or fork, you can also enjoy support.
You can download the installation in NuGet Manager in Visual Studio, or you can enter the following instructions directly in the NuGet console
Install-package hslcommunication
If you need a tutorial: NuGet installation Tutorial: http://www.cnblogs.com/dathlin/p/7705014.html
Technical Support QQ Group: 592132877 (the component version update details will be released in the group first) Last modified: April 1, 2018 09:42:29
Inside all kinds of small partners, for you to answer data interaction, programming skills, if you have any questions about the API provided by this interface, you can add group consultation, if there are better suggestions, welcome to put forward.
Complete information about the components and other API descriptions refer to: The Http://www.cnblogs.com/dathlin/p/7703805.html component's authorization protocol, the update log, all within the page.
This article will show you how to configure the network parameters and how to use the code to access the PLC data, hoping to give the people in need to solve some practical problems. Mainly for Siemens PLC M,Q,I,DB block data read and write, pro-test effective.
Here the use of the direct way of the network cable, if the PLC is connected to the LAN, you can read and write remotely ^_^
There are 2 namespaces used here:
Using hslcommunication;using HslCommunication.Profinet.Siemens;
Just talk.
When a host computer needs to read 100 Siemens PLC equipment (here is just for example, usually use Modbus TCP is the same), you use the server initiative to request the mechanism of 100 devices is a great test of performance, If you open 100 threads to poll 100 devices, then the performance loss will be very large, not to mention the addition of equipment, if the installation of Modbus TCP server, can be a perfect solution to the performance problem, because the connection pressure will be evenly distributed to each plc, Server side as long as a new timestamp to know that the client has no connection.
We add the Modbus TCP method to 100 plc, send the data to the server's IP and port, the server distinguishes the device according to station number. This makes it possible to build a high-performance terminus. This component supports the fast setting up of a high performance Modbus TCP terminus.
Http://www.cnblogs.com/dathlin/p/7782315.html
About two modes
All of the client classes provided by this component, including Mitsubishi, Siemens, Omron, Modbus-tcp, and Simplifynet, are inherited from the dual-mode base class, and the dual-mode includes a short connection and a long connection, and the following details the differences between the next two modes
Short Connection : Each read and write is a separate request, the request is closed, if the server's port only supports single connection, then the port can be reused after shutdown, but in frequent network requests, prone to abnormal, there will be other requests unsuccessful, Especially in the case of multithreading.
Long Connection : Create a common connection channel, all the read and write requests to use this channel to complete, so that the read and write performance faster, instant multi-threaded calls will not affect the internal synchronization mechanism. If the server's port only supports a single connection, then this port is occupied, such as the port mechanism of Mitsubishi, Siemens Modbus TCP port mechanism is also the case. The following code uses long connections by default, with higher performance and multi-threaded synchronization.
In the short-connected mode, each request is a separate access, so there is no reconnection, in the long-connected mode, if this request fails, the next time the request, it will automatically reconnect to the server until the request is successful. In addition, as far as possible, all reading and writing are judged on the success of the results.
About Logging
Whether it is Mitsubishi's data access class, or Siemens, or Modbus TCP Access class, there is a lognet property used to record the log, the property is an interface class, Ilognet, everything inherits the interface can be used to record the log, the log will be in the failure of access, This is especially true because the network causes logging when Access fails (if you have a real logger configured for this lognet property): If you want to use this logging feature, please follow the blog below to instantiate it:
Http://www.cnblogs.com/dathlin/p/7691693.html
About the two types of agreements
This component supports the Siemens communication has two kinds of protocol, one is the S7 protocol, in the PLC side almost do not need configuration parameters, another protocol Fetch/write protocol, relatively troublesome point, if S7 inconvenient to read, can choose Fetch/write, relatively, S7 more convenient point
Accessing test projects
In the above GitHub source code, there is a test project, Hslcommunicationdemo, which contains a variety of client-side demo project, do not need to write any code to test the data access.
Demo Project
The following three shows how to access the data of the PLC, we usually need to process after the access, the following example project shows the background from the PLC reading data, the foreground display and push to all the online client functions, clients and graphical display, with a certain reference, the project address is:
Https://github.com/dathlin/RemoteMonitor
The following picture example of the left side of the program is the server program, it should be directly connected with the PLC and access to the local area network, and then push the data to the client display. Note: A sophisticated program should separate the processing logic programs and interface programs, such as the server program here to achieve data collection, push, storage. Let the client program to implement data collation, analysis, display, so that even if the client program because of a bug, the server can still work properly.
Siemens chapter II (TCP Direct communication using S7 protocol, simple configuration, General PLC support)
The test passed the plc:1200 series of my pro-test
200smart thanks to the ① termination ^ ^ test
300 series thanks to understand the PLC does not understand C # test
1500 Series thanks for the test of the brook wind-⊙_⌒
The format of the message refers to the following two articles
Http://www.itpub.net/thread-2052649-1-1.html
Https://wenku.baidu.com/view/d93b88b06394dd88d0d233d4b14e852459fb3912.html
If you are good at network communication and component development, you can develop their own Siemens communication library through the message format, I do is based on the message format two times, hiding the details of the socket communication, but also contains the exception processing, provides a simple and convenient API to read and write data. Provides read and write integer data, string read and write, to enrich a variety of requirements, in fact, as long as the read and write section, equivalent to any data.
Preparation: Configure the IP address on Siemens PLC, only one IP address is enough, and then turn on the computer cmd instructions, as long as can ping Siemens PLC can.
Also need to be configured on the PLC side to open the Get/set communication allow: (thanks to the user Olife to provide the picture) (if you encounter the reading data when the length of the Verification failure information, please be sure to check the following tick whether to play)
Instantiation:
Siemenstcpnet = new Siemenss7net (Siemensplcs, "192.168.0.100") { connecttimeout = n};
Connection server, can also be placed in the window's Load method, it is generally recommended to use a long connection, faster, but also thread-safe (call the following method is to use a long connection, if you do not directly read data, it is a short connection):
Operateresult connect = Siemenstcpnet.connectserver (); if (connect. issuccess) { MessageBox.Show ("Connection succeeded! " ); } else { MessageBox.Show ("Connection failed! " ); }
Disconnect, that is, close the long connection, if the request data, it becomes a short connection
Siemenstcpnet.connectclose ();
The following is a demonstration of some simple data operations, eliminating the success of the validation of the results, all read and write results are operateresult type and derived type, there is a issuccess attribute to determine the success or not
Read operation, where the M100 can replace the i100,q100,db20.100 effect when the same bool m100_7 = Siemenstcpnet.readbool ("M100.7"). Content; Read if M100.7 is off, note that M100.0 equals M100 byte byte_m100 = Siemenstcpnet.readbyte ("M100"). Content; Read the value of M100 short short_m100 = siemenstcpnet.readint16 ("M100"). Content; Read the m100-m101 composed of the word ushort ushort_m100 = siemenstcpnet.readuint16 ("M100"). Content; Reads a m100-m101 composed of unsigned values int int_m100 = Siemenstcpnet.readint32 ("M100"). Content; Read m100-m103 composed of signed data uint UINT_M100 = Siemenstcpnet.readuint32 ("M100"). Content; Read m100-m103 composed of unsigned values float float_m100 = siemenstcpnet.readfloat ("M100"). Content; Read m100-m103 composed of single-precision values long long_m100 = Siemenstcpnet.readint64 ("M100"). Content; Read m100-m107 a large data value of ulong ulong_m100 = Siemenstcpnet.readuint64 ("M100"). Content; Read m100-m107 composed of unsigned big data double double_m100 = siemenstcpnet.readdouble ("M100"). Content Read m100-m107 composed of double values string str_m100 = Siemenstcpnet.readstring ("M100", 10). content;//reads the ASCII string data//write operation consisting of m100-m109, where M100 can be replaced by i100,q100,db20.100 effect Siemenstcpnet.write ("M100.7", true); Write a bit, note that M100.0 is equivalent to M100 siemenstcpnet.write ("M100", (byte) 0x33); Write single byte Siemenstcpnet.write ("M100", (short) 12345); Write double-byte signed Siemenstcpnet.write ("M100", (ushort) 45678); Write double-byte unsigned siemenstcpnet.write ("M100", 123456789); Write two-character signed Siemenstcpnet.write ("M100", (UINT) 3456789123); Write double-word unsigned siemenstcpnet.write ("M100", 123.456f); Write single precision Siemenstcpnet.write ("M100", 1234556434534545L); Write large integers with signed siemenstcpnet.write ("M100", 523434234234343UL); Write large integer unsigned siemenstcpnet.write ("M100", 123.456d); Write double precision Siemenstcpnet.write ("M100", "K123456789");//write ASCII string
The following is a description of complex data operations, as well as bulk data operations, such as reading m100-m109
operateresult<byte[]> read = Siemenstcpnet.read ("M100", ten); { if (read. issuccess) { byte m100 = read. CONTENT[0]; byte M101 = read. CONTENT[1]; byte m102 = read. CONTENT[2]; byte m103 = read. CONTENT[3]; byte m104 = read. CONTENT[4]; byte m105 = read. CONTENT[5]; byte m106 = read. CONTENT[6]; byte m107 = read. CONTENT[7]; byte m108 = read. CONTENT[8]; byte m109 = read. CONTENT[9]; } else { //An exception occurred } }
In this way, all the bytes of data are extracted, if the data is more complex, can also be processed according to the actual situation. Of course, it also supports batch write data information.
If you want to implement a custom data type, you need to inherit an interface
public class usertype:hslcommunication.idatatransfer{#region Idatatransfer private HslCommunication.Core.IByteTra Nsform bytetransform = new HslCommunication.Core.ReverseBytesTransform (); Public ushort Readcount = 20; public void ParseSource (byte[] Content) {int count = bytetransform.transint32 (content, 0); float temp = Bytetransform.transsingle (Content, 4); Short name1 = Bytetransform.transint16 (Content, 8); String barcode = Encoding.ASCII.GetString (Content, 10, 10); } public byte[] Tosource () {byte[] buffer = new BYTE[20]; Bytetransform.transbyte (count). CopyTo (buffer, 0); Bytetransform.transbyte (temp). CopyTo (buffer, 4); Bytetransform.transbyte (name1). CopyTo (buffer, 8); Encoding.ASCII.GetBytes (barcode). CopyTo (buffer, 10); return buffer; #endregion #region Public Data public int count {get; set;} Public float Temp {get; set;} Public sHort name1 {get; set;} public string Barcode {get; set;} #endregion}
So that we can read and write special data.
operateresult<usertype> read = siemenstcpnet.readcustomer<usertype> ("M100"), if (read. issuccess) { usertype value = read. Content;} Write Valuesiemenstcpnet.writecustomer ("M100", New Usertype ());
Supports read and write operations for M,I,Q,DB,T,C data
The reading of the ultimate data:
Here provides a core message reading mechanism, you can pass your own message, and then receive the server's message, and then their own resolution operation, can be implemented according to the message format arbitrary operation, of course, the premise is the need for message support. Suppose I want to implement write M100, for 0x3b, then the final message is
At F0 (0A)------------------0E
private void Userbutton23_click_1 (object sender, EventArgs e) { byte[] buffer = HslCommunication.BasicFramework.SoftBasic.HexStringToBytes ( "F0 0E 00 05 05 01" At the "XX" 0A (3B ") from the page of the (XX) Operateresult<byte[]> operate = siemenstcpnet.readfromservercore (buffer); if (operate. issuccess) { //display the message returned by the server textboxappendstringline ( HslCommunication.BasicFramework.SoftBasic.ByteToHexString (operate. Content)); } else { //Display network error MessageBox.Show (operate. Tomessageshowstring ()); } }
For more detailed information, you can refer to the test items in the source code.
If using the Fetch/write protocol to read and write operations, the configuration of the PLC side is inconsistent, the class is inconsistent, the other is the same, no longer repeat, the emphasis on the PLC network module configuration
Environment : The Step 7v5.5 SP4 programming software is used here as an example, when adding an Ethernet module (6gk7 343-1ex30-0e0 cp343-1) to the configuration, you can set the IP address and subnet mask, which is used here for testing, so do not use routers, If your Siemens needs to connect to the intranet, you need to configure the router. Currently only supports M,I,Q data read and write. Then click New to create a Ethernet (1) network. Ethernet parameter configuration such as:
After adding the Ethernet module to the rack, now open the network configuration, open and click on the configuration of the PLC module. The following interface appears, where you can double-click the arrows to pop up the dialog box and perform a series of actions:
Follow the above set of operations down, create a read port, the port number is 2000, later useful, you need to remember, follow the above steps to create a write port, only the last step is inconsistent, as follows:
After the configuration is complete, two new ports are created, one for reading data and one for writing data. <strong> Note: Be sure to write to the PLC after Setup is complete. </strong>
As shown in the configuration error, you should configure a port that supports both read and write operations
When instantiating a class.
Private siemensfetchwritenet siemensfwnet = null;
Siemensfwnet = new Siemensfetchwritenet () { IpAddress = "192.168.0.100", Port = +, connecttimeout = (The );
Not easy to create, thanks for playing rewards
C # Read and write Siemens PLC data, including S7 protocol and Fetch/write protocol, S7 support 200SMART,300PLC,1200PLC,1500PLC