The serial | Internet of Things Framework Serversuperio Tutorial-6. Development of concurrent communication mode and matters needing attention

Source: Internet
Author: User



1.c# Cross-platform IoT communication Framework Serversuperio (Ssio) Introduction



The serial | Internet of Things Framework Serversuperio Tutorial 1.4 Communication mode mechanism.



The serial | Internet of Things framework Serversuperio Tutorial 2. Configuration parameter description of the service instance



The serial | Internet of Things Framework Serversuperio Tutorial-3. Introduction to Device drivers



The serial | IoT Framework Serversuperio Tutorial-4. Develop a set of device drivers that support both serial and network communication.



The serial | Internet of Things Framework Serversuperio Tutorial-5. Polling communication mode development and considerations.






Directory



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



6.1 Overview ... 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 priority to send additional data ... 3



6.3.3 How to choose 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 concurrency mode Run effect ... 6





6. Concurrency communication mode Development and considerations  
6.1 Overview


The concurrent communication mode can only be used for network communication devices, mainly to enhance the concurrency capability of communication, send request data centrally, and receive the return data asynchronously. The interval between sending request data centrally can be set, and receiving the data asynchronously is related to how to allocate data to the corresponding device driver, mainly through two ways: IP address and device code, the former applies to the case that the device terminal is a fixed IP address. The latter applies to the device terminal is a dynamic IP situation, such as: DTU, GPRS, 3g/4g and other wireless communication methods.



The concurrent communication mode is essentially a call-answering mode of communication, similar to the polling communication pattern, but more efficient than polling the data for a communication pattern.


6.2 Description of the communication mechanism


In the case of network communication, the polling mode is obviously less efficient, then the concurrency mode can be used. The concurrent traffic mode is centralized to all device request instructions, the framework is the use of cyclic synchronization to send the request command to each IO channel corresponding device, of course, can also be in parallel to send request commands in a centralized manner. After the hardware equipment receives the instruction to verify, after the verification succeeds returns the corresponding instruction the data, the communication platform asynchronously hears the data information, carries on the reception operation, then carries on the data the dissemination, the processing and so on.



So here is the data received by the IO Channel is received asynchronously, how to match the device driver matching (data distribution to the device driver), this can be over Devicecode and DEVICEIP two ways to achieve. Devicecode can be a device address or device encoding, DEVICEIP is pre-set parameters, requires the terminal device IP address is fixed.



Communication structures such as:



`


6.3 Device-Driven development considerations 6.3.1 send data in real time


The Serversuperio framework polls and dispatches all devices to send a request real-time data command to the device in a call-answering manner, which is generally relatively fixed for the request-real-time data command for the same device. When scheduling a specific device driver, 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", "a", null);            String hexs = Binaryutil.bytetohex (data);            Ondeviceruninglog ("Send >>" +hexs);            return data;        }


This. The protocol.driverpackage driver invokes the 61 command to get the command to be sent and returns the byte[] array, and the Serversuperio obtains the data and sends the command data via the IO interface automatically. If a null type is returned, the system does not release the operation.


6.3.2 priority to send additional data


For a device can not have only one read real-time data command, there may be other commands to interact, such as: Read parameters, real-time calibration, etc., it is necessary to carry out priority scheduling to send data information. There are two ways to get the Serversuperio framework to prioritize the device driver.


    1. Add the command to the send data cache, and the framework will automatically delete the data after it is obtained from the cache, with the following code:
This. PROTOCOL.SENDCACHE.ADD ("Reading Parameters", readparabytes);


2. Set the priority property of the device, as shown in the following code:


This. devicepriority=devicepriority.priority;
6.3.3 How to select IO Channel to send data


When sending data centrally, it involves how to correlate device drivers with IO channels, and the framework chooses the IO channel to send data with the terminal IP parameters set by DeviceParameter.NET.RemoteIP. However, if the terminal device is a dynamic IP address, then the REMOTEIP parameter should also be variable. At this point, you need to set up a service instance is distributed data to the device driver in a devicecode way, the terminal equipment sends the simple authentication data, guarantees the transmission Devicecode and the device drive counterpart, the device driver receives the verification data to need to save the temporary REMOTEIP information, This ensures that when the data is sent, the parameters accurately locate the IO channel to the terminal device to request the data.



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 setting allocates data in Deliverymode.devicecode mode, then it is necessary to implement the interface of filtering Devicecode encoding 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 you create a service instance in the host program, you need to set the configuration parameters of the service instance to the concurrent traffic mode and start the service instance to 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 = Netreceivetimeout = +, netsendtimeout = 1000, Controlmode = controlmode.parallel, Socketmode = socketmode.tcp, startreceivedataflit ER = false, Clearsocketsession = False, Startcheckpackagelength = False, Chec            Ksamesocketsession = 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 is to set the service instance scheduling device to be the concurrency control mode, and the data distribution in DeliveryMode = Deliverymode.devicecode mode, of course, I am now simulating the dependent terminal IP.


6.5 Concurrency mode Run effect


1. Picture






2. Video









1.[serial] "C # communication (Serial and network) framework design and implementation"



2.[Open source]c# cross-platform IoT communication Framework Serversuperio (Ssio) Introduction



2. Superio (SIO) and open-source cross-platform IoT framework Serversuperio (SSIO) building a system's overall solution



3.c# Technology Roadmap for industrial IoT and Integrated system solutions (data source, , upload and receive, ActiveMQ, Mongodb, WEBAPI, mobile app)



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



Internet of Things & Integration technology (. NET) QQ group :54256083



The serial | Internet of Things Framework Serversuperio Tutorial-6. Development of concurrent communication mode and matters needing attention


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.