The serial | IoT Framework Serversuperio Tutorial-7. Self-control communication mode development 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.

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

Directory

7. Self-control communication mode development and matters needing attention ... 2

7.1 Overview ... 2

7.2 Communication Mechanism Description ... 2

7.3 Device-Driven development considerations ... 3

7.3.1 send data in real time ... 3

7.3.2 Send fixed real-time Request data Command ... 4

7.3.3 priority to send additional data ... 4

7.3.4 How to choose IO Channel to send data ... 5

7.3.5 How to allocate data with Devicecode ... 5

7.3.6 How to change the state of the device driver ... 6

7.4 Host Program Service instance configuration considerations ... 6

7.5 Self-control mode operation effect ... 8

7. Self-control communication mode development and matters needing attention  
7.1 Overview

The self-control communication mode is similar to the concurrent communication mode, the only difference is that the request data command is sent, the automatic communication mode can use the timer, the request data command is sent periodically, and it is not sent centrally like the concurrent communication mode.

In the industrial IoT construction, the equipment is different, the protocol is different, the scene is different, for some different equipment timing data acquisition frequency is not the same, too high-frequency data acquisition is also a waste of resources, so it is two times developers in the development of equipment driven by more autonomous control mode.

7.2 Description of the communication mechanism

This control mode can be used only for network communication. The self-control communication mode is similar to the concurrent communication mode, the difference is that the sending instruction operation is given to the device driver itself for control, or to two developers, two times the developer can send the instruction data in event-driven manner through the clock timing. 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.

The self-control communication mode can provide accurate timing request real-time data mechanism for two time developers, make communication mechanism more flexible and autonomous, if multiple device driver share use the same IO channel, TIME control will be biased.

Also involves the distribution of data, as is the case with concurrency patterns. Communication structures such as:

7.3 Device-Driven development considerations 7.3.1 send data in real time

The Irundevice driver interface of the Serversuperio framework has a getsendbytes function that simultaneously coordinates the buffer that invokes the Getconstantcommand fixed request data interface and Sendcache send data. and set the priority level of the device for scheduling.

You can inherit the previously written device driver, and on this basis, add the code that sends the data at a scheduled time. The code is as follows:

public class Deviceselfdriver:devicedriver {public        deviceselfdriver (): Base ()        {        } public        override void Initialize (String devid)        {            base. Initialize (devid);            This. Runtimerinterval =;            This. Isruntimer = true;        }        public override void Onruntimer ()        {            byte[] data = this. Getsendbytes ();            Onsenddata (data);            Base. Onruntimer ();        }  }
7.3.2 Send fixed real-time Request data command

The self-control communication mode sends the request data command periodically, and also sends the request real-time data command to the device in the way of call answering, which is generally relatively fixed for the request real-time data command of 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.

7.3.3 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;
7.3.4 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);            ......}
7.3.5 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 ();}}        }
7.3.6 How to change device-driven status

Unlike polling the communication mode, sending the data, receiving the data is a reincarnation, after the process of receiving the data to drive the device driver, the device to perform the entire life cycle of the process, according to the received data, will automatically change the state of the device driver.

Automatic communication mode and concurrent communication mode more emphasis on request data in different ways, then can not always send the request data command, and the device state has not changed, such as: communication has become a normal communication interruption, communication interruption into a normal communication. The transmission and reception process of the two communication modes have a coordination mechanism, send 3 requests data command, without receiving any data, will automatically invoke the device-driven interface to drive the entire execution of the device-driven process, so that the state of the device will automatically change, without the need to write the corresponding code two times.

7.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 Automatic communication 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) {devicedriver dev1 = new Devicedriver (); Dev1.            Deviceparameter.devicename = "Serial Device"; Dev1.            deviceparameter.deviceaddr = 0; Dev1.            Deviceparameter.deviceid = "0"; Dev1.            Devicedynamic.deviceid = "0"; Dev1.            Deviceparameter.devicecode = "0";            Dev1.DeviceParameter.COM.Port = 1;            Dev1.DeviceParameter.COM.Baud = 9600;            Dev1.communicatetype = communicatetype.com; Dev1.            Initialize ("0");            Deviceselfdriver Dev2 = new Deviceselfdriver (); Dev2.            Deviceparameter.devicename = "Network Device"; Dev2.            DEVICEPARAMETER.DEVICEADDR = 1; Dev2.            Deviceparameter.deviceid = "1"; Dev2.            Devicedynamic.deviceid = "1"; Dev2.            Deviceparameter.devicecode = "1";            Dev2.DeviceParameter.NET.RemoteIP = "127.0.0.1";            Dev2.DeviceParameter.NET.RemotePort = 9600;       Dev2.communicatetype = communicatetype.net;     Dev2.            Initialize ("1"); IServer Server = new Servermanager ().                Createserver (New Serverconfig () {ServerName = "Service 1", Comreadtimeout = 1000,                Comwritetimeout = Netreceivetimeout = +, netsendtimeout = 1000,  Controlmode = controlmode.self, Socketmode = socketmode.tcp, Startreceivedatafliter = False, Clearsocketsession = False, Startcheckpackagelength = False, Checksam            Esocketsession = False, DeliveryMode = Deliverymode.devicecode,}); Server.            adddevicecompleted + = server_adddevicecompleted; Server.            deletedevicecompleted+=server_deletedevicecompleted; Server.            Start (); Server.            AddDevice (DEV1); Server.            AddDevice (DEV2); while ("exit" = = Console.ReadLine ()) {server.            Stop (); }}

Controlmode = Controlmode. The self code is to set the service instance dispatch device to the concurrency control mode, and the data is distributed in DeliveryMode = Deliverymode.devicecode mode, of course I am now simulating a fixed terminal IP.

7.5 Self-control mode operation 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, data acquisition, 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 | IoT Framework Serversuperio Tutorial-7. Self-control communication mode development 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.