Serialization | Iot framework ServerSuperIO tutorial-6. concurrent communication mode development and precautions, and IOT framework

Source: Internet
Author: User

Serialization | Iot framework ServerSuperIO tutorial-6. concurrent communication mode development and precautions, and IOT framework

1. C # Introduction to cross-platform Iot communication framework ServerSuperIO (SSIO)

Serialization | Iot framework ServerSuperIO tutorial 1.4 communication modes and mechanisms.

Serialization | Iot framework ServerSuperIO tutorial 2. service instance configuration parameters

Serialization | Iot framework ServerSuperIO tutorial-3. Device Driver Introduction

Serialization | Iot framework ServerSuperIO tutorial-4. For example, you can develop a device driver that supports both serial and network communication.

Serialization | Iot framework ServerSuperIO tutorial-5. Polling communication mode development and precautions.

 

Contents

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

6.1 Overview... 2

6.2 communication mechanism description... 2

6.3 precautions for device driver development

6.3.1 sending data in real time... 3

6.3.2 sending other data first... 3

6.3.3 how to select the IO channel to send data... 4

6.3.4 how to allocate data with DeviceCode... 4

6.4 host program service instance configuration considerations... 5

6.5 concurrent mode running effect... 6

 

6. concurrent communication mode development and precautions  
6.1 Overview

The concurrent communication mode can only be used for network communication devices. It mainly enhances the concurrency capability of communication, centrally sends request data, and asynchronously receives returned data. You can set the interval between sending request data in a centralized manner. asynchronous receiving of returned data involves how to allocate data to the corresponding device driver. There are two methods: the IP address method and the device Code method. The former is applicable when the device terminal is a fixed IP address, and the latter is applicable when the device terminal is a dynamic IP address, for example: DTU, GPRS, 3G/4G, and other wireless communication methods.

The concurrent communication mode is essentially a call response communication mode. It is similar to the polling communication mode, but it is more efficient than the polling communication mode.

6.2 communication mechanism description

In the case of network communication, the polling mode is obviously less efficient, so the concurrent mode can be used. The concurrent communication mode is to send request commands centrally to all devices. The framework sends request commands cyclically and synchronously to the corresponding devices of each IO channel, of course, you can also use the parallel Asynchronous Method to send request commands in a centralized manner. The hardware device verifies the received commands and returns the data of the corresponding commands after the verification is successful. After the communication platform asynchronously listens to the data information, it receives the commands, then, data is distributed and processed.

This involves how the data received by the I/O channel is asynchronously received and matched with the device driver (data is distributed to the device driver ), this can be achieved through two methods: DeviceCode and DeviceIP. DeviceCode can be the device address or device code. DeviceIP is a preset parameter and requires that the IP address of the terminal device be fixed.

The communication structure is as follows:

`

6.3 precautions for device driver development 6.3.1 send data in real time

The ServerSuperIO framework polls and schedules all devices, and sends real-time request data commands to devices in the form of call responses. The real-time request data commands for the same device are generally relatively fixed. When a specific device driver is scheduled, a fixed call to the GetConstantCommand function of the IRunDevice driver interface is called to obtain 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 >>" + hexs);
             return data;
         }

This. Protocol. DriverPackage driver calls the 61 command to obtain the command to be sent and returns the byte [] array. After ServerSuperIO obtains the data, it will automatically issue the command data through the IO Interface. If the return value belongs to the null type, the system does not issue the data.

6.3.2 sending other data preferentially

A device cannot have only one command to read Real-time Data. Other commands may interact with each other, such as read parameters and real-time calibration. In this case, priority scheduling is required to send data information. The ServerSuperIO framework can prioritize the scheduling of the device driver in two ways.

This. Protocol. SendCache. Add ("read parameter", readParaBytes );

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

this.DevicePriority=DevicePriority.Priority;
6.3.3 how to select an I/O channel to send data

When sending data in a centralized manner, how to associate the device driver with the I/O channel is involved. The framework selects the I/O channel to send data using the terminal IP parameter set by DeviceParameter. NET. RemoteIP. However, if the terminal device is a dynamic IP address, the RemoteIP parameter should also be changed. In this case, you need to set the service instance to distribute data to the device driver in the form of DeviceCode. The terminal device first sends simple verification data to ensure that the sent DeviceCode corresponds to the device driver, after the device driver receives the verification data, it needs to save the temporary RemoteIP information, so that when sending the data, the parameter can accurately find the I/O channel to request the data to the terminal device.

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);
            ……
}
6.3.4 how to allocate data with DeviceCode

If the service instance is set to allocate data in DeliveryMode. DeviceCode mode, you need to implement the DeviceCode filtering interface in the communication protocol 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();
            }
        }
}
6.4 host program 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, start the service instance, and 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 driver" + 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 driver" + 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 ();
            }
} 

ControlMode = ControlMode. The Parallel code sets the scheduling device of the service instance as the concurrency control mode. It uses DeliveryMode = DeliveryMode. DeviceCode for data distribution. Of course, I am simulating a specific terminal IP address.

6.5 concurrent mode Running Effect

1. Images

 

2. Video

 

 

1. [serialization] C # communication (Serial Port and network) Framework Design and Implementation

2. [Open Source] C # cross-platform Iot communication framework ServerSuperIO (SSIO) Introduction

2. Overall system construction solution using SuperIO and open-source cross-platform Iot framework ServerSuperIO

3. C # technical route of industrial IoT and integrated system solutions (data sources, , data upload and receiving, ActiveMQ, Mongodb, WebApi, and mobile App)

5. ServerSuperIO Open Source Address: https://github.com/wxzz/ServerSuperIO

Internet of Things & integrated technology (. NET) QQ Group:54256083


Related Article

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.