[Serial] "C # communication (Serial and network) framework design and implementation"-8. Overall controller design

Source: Internet
Author: User
Tags response code

Directory

The eighth chapter design of the overall controller ... 2

8.1 Function of the master controller ... 2

8.2 Assembling and releasing parts ... 3

8.3 Event Response ... 5

8.4 Summary ... 9

The eighth chapter design of the overall controller

With the IO section, the device driver section, the display section, the Data Export section, and the service component part, we build a total controller of the integrated parts on these existing interfaces to coordinate the orderly work of each part, the incident response and control the flow of data.

In addition, this master controller is also responsible for interacting with the host program, which can be understood as follows: The master controller is the host program and the IO part, the device driver part, the display section, the Data Export section and the service component part of the interaction between the carrier, and is unique. The structure is as follows:

8.1 Functions of the master controller

The functions of the master Controller (Idevicecontroller) include adding and removing device drivers, increasing export data instances, adding graphical display instances, increasing service component instances, clicking on service events, freeing controller resources, and so on. Interface definitions such as:

8.2 Assembling and releasing parts

Devicecontroller is the instance body class of the master controller and inherits from the Idevicecontroller interface. The initialization of the master controller is done through the constructor, and the code is as follows:

Public Devicecontroller () {       _devlist = devicemanager.getinstance ();       _iocontroller = Iocontrollermanager.getinstance ();       _runcontainer = Runcontainerform.getruncontainer ();       _runcontainer.mouserightcontextmenuhandler + = Runcontainer_mouserightcontextmenuhandler;       _datashowcontroller = new Graphicsshowcontroller ();       _exportcontroller = new Exportdatacontroller ();       _appservicemanager = new Appservicemanager ();}

Through the Releasedevicecontroller interface to complete the resource release of the master controller, the code is as follows:

public void Releasedevicecontroller () {       _iocontroller.removeallcontroller ();       _runcontainer.removealldevice ();       _runcontainer.mouserightcontextmenuhandler-= Runcontainer_mouserightcontextmenuhandler;       _exportcontroller.removeall ();       _datashowcontroller.removeall ();       _appservicemanager.removeall ();       ienumerator<irundevice> devlist = _devlist.getenumerator ();       while (Devlist.movenext ())       {              devList.Current.ExitDevice ();       }       _devlist.removealldevice ();}

Software release when releasing resources more complex than the load of resources when the software starts, this block involves two aspects: (1) Release the resource order, if the resources are released in advance, it will often result in the following code in the execution process of the phenomenon of unable to reference object resources, resulting in unexpected results, So be sure to judge the availability of the instance. (2) A transactional thread fails to exit gracefully, causing the software interface to be closed, but the background process persists. In particular, the processing of thread exits, the framework platform uses a unified thread exit mechanism, the code is as follows:

public void Startthead () {if (_runthread = = NULL | |!_runthread.isalive)              {this._isexit = false;              This._runthread = new Thread (new ThreadStart (Runthead)); This._runthread.isbackground = true;              The thread is a background thread this._runthread.name = "Runthread";       This._runthread.start ();              }}private void Runthead () {while (!_isexit) {if (_isexit)//If ID is true, exits the loop, exits the thread              {break; }//Transaction}}public void Stopthead () {if (This._runthread! = null && This._runthread.isaliv       E) {this._isexit = true;              Identifies the current thread as a fallback. This._runthread.join (1000);//block the calling thread until a thread terminates or passes the specified time, try {_runthread.abor    T (); In order to prevent the thread from quitting, it may cause file corruption} catch {}}} 
8.3 Event Response

Adding and removing device drivers binds and reconciles the events of the device. The code is as follows:

Dev. Deviceruningloghandler + = new Deviceruningloghandler (deviceruningloghandler);d ev. Updatecontainerhandler + = new Updatecontainerhandler (updatecontainerhandler);d ev. Deviceobjectchangedhandler + = new Deviceobjectchangedhandler (deviceobjectchangedhandler);d ev. Receivedatahandler + = new Receivedatahandler (receivedatahandler);d ev. Senddatahandler + = new Senddatahandler (senddatahandler);d ev. Comparameterexchangehandler + = new Comparameterexchangehandler (comparameterexchangehandler);d ev. Deletedevicehandler + = new Deletedevicehandler (Deletedevicehandler);

For specific meanings, see "3.12 Incident Response Design" in the 3rd device-driven design, Comparameterexchangehandler changing the serial port parameter event response code as follows:

private void Comparameterexchangehandler (object source, Comparameterexchangeargs e) {if (E = = null) {       Return       } Irundevice dev = This._devlist.getdevice (e.deviceid.tostring ()); if (dev! = null) {if (dev.                     Communicationtype = = communicationtype.com) {if (e.oldcom! = e.newcom) {//--------------processing the old serial port----------------//irundevice[] Oldcomd                            Evlist = This._devlist.getdevices (e.oldcom.tostring (), communicationtype.com);                            ---------------detect the current number of serial devices------------//int existcomcount = 0; for (int i = 0; i < oldcomdevlist.length; i++) {if (oldco Mdevlist[i]. GetHashCode ()! = Dev.   GetHashCode ()) {existcomcount++;                                }                            }                            //------------------------------                                   ------------//if (existcomcount <= 0)//The serial port is not available for the device { Iiocontroller Oldcomcontroller = Iocontrollermanager.getinstance ().                                   Getcontroller (Sessioncom.formatkey (e.oldcom)); if (Oldcomcontroller! = null) {_IOCONTROLLER.C                                   Losecontroller (Oldcomcontroller.key); } else {Devi                                   Cemonitorlog.writelog (E.devicename, "The serial controller of the device is empty");                            }}//--------------processing the new serial port----------------// BOOL Newcomcontrollerexist = Iocontrollermanager.getinstAnce ().                            Containcontroller (Sessioncom.formatkey (e.newcom)); if (!newcomcontrollerexist) {Iiocontroller Newcomcontroller                                   = _iocontroller.buildcontroller (E.newcom.tostring (), e.newbaud.tostring (), communicationtype.com); if (Newcomcontroller! = null) {NEW                                        Comcontroller.startservice ();                                   _iocontroller.addcontroller (NewCOMController.Key.ToString (), Newcomcontroller); } else {Devi                                   Cemonitorlog.writelog (E.devicename, "failed to create serial controller for this device"); }} devicemonitorlog.writelog (E.devicename, String.Format ("serial port changed from {0}} to { 1} ", E.oldcom.tostring (), e.newcom.ToString ()));                            } else {if (E.oldbaud! = E.newbaud) {isessioncom Comio = (isessioncom) sessioncommanager.getinstance ().                                   Getio (Sessioncom.formatkey (e.oldcom)); if (Comio! = NULL) {BOOL success = Comio.ioset                                          Tings (E.newbaud); if (success) {DEVICEMONITORLOG.W Ritelog (E.devicename, String.Format ("serial port {0} baud rate changed from {1} to {2} success", E.oldcom.tostring (), e.oldbaud.tostring (), E.                                          Newbaud.tostring ()));                                                 } else { Devicemonitorlog.writelog (E.devicename, String.formAt ("Serial port {0} baud rate changed from {1} to {2} failed", E.oldcom.tostring (), e.oldbaud.tostring (), e.newbaud.tostring ()));              }                                   }                            }                     }              } else {Devicemonitorlog.writelog (e.devicename, "Not a serial port type device"); }       }}

It also includes two events for Graphicsshowclosedhandler and Mouserightcontextmenuhandler. When the display view is turned off, the Graphicsshowclosedhandler event is triggered, the current view is removed from the manager, and the resource is released, and when you right-click the display view, the Mouserightcontextmenuhandler event is triggered. To invoke the appropriate device's context menu.

8.4 Summary

The overall controller is not required, and the host program can interact directly with the IO section, the device driver section, the display section, the Data Export section, and the service component part. However, in order to make the structure clear and easy to expand, a layer is added in the middle for overall coordination.

Only laugh at

Email:[email protected]

qq:504547114

. NET Development Technology Alliance: 54256083

Document Download: Http://pan.baidu.com/s/1pJ7lZWf

Official website: http://www.bmpj.net

[Serial] "C # communication (Serial and network) framework design and implementation"-8. Overall controller design

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.