当前位置:网站首页>Serial | Internet of things framework serversuper tutorial - 6. Concurrent communication mode development and precautions

Serial | Internet of things framework serversuper tutorial - 6. Concurrent communication mode development and precautions

2021-09-15 04:32:13 Ineuos industrial Internet operating system

Concurrent communication mode can only be used for network communication equipment , It is mainly to strengthen the concurrency of communication , Send request data centrally , Receive return data asynchronously . The interval between centralized sending of request data can be set ; Receiving the returned data asynchronously involves how to allocate the data to the corresponding device driver , Mainly in two ways :IP Address mode and equipment Code The way , The former applies when the equipment terminal is fixed IP Address , The latter applies to devices where the terminal is dynamic IP The situation of , for example :DTU、GPRS、3G/4G Other wireless communication modes . Concurrent communication mode is essentially the communication mode of call response , Similar to polling communication mode , But it is more efficient than the data collection in polling communication mode .

1.C# Cross platform Internet of things communication framework ServerSuperIO(SSIO) Introduce

《 Serial | The Internet of things framework ServerSuperIO course 》1.4 A communication mode mechanism .

《 Serial | The Internet of things framework ServerSuperIO course 》2. Description of configuration parameters of service instance

《 Serial | The Internet of things framework ServerSuperIO course 》- 3. Introduction to device driver

《 Serial | The Internet of things framework ServerSuperIO course 》-4. Such as developing a set of device drivers , Support serial port and network communication at the same time .

《 Serial | The Internet of things framework ServerSuperIO course 》- 5. Polling communication mode development and precautions .

 

Objective        record

6. Concurrent communication mode development and precautions ... 2

6.1           summary ... 2

6.2           Communication mechanism description ... 2

6.3           Device driven development considerations ... 3

6.3.1    Send data in real time ... 3

6.3.2    Send other data first ... 3

6.3.3    How to choose IO Channel send data ... 4

6.3.4    How to use DeviceCode Assign data ... 4

6.4           Host service instance configuration considerations ... 5

6.5           The running effect of concurrent mode ... 6

 

6. Concurrent communication mode development and precautions  

6.1    summary

     Concurrent communication mode can only be used for network communication equipment , It is mainly to strengthen the concurrency of communication , Send request data centrally , Receive return data asynchronously . The interval between centralized sending of request data can be set ; Receiving the returned data asynchronously involves how to allocate the data to the corresponding device driver , Mainly in two ways :IP Address mode and equipment Code The way , The former applies when the equipment terminal is fixed IP Address , The latter applies to devices where the terminal is dynamic IP The situation of , for example :DTU、GPRS、3G/4G Other wireless communication modes .

     Concurrent communication mode is essentially the communication mode of call response , Similar to polling communication mode , But it is more efficient than the data collection in polling communication mode .

6.2    Communication mechanism description

     In the case of network communication , Polling mode is obviously inefficient , Then you can use the concurrent mode . The concurrent communication mode is to send request instructions to all devices in a centralized manner , The framework uses cyclic synchronization to send request commands to each server IO The device corresponding to the channel , Of course, you can also send request commands in parallel and asynchronous mode . The hardware device checks after receiving the instruction , After successful verification, the data of the corresponding instruction is returned , After the communication platform asynchronously monitors the data information , Receive operation , Then distribute the data 、 Processing, etc .

     So this is about IO The data received by the channel is received asynchronously , How to match the device driver ( Distribute data to device drivers ), It's OK DeviceCode and DeviceIP Two ways to achieve .DeviceCode It can be a device address or a device code ,DeviceIP Is a preset parameter , Requirements for terminal equipment IP The address is fixed .

     The communication structure is shown in the figure below :

`《 Serial  |  The Internet of things framework ServerSuperIO course 》- 6. Concurrent communication mode development and precautions _ data

6.3    Device driven development considerations

6.3.1    Send data in real time

     ServerSuperIO The framework will poll and schedule all devices , Send a command requesting real-time data to the device in the form of call response , For the request of the same device, the real-time data command is generally relatively fixed . When scheduling a specific device driver , Will call a fixed call IRunDevice Driver interface GetConstantCommand function , To get the command to request real-time data . The code is as follows :

       public override byte[] GetConstantCommand()
        {
            byte[] data = this.Protocol.DriverPackage<String>("0", "61", null);
            string hexs = BinaryUtil.ByteToHex(data);
            OnDeviceRuningLog(" send out >>"+hexs);
            return data;
        }

       
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

     this.Protocol.DriverPackage Drive call 61 Command get the command to send , And back to byte[] Array ,ServerSuperIO After obtaining the data, it will automatically pass IO Interface issues command data . If you return null type , The system does not issue .

6.3.2    Send other data first

It is impossible for a device to have only one command to read real-time data , There may be other commands to interact with , for example : Read parameters 、 Real time calibration, etc , At this time, priority scheduling is needed to send data information . There are two ways to make ServerSuperIO The framework gives priority to scheduling the device driver .

  1. Add the command to the send data cache , After the framework obtains data from the cache, it will be deleted automatically , The code is as follows :
this.Protocol.SendCache.Add(" Read parameters ",readParaBytes);

       
  • 1.

      2. Set the priority attribute of the device , The code is as follows :

this.DevicePriority=DevicePriority.Priority;

       
  • 1.

6.3.3    How to choose IO Channel send data

     When sending data centrally , It involves how to associate device drivers with IO passageway , The frame will be in DeviceParameter.NET.RemoteIP Set the terminal IP Parameter selection IO Channel send data . But if the terminal device is dynamic IP Address if , that RemoteIP Parameters should also be variable . At this time, you need to set the service instance to DeviceCode Distribute data to device drivers in a way , The terminal device sends simple verification data first , Guaranteed to send DeviceCode Corresponding to equipment drive , After receiving the verification data, the device driver needs to save the temporary data RemoteIP Information , This ensures that the parameters accurately find the data to be requested when sending data IO Channel to terminal equipment .

     For example, the following code :

public override void Communicate(ServerSuperIO.Communicate.IRequestInfo info)
{
            this.DeviceParameter.NET.RemoteIP = info.Channel.Key;
            this.DeviceParameter.Save(this.DeviceParameter);
            ……
}

       
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

6.3.4    How to use DeviceCode Assign data

     If the service instance is set to DeliveryMode.DeviceCode Pattern assignment data , Then you need to implement filtering in the communication protocol interface DeviceCode Coded interface .

     For example, the following code :

  internal class DeviceProtocol:ProtocolDriver
    {
        public override string GetCode(byte[] data)
        {
            byte[] head = new byte[] {0x55, 0xaa};
            int codeIndex = data.Mark(0, data.Length, head);
            if (codeIndex == -1)
            {
                return String.Empty;
            }
            else
            {
                return data[codeIndex + head.Length].ToString();
            }
        }
}

       
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

6.4    Host service instance configuration considerations

     When creating a service instance in the host program , You need to set the configuration parameters of the service instance to the concurrent communication mode , And start the service instance , Add the instantiated device driver to the service instance . The code is as follows :

static void Main(string[] args)
{
            IServer server = new ServerFactory().CreateServer(new ServerConfig()
            {
                ServerName = " service 1",
                ComReadTimeout = 1000,
                ComWriteTimeout = 1000,
                NetReceiveTimeout = 1000,
                NetSendTimeout = 1000,
                ControlMode = ControlMode.Parallel,
                SocketMode = SocketMode.Tcp,
                StartReceiveDataFliter = false,
                ClearSocketSession = false,
                StartCheckPackageLength = false,
                CheckSameSocketSession = false,
                DeliveryMode = DeliveryMode.DeviceCode,
                ParallelInterval = 1000
            });
            server.AddDeviceCompleted += server_AddDeviceCompleted;
            server.DeleteDeviceCompleted += server_DeleteDeviceCompleted;
            server.Start();

            string devCode = "0";
            DeviceDriver dev1 = new DeviceDriver();
            dev1.DeviceParameter.DeviceName = " Device drivers "+ devCode.ToString();
            dev1.DeviceParameter.DeviceAddr = int.Parse(devCode);
            dev1.DeviceParameter.DeviceCode = devCode.ToString();
            dev1.DeviceParameter.DeviceID = devCode.ToString();
            dev1.DeviceDynamic.DeviceID = devCode.ToString();
            dev1.DeviceParameter.NET.RemoteIP = "127.0.0.1";
            dev1.DeviceParameter.NET.RemotePort = 9600;
            dev1.CommunicateType = CommunicateType.NET;
            dev1.Initialize(devCode.ToString());
            server.AddDevice(dev1);

            devCode = "1";
            DeviceDriver dev2 = new DeviceDriver();
            dev2.DeviceParameter.DeviceName = " Device drivers " + devCode.ToString();
            dev2.DeviceParameter.DeviceAddr = int.Parse(devCode);
            dev2.DeviceParameter.DeviceCode = devCode.ToString();
            dev2.DeviceParameter.DeviceID = devCode.ToString();
            dev2.DeviceDynamic.DeviceID = devCode.ToString();
            dev2.DeviceParameter.NET.RemoteIP = "192.168.1.102";
            dev2.DeviceParameter.NET.RemotePort = 9600;
            dev2.CommunicateType = CommunicateType.NET;
            dev2.Initialize(devCode.ToString());
            server.AddDevice(dev2);

            while ("exit" == Console.ReadLine())
            {
                server.Stop();
            }
}

       
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.

    ControlMode = ControlMode. Parallel The code is to set the service instance scheduling device to concurrency control mode ; With DeliveryMode = DeliveryMode.DeviceCode Data distribution by , Of course, what I'm simulating now is a deterministic terminal IP.

6.5    The running effect of concurrent mode

1. picture

 《 Serial  |  The Internet of things framework ServerSuperIO course 》- 6. Concurrent communication mode development and precautions _ Device drivers _02

 

 


 

1.[ Serial ]《C# Communications ( Serial port and network ) The design and implementation of the framework 》

2.[ Open source ]C# Cross platform Internet of things communication framework ServerSuperIO(SSIO) Introduce

2. application SuperIO(SIO) And open source cross platform Internet of things framework ServerSuperIO(SSIO) Build the overall scheme of the system

3.C# The technical route of industrial Internet of things and integrated system solutions ( data source 、 Data collection 、 Data upload and receive 、ActiveMQ、Mongodb、WebApi、 mobile phone App)

5.ServerSuperIO Open source address : https://github.com/wxzz/ServerSuperIO

The Internet of things & Integrated technology (.NET) QQ Group 54256083 

 
 
 
 

版权声明
本文为[Ineuos industrial Internet operating system]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/09/20210909112709634v.html

随机推荐