Open source Pure C # Industrial Gateway + configuration software (four) communication principle of the upper and lower machine

Source: Internet
Author: User

First, Gateway features: Connecting

Recently a little busy, the update is slow. Thanks to the support of the park friends, there are now on GitHub . The goal is the best open source configuration, it seems a step closer ^ ^

Previously mentioned is the gateway is the key link of the Internet of things, it is the role of connecting .

The lower machine has the language of the lower machine, the upper computer has the idea of the upper machine. The gateway is a translation, the language of the next machine into a word, and then tell the host computer how to do.

This process of translation should ensure that:

    1. real-time sex . If it is too slow, the upper and lower machine is obviously out of sync, there will be problems.
    2. accuracy . The signal can not be lost frequently, lost step, jump step, can not have too much error, and will not bring too much interference and noise.
    3. stability . If a fault occurs, such as a broken communication, to be able to automatically re-connect, strong enough, not prone to crashes, an unexpected crash, to be able to automatically restart, there is a fault-tolerant mechanism and error log, and so on.

There is still a challenge to achieve these metrics.

Second, Communication Blueprint: OPC specification

Gateway seems to be a very mysterious thing, on the internet to find a lot of relevant information, found to focus on one point: OPC specification.

OPC is like a lamp that illuminates the way forward. OPC specification is everyone wrote, focused on the wisdom of industry experts, standing on the shoulders of giants, can go a lot less detours.

The OPC specification defines data acquisition, archiving, alerting, and a complete interface demonstration.

The OPC data acquisition specification contains such important concepts as:

    1. Data reading and writing methods include the lower computer batch push data, the host computer read and write data separately.
    2. Can read and write asynchronously or synchronously.
    3. The data includes metadata (attributes of the data, such as data type, length, name, and so on) and process data (ID, value, timestamp, quality stamp).
    4. A level three schema that contains the drive (Driver)-Group-variable (Item). Variables can be grouped on demand, some groups are read only, some groups need to be refreshed 1 seconds, and some can be as long as 5 minutes, to differentiate to improve efficiency.
    5. To be able to determine if the driver is disconnected, to provide an event that is disconnected or closed for application processing.

In short, the information is very rich, the inspiration is very large, my gateway program to a large extent, reference to the OPC specification.

But OPC has its inherent flaw: relying on Microsoft's COM component technology, not cross-platform, while COM is an outdated technology, the configuration of communication on different hosts is cumbersome and insecure.

Therefore, I transformed the implementation of my own class OPC server. The basic communication process is: batch polling of the lower machine-compared to the previous cycle of data-extraction of changes in data-batch to the host computer.

Third, communication with lower machine: Batch polling

    • Features of the lower machine-why use polling

The characteristics of the next-computer communication:

    1. the lower machine Many adopt the Master-slave mode. That is, the information sent by the host can be transferred to each slave or the specified slave, and the information from each slave is sent to the host only. The host uses the query method to receive the sending data, and the slave uses the interrupt mode to receive the sending data. This model is naturally suitable for polling.
    2. Most of the lower machine has only one communication port, some or serial port, nature is not suitable for push mode.
    3. Many of the lower-level computer is a single-chip, subscription-release mode is often more complex logic, the program is difficult to write, the chip and storage requirements will inevitably improve. Therefore, the use of this model of the lower machine is very few.

Polling is the gateway as a host, periodically requesting data from the next computer. If you implement bulk requests and reduce round trips, the efficiency of polling is not low. Thousands of variable polling cycle of 500 ms (Siemens), no pressure.

Polling is in groups (group). The group inherits from the igroup interface:

  Public InterfaceIgroup: IDisposable {BOOLIsActive {Get;Set; }  ShortID {Get; } intupdaterate {Get;Set; } floatDeadband {Get;Set; } stringName {Get;Set; } Idriver Parent {Get; } Idataserver Server {Get; } IEnumerable<ITag> Items {Get; } BOOLAdditems (ilist<tagmetadata>items); BOOLAddtags (ienumerable<itag>tags); BOOLRemoveitems (paramsitag[] items); BOOLSetactivestate (BOOLActiveparams  Short[] items);        ITag finditembyaddress (deviceaddress addr); Historydata[] Batchread (DataSource source,BOOLIssync,paramsitag[] itemarray); intBatchWrite (Sorteddictionary<itag,Object> Items,BOOLIssync =true); ItemData<int> ReadInt32 (deviceaddress address, DataSource Source =Datasource.cache); ItemData< Short> ReadInt16 (deviceaddress address, DataSource Source =Datasource.cache); ItemData<byte> readbyte (deviceaddress address, DataSource Source =Datasource.cache); ItemData<float> readfloat (deviceaddress address, DataSource Source =Datasource.cache); ItemData<BOOL> Readbool (deviceaddress address, DataSource Source =Datasource.cache); ItemData<string> ReadString (deviceaddress address, DataSource Source =Datasource.cache); intWriteInt32 (deviceaddress address,intvalue); intWriteInt16 (deviceaddress address, Shortvalue); intWritefloat (deviceaddress address,floatvalue); intWriteString (deviceaddress address,stringvalue); intWritebit (deviceaddress address,BOOLvalue); intWritebits (deviceaddress address,bytevalue); EventDatachangeeventhandler DataChange; }

among them, Updaterate is the polling cycle. Deadband is the dead zone. The dead zone represents sensitivity, and the small sensitivity is high, but it also brings more noise.

Each group variable can support separate reads and writes (such as each readxxx,writexxx method), and also supports bulk push (DataChange event). The poll of the lower machine, all in groups, each group in the active state in accordance with their own polling cycle, collect, push data, non-interference.

each group contains a set of variables with similar characteristics: Having the same polling period, activating attributes (polling or no polling required), read and write properties (both read-only, read/write, or write-only), and can be enabled or masked at the same time as required.

Because some variables do not need to be monitored at any time, they can be drawn into a group, not refreshed (polling); Some variables change quickly, require high-frequency scanning; some changes are slow and do not need to be viewed at all times, and can be polled in a few minutes. The effective grouping of variables can improve the reading and writing efficiency of the key monitoring variables, and reduce the utilization of the resources of the subordinate machine.

If you have more than one client connected to each other, the data you need is not the same, and it is very efficient to send it to the client by a unified periodic polling by the gateway.

For example, open a supermarket, hurrying guests (client) demand varies, but the supermarket (gateway) to unified Procurement (polling), without customers with the wholesale market (lower machine) direct orders, focus on my shopping (subscribe to tag) on the line.

    • The future of expansion

Although the current mainstream plc does not all support subscription-push mode, but this is the historical trend. AB Controllogix, the new Siemens S71200-1500, supports label addresses, which are direct push-to-change tags (tag) data.

Future consider developing a new interface to support this model.

Four, Communication with host computer: subscribe-Push

    • features of the host computer-why subscribe-Push

The main features of PC communication are:

    1. To timely and accurate knowledge of the next computer message. Whether it is monitoring screen, or alarm, prompt manual operation of these, the more real-time the better, the more accurate the better. If the request-response mode is used, the cycle of the request determines that the real-time is not too good. Request frequent gateway pressure is large, reverse real-time poor.
    2. A gateway may have to drag several upper computer, more than one section, may have to open more than 10 display monitor simultaneously. Therefore, each host computer to the gateway to request data, traffic will rise steeply, the network will block, the gateway pressure will be very large.
    3. Most of the data required by the host computer does not change frequently, especially some switching volumes. If you repeatedly request, waste resources, waste time.
    4. If the use of individual variables to request data, it is bound to cause a large number of different time request-response process staggered, difficult to integrate, it is difficult to bulk read and write, very low efficiency.

Therefore, it is undoubtedly an efficient way to push only the changed data to subscribers. As long as the data changes, immediately pushed to the client, not only to ensure the real-time, but also to ensure the minimization of push data.

Like a repair call, send repair units (lower machine) is not always someone to the door; phone call over, the main station (gateway) down the number, someone door (the next computer has data changes) notice (push) you. No one you don't need a few seconds to hit a reminder (poll), you annoying everyone.

    • How to implement a subscription-push

Push is a variable that pushes only changes. To know which variables have changed, you must cache the data of the last variable to be compared. Therefore, you need to cache the data for each poll.

The gateway scans the subscription variables during the polling process for the next machine, and the data has a variable cache of memory:ICache:

          Interface   ICache: ireaderwriter  {  int  Size {get ; set  ;}  int  ByteCount {get  ;        The Array Cache { get  ;}  int   GetOffset ( deviceaddress  start, deviceaddress  end) ;}

First,ICache inherits the Ireaderwriter, which means that the cache class is also read-write to facilitate comparison. There is also a cache property, which is the memory area: the variable that maps and stores the lower machine.

Because the lower machine may have many, the storage address is also discontinuous, but by the order of the address deviceaddress of the lower machine, the final slave address maps to a contiguous memory address, through deviceaddress The CacheIndex (cache index) associated with the.

Each poll, called Ireaderwriter 's Readbytes method, is read into the local variable area, and the value read is compared to the cached data in the cache, and all the changed parts are added to a changedlist table. Storage of changes in CacheIndex.

These functions are implemented in the Poll function within the Plcgroup timer .       

   void  Timer_timer (object   sender,            EventArgs e) { if   (_isactive)                { lock   (sync)                    {  Poll   ();  if  (_changedlist.count > 0  Span style= "color: #000000;"                >) Update (); }}  else  return  
   
    ; }
   

After comparison, if the number of changedlist found is greater than 0, indicating that there is a variable value update, execute the Update method, according to CacheIndex find all variable variables, through the igroup Interface DataChange event packaging push out.

The specific subscription-push process is implemented using a socket (socket) in the DaService class. The socket, as its name implies, is a telephone line: Each client sends a request to the gateway server to establish a long connection: As long as the customer does not hang up the phone, it has been attached. The customer always listens to the telephone, as long as the server data changes, immediately has the operator promptly informs, the customer responds. Here I implemented a custom TLV (type-length-value) protocol, the change of data package sent, the client unpacking, decomposition of the variable ID, real-time value, time stamp and other information, and converted to the animation of the elements, and then elaborated in detail.

    • Communication flowchart of the upper and lower machine:

Five, the following plan

Write a series of posts to clarify the architecture and principles. Roughly as follows:

    • Gateway-Layer Interface Overview
    • Communication principle of the upper and lower machine
    • How to implement a device driver
    • How to design elements
    • VS plug-in modules and principles
    • Archive modules and file formats
    • How to extend a feature
    • Configuration Variable Expression implementation

GitHub Address: Https://github.com/GavinYellow/SharpSCADA. QQ Group: 102486275

Open source Pure C # Industrial Gateway + configuration software (four) communication principle of the upper and lower machine

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.