Objective
This article uses a NuGet-exposed component technology to implement a Modbus TCP server-side data engine that facilitates the reception of data from a variety of devices.
You can download the installation in the NuGet Manager in Visual Studio, or you can enter the following instructions directly in the NuGet console to install:
Install-package hslcommunication
NuGet Installation Tutorial Http://www.cnblogs.com/dathlin/p/7705014.html
QQ Group of Technical Support: 592132877 (the component version update details will be released in the group in the first time)
Reference
ModBus components all the function classes are in the Hslcommunication.modbus namespace, so before you use Add
Using Hslcommunication.enthernet;
How to use
private bool M_ismodbusstart {get; set;} = false; Whether the service starts private modbustcpserver tcpserver; Server engine Object private void Userbutton1_click (object sender, EventArgs e) {if (!m_ismodbusstart) {M_ismodbusstart = true; TCPServer = new Modbustcpserver (); Instantiate server receive Object tcpserver.lognet = new HslCommunication.LogNet.LogNetSingle (Application.startuppath + @ "\logs \log.txt "); Set log file tcpserver.ondatareceived + = tcpserver_ondatareceived; Correlation data Receive Method Tcpserver.serverstart (51234); Bind Port timer. Start (); Start service}} private void Tcpserver_ondatareceived (byte[] object1) {//Display received What data BeginInvoke (new action<byte[]> (Showmodbusdata), object1); } private void Showmodbusdata (byte[] modbus) {Textbox1.appendtext (DateTime.Now.ToString ("yyyy-mm -DD HH:mm:ss.fff ") +": "+ HslCommunication.BasicFramework.SoftBasic.ByteToHexString (Modbus) + Environment.NewLine); }
Special Note:
The server is only responsible for accepting the Modbus TCP protocol data, regardless of the client read and write instructions, will trigger the receive event, and return a string of successful data.
So on the server side, the read of the receiving client is invalid and the returned data is empty.
The key is to receive the write function from each device, so you can develop a data terminal to collect data information.
The following window is the test example, Siemens 1215C PLC to the server to send read and write data, server-side reception data display.
Attention:
The 7th byte of the data string is Modbus station information, the following interface is FF, or 255, can be used to distinguish between different devices sent data information, so here a server instance hangs the maximum number of clients is 256 devices, the first two bytes of the message header sequence, if the device can pin the message header, Use this to identify the device, you can distinguish 65536 devices.
Reference Links:
If you are not familiar with Modbus TCP, then please refer to the following address, I am referring to the address of the blog Development code:
http://blog.csdn.net/thebestleo/article/details/52269999
C # Development of Modbus TCP server program to achieve modbus TCP data total Exchange