Communication between Alien Modbus Client and Alien Modbus Server Listening mode modbus-tcp client communication

foreword


 This article will use a component technology disclosed by Github to implement a profiled ModBus TCP client, which can easily read and write to profiled Modbus tcp server. Can be any other server that supports this communication protocol.

github address: https://github.com/dathlin/HslCommunication  If you like, you can star or fork, and you can also give a reward for support.

The source code of the DEMO project below this article is in this address

 

You can download and install it in the NuGet manager in Visual Studio, or you can directly enter the following instructions in the NuGet console to install

Install-Package HslCommunication

 

NuGet installation tutorial  http://www.cnblogs.com/dathlin/p/7705014.html

Technical support QQ group: 592132877 (The version update details of the component will also be released in the group as soon as possible) Component API address: http://www.cnblogs.com/dathlin/p/7703805.html

 

About Alien Mode, also known as Listening Client


The usual mode is the server-client mode, where the client initiates the connection and then exchanges data with the server. However, there is another mode in which the server actively initiates the connection to the client, and then the client executes the connection to the server. The data interaction may sound a little awkward, but it does exist, and it is usually used in some 4G Internet-connected devices in industrial sites. Of course, some custom functions can also be implemented. Need to slowly look at the code below.

 

The alien mode is mainly composed of two parts, the listening server and the alien modbus-tcp client.

The listening server is responsible for listening for network requests and Alien protocol verification, and after passing the connection, it throws the connection to the alien modbus-tcp client for use.

 

 

Registration Package and Alien Protocol


After the modbus server is connected to the ip and port address of the cloud server, it needs to send a registration package information. This registration package information needs to follow the following protocol, called the Alien protocol, and other protocol support needs to be completed by secondary custom development. The details of this agreement are as follows:

The registration package has a total of 28 bytes,

// registration 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 0x
+----------------- 0xA8 0x -+ +----+ +-----+ +---------------------------------- -------------------------------------------+ +----- -----------------------------------+ +------------- --------------+ +-------------+
+ Fixed message header + Spare back length DTU code 12345678901 (unique identifier, such as mobile phone number) Login Password (untrusted excluded) Ip: 192.168.0.1 port 10000
+------------------+     +----+     +-----+        +----------------------------------------------------------------------------+          +-----------------------------------------+         +-------------------------+       +--------------+

 

// Return the message, the following is the normal successful registration information

0x48 0x73 0x6E 0x00 0x01 0x00
+----- + +----+ +----+ +----+
    fixed message header spare back length result code

 

// result code

 0x00: Login successful
 0x01: DTU repeated login 
 0x02: DTU prohibits login
 0x03: Password verification failed

 

 

listen for requests


The first step is to establish a listening server. The reason why the triggered event has two parameters is because when you have multiple servers, you can associate a method together, and then distinguish the signal through the network. The core code is as follows, At that time, just call the NetworkAlienStart method.

        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 will be triggered when there is a connection
        }

 

 

Alien Modbus-Tcp


To build a special-shaped client, the data interaction block is almost the same as the original client, only the connection principle is different. After setting it as a special-shaped client, it is no longer responsible for the connection function, and becomes a passive setting connection object. Function. The code is also very convenient

1. First define the object

private ModbusTcpNet busTcpClient = null;

 

2. Instantiate, IP and port are randomly specified, anyway, it is useless for alien clients, but the station number is still useful

busTcpClient = new ModbusTcpNet( textBox1.Text, port, station );

3. Set to special-shaped mode, of course, you can not set it first, you can adjust to the fourth step and then set it

busTcpClient.ConnectServer( null );

4. Modify the callback method for request listening

private void NetworkAlien_OnClientConnected( NetworkAlienClient network, AlienSession session )
        {
            busTcpClient.ConnectServer( session );
        }

 

When a modbus server is connected, this method is triggered, and data can be read and written to the server through busTcpClient. The situation demonstrated above is only for one client, what if there are multiple clients? We can associate with each other through a unique identification code. For each incoming session data, it also includes the DTU unique code of the connection request, and we can make a judgment.

We first set the unique code of the alien client

busTcpClient.ConnectionId = "12345678901"

Modify the callback method again

private void NetworkAlien_OnClientConnected( NetworkAlienClient network , AlienSession session )
        {
            if(session.DTU == busTcpClient.ConnectionId)
            {
                busTcpClient.ConnectServer( session );
            }
        }

 

The usual method is to define your own client array first, specify the DTU code, and then perform data request operations on the array in batches.

 

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

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

 

Paste some sample code below

 

private void userButton30_Click(object sender, EventArgs e)

{

    // read operation

    bool coil100 = busTcpClient.ReadCoil("100").Content; // Read the on-off of coil 100

    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 registers 100-101

    uint uint100 = busTcpClient.ReadUInt32("100").Content; // read the uint value of registers 100-101

    float float100 = busTcpClient.ReadFloat("100").Content; // read the float value of registers 100-101

    long long100 = busTcpClient.ReadInt64("100").Content; // read the long value of registers 100-103

    ulong ulong100 = busTcpClient.ReadUInt64("100").Content; // Read the ulong value of registers 100-103

    double double100 = busTcpClient.ReadDouble("100").Content; // read the double value of registers 100-103

    string str100 = busTcpClient.ReadString("100", 5).Content;// Read a string of 10 characters from 100 to 104

 

    // write operation

    busTcpClient.WriteCoil("100", true);//Write coil 100 to pass

    busTcpClient.Write("100", (short)12345);//Write register 100 to 12345

    busTcpClient.Write("100", (ushort)45678);//Write register 100 to 45678

    busTcpClient.Write("100", 123456789);// Write register 100-101 to 123456789

    busTcpClient.Write("100", (uint)123456778);// Write register 100-101 to 123456778

    busTcpClient.Write("100", 123.456);//Write register 100-101 to 123.456

    busTcpClient.Write("100", 12312312312414L);//Write registers 100-103 as a large data

    busTcpClient.Write("100", 12634534534543656UL);// Write registers 100-103 as a large data

    busTcpClient.Write("100", 123.456d);//Write registers 100-103 as a double-precision data

    busTcpClient.Write("100", "K123456789");

     

}

 

 

 

Test Demo


If you are interested, you can download the Demo test and download 2 softwares, one is the server software and the other is the client software

HslCommunicationDemo.zip

ModbusTcpServer.zip

 

First open the server and start the service

 

Open the alien client again

After setting the relevant parameters, start the server. The default parameters are as follows. If the startup is successful, a message box will pop up [Waiting for server connection]

 

 

Then we go back to the server interface, click the Connect Alien Client button, and the following pops up: Then change the IP address to the local machine, change the port to the one just filled in the client, and the ID is the same as the client

 

Then the server interface pops up and the connection is successful. Then we can go back to the client interface for data read and write interaction.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325077311&siteId=291194637