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 star or fork, you can also reward support, please look for the source code project.
NuGet Address: https://www.nuget.org/packages/HslCommunication/
GitHub Address: Https://github.com/dathlin/HslCommunication If you like star or fork, you can also enjoy support.
This project currently supports the C # language and Java language, the C # language is more complete, the Java version of the library is still in development and improvement.
Click to download the jar package for this component. Hslcommunication.jar
Examples of code use support can be referenced in the C # version, and they are almost identical, and the supported data types are consistent.
Technical Support QQ Group: 592132877 (the component version update details will be released in the group first) Last modified: April 3, 2018 09:44:41
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:
Import Hslcommunication.profinet.siemens.siemensplcs;import HslCommunication.Profinet.Siemens.SiemensS7Net; Import HslCommunication.Core.Types.OperateResultExOne;
Talk to me. (C # version server)
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
Temporarily just reserved the interface, specifically can be implemented by themselves
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.
These two protocols in addition to the type of instantiation inconsistent, read and write plc code and connection mechanism are consistent, so the specific code of the FW protocol is not pasted, detailed reference to the following demo project.
Access to test Projects (C # version, easy to test)
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.
is:hslcommunicationdemo.zip
Demo project (C # server +asp.net + C # client + Android Client)
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.
TCP Direct communication under 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)
The simplest application requires only two lines of code, example: Reading a M100 byte value
private static void Siemestest () { siemenss7net siemens_net = new Siemenss7net (siemensplcs.s1200, "192.168.1.195"); System.out.println (siemens_net. ReadByte ("M100"). Content); }
The above uses a short connection mode, with the accompanying, we demonstrate the following long connection
Siemenss7net siemens_net = new Siemenss7net (siemensplcs.s1200, "192.168.1.195"); Operateresult connect = siemens_net. ConnectServer (); if (connect. issuccess) { System.out.println ("Connect success!"); } else { System.out.println ("failed:" +connect. Message); }
You need to close a long connection when the program exits
Siemens_net. Connectclose ();
The following shows some simple common read and write operations, but the results are not judged, in the actual production code, the results need to be strictly judged
byte M100_byte = siemens_net. ReadByte ("M100"). Content; Short M100_short = siemens_net. ReadInt16 ("M100"). Content; int m100_int = siemens_net. ReadInt32 ("M100"). Content; Long M100_long = siemens_net. ReadInt64 ("M100"). Content; float m100_float = siemens_net. Readfloat ("M100"). Content; Double m100_double = siemens_net. Readdouble ("M100"). Content; String m100_string = siemens_net. ReadString ("M100", (short) 10). Content; Siemens_net. Write ("M100", (byte) 123); Siemens_net. Write ("M100", (short) 123); Siemens_net. Write ("M100", (int) 123); Siemens_net. Write ("M100", (long) 123); Siemens_net. Write ("M100", 123.456f); Siemens_net. Write ("M100", 123.456d); Siemens_net. Write ("M100", "1234567890");
The following is a description of the complex data operations and the determination of the results, as well as the bulk data operations, such as reading m100-m109
operateresultexone<byte[]> read = Siemens_net. Read ("M100", (short)); { 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 } }
Read-write data type, support M. I. Q,db block, T, C
M Address Example: M100
I Address Example: I100
Q Address Example Q100
DB block. DB1.100 for smart200, V-Zone 100 is DB1.100.
T Address Example T100
C Address Example: C100
Java Android Read and write Siemens PLC data, including S7 protocol and Fetch/write protocol, S7 support 200SMART,300PLC,1200PLC,1500PLC