MODBUS-TCP client communication of the communication listening mode between the irregular Modbus client and the irregular Modbus server

Source: Internet
Author: User

Preface

This article will use a GitHub exposed component technology to implement a special-shaped Modbus TCP client, convenient for the special-shaped Modbus TCP server to read and write, this server can be computer-side C # design, can also be a special device to achieve, It can also be any other server that supports this communication protocol.

GitHub Address: Https://github.com/dathlin/HslCommunication If you like star or fork, you can also enjoy support.

The source code for the demo project below this article is in this address

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

NuGet Installation Tutorial Http://www.cnblogs.com/dathlin/p/7705014.html

Technical Support QQ Group: 592132877 (Component version update details will be released in the group first) Component API address: http://www.cnblogs.com/dathlin/p/7703805.html

About profiled mode, also known as a listening client

The usual pattern is the server client mode, the client initiates the connection, and then the data exchange with the server is implemented, but in fact there is a pattern is that the server actively initiate the connection to the client, and then the client to perform the data interaction with the server, listening may be a bit awkward, but there is a real, Usually used in some, industrial site 4G Internet equipment. Of course, you can also implement some of the custom functions. You need to look at the following code slowly.

Profiled mode mainly consists of two parts, listening server, profiled MODBUS-TCP client.

The listening server is responsible for listening for network requests and Alien protocol validation, and then throwing the connection to the profiled MODBUS-TCP client for use.

Registration Package and Alien Protocol

After the Modbus server connected to the IP and port address of the cloud server, it is necessary to send a registration package information, this registration package information should follow the following protocol, known as the Alien Protocol, other protocol support needs two times custom development completed. This agreement is detailed as follows:

The registration package has a total of 28 bytes,

Register Package
0x48 0x73 0x6e 0x00 0x17 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x30 0x31 0x00 0x00 0x00 0x00 0x00 0x00 0xC0 0xa8 0x00 0x01 0x17 0x10
+------------------+     +----+     +-----+       +----------------------------------------------------------------------- ------+          +----------------------------------------+        +---------------------------+       +-------------+
+ Fixed message header + spare back length DTU code 12345678901 (unique identification, such as mobile phone number) login password (not subject to the exclusion of the letter) ip:192.168.0.1 Port 10000
+------------------+     +----+     +-----+        +---------------------------------------------------------------------- ------+          +-----------------------------------------+         +-------------------------+       +--------------+

Return message, below is the normal registration success information

0x48 0x73 0x6e 0x00 0x01 0x00
+----------------- +        +----+   +----+      +----+
Fixed message header alternate trailing length result code

Result code

0x00: Login Successful
0X01:DTU Repeat Login
0X02:DTU Disable Login
0X03: Password validation failed

Listening for requests

The first step is to establish a listening server, why the event triggered two parameters is because when you have more than one server, you can associate a method, and then through the network to distinguish the signal, the core code is as follows, then just call the Networkalienstart method to

        Private networkalienclient Networkalien = null;        private void Networkalienstart (int port)        {            Networkalien = new Networkalienclient ();            networkalien.onclientconnected + = networkalien_onclientconnected;            Networkalien.lognet = lognet;            Networkalien.serverstart (port);        }        private void networkalien_onclientconnected (Networkalienclient network, aliensession session)        {            // This method is triggered when there is a connection        }

Alien Modbus-tcp

Create a special-shaped client, the data interaction block and, the original client is almost identical, just the connection principle is not the same, set up as a special-shaped client, is not responsible for the function of the connection, has become a passive setting of the function of the connection object. The code is also very convenient

1. Define the object first

Private modbustcpnet bustcpclient = null;

2. Instantiation, IP and port arbitrary designation, anyway for the special-shaped client is no use, but station number is still useful

Bustcpclient = new Modbustcpnet (TextBox1.Text, Port, station);

3. Set to irregular mode, of course, do not set the first, you can adjust to the fourth step and then set

Bustcpclient.connectserver (NULL);

4. Modify the callback method for request listening

private void networkalien_onclientconnected (Networkalienclient network, aliensession session)        {            Bustcpclient.connectserver (session);        }

When the Modbus server is connected, the method is triggered, and the data can be read and written to the server via Bustcpclient. The scenario shown above is only a client case, what if there are multiple clients? We can use a unique identification code to correlate, for each incoming session data, also contains the connection request DTU unique code, we can make a decision.

We first set up the unique code of the profiled client

Bustcpclient.connectionid = "12345678901"

Then modify the callback method

private void networkalien_onclientconnected (Networkalienclient network, aliensession session)        {            if (session). DTU = = Bustcpclient.connectionid)            {                bustcpclient.connectserver (session);            }        }

The usual approach is to define your client array first, specify the DTU code, and then batch the array for data request operations.

For specific data request operations, please refer to the normal MODBUS-TCP blog,

Http://www.cnblogs.com/dathlin/p/7885368.html

Here's a little bit of sample code.

private void Userbutton30_click (object sender, EventArgs e) {//read operation bool coil100 = Bustcpclient.readcoil ("100").   Content; Read the coil 100 on-off short short100 = bustcpclient.readint16 ("100"). Content; Read the short value of register 100 ushort ushort100 = bustcpclient.readuint16 ("100"). Content; Read the ushort value of register 100 int int100 = Bustcpclient.readint32 ("100").      Content; Read the int value of register 100-101 UINT uint100 = Bustcpclient.readuint32 ("100").   Content; Read the UINT value of register 100-101 float float100 = bustcpclient.readfloat ("100"). Content; Read the float value of register 100-101 long long100 = Bustcpclient.readint64 ("100").    Content; Read the Long value of register 100-103 ulong ulong100 = Bustcpclient.readuint64 ("100"). Content; Read the ULONG value of register 100-103 double double100 = bustcpclient.readdouble ("100"). Content; Read the Double value of register 100-103 string str100 = bustcpclient.readstring ("100", 5). content;//reads 100 to 104 of a 10-character string//write operation Bustcpclient.writecoil ("+", true);//write Coil 100 for bustcpclient.write ("100" , (short) 12345);//write Register 100 is 12345 bustcpclient.write ("", (ushort) 45678);//write Register 100 is 45678 bustcpclient.write ("100", 123456789);//write Register 100 -101 is 123456789 bustcpclient.write ("100-101", (UINT) 123456778);//write Register for 123456778 bustcpclient.write ("100", 123.4 56);//write Register 100-101 is 123.456 bustcpclient.write ("12312312312414L");//write Register 100-103 is a big data bustcpclient.write (" 12634534534543656UL);//write Register 100-103 for a large data bustcpclient.write ("", 123.456d);//write Register 100-103 as a double-precision data bustcp     Client.write ("n", "K123456789"); }

Demo of the Test

If interested, you can download the demo test, download 2 software, one is the server software, one is the client software

Hslcommunicationdemo.zip

Modbustcpserver.zip

Open the server first and start the service

Then open the profiled client

After setting the relevant parameters, start the server, the default parameters are as follows, start successfully, will pop up the message box "Wait for the server connection"

Then we go back to the server interface, click the Connect Special client button, pop up as follows: Then the IP address modification of the cost machine, the port changed to just fill in the client, the ID and the client's consistent

Then the server Interface bullet box, the connection succeeds. Then we can go back to the client interface for the data read and write interaction.

MODBUS-TCP client Communication for communication listening mode between the profiled Modbus client and the profiled Modbus server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.