[Serialization] Design and Implementation of the C # communication (Serial Port and network) framework-8. Design of the overall controller,

Source: Internet
Author: User

[Serialization] Design and Implementation of the C # communication (Serial Port and network) framework-8. Design of the overall controller,

Contents

Chapter 8 design of the overall controller... 2

8.1 functions of the total controller... 2

8.2 assemble and release parts... 3

8.3 Event Response... 5

8.4 summary... 9

 

Chapter 8 Overall Controller Design

With the I/O, device driver, display, data export, and service components, you can build a controller that integrates various components on these existing interfaces, coordinates the orderly work, event response, and data flow control of various parts.

In addition, this controller is also responsible for interacting with the Host Program, which can be understood as follows: the master controller is the sole carrier for interaction between the host Program and the IO, device driver, display, data export, and service components. The structure is as follows:

 

8.1 functions of the total Controller

The functions of the IDeviceController include adding and deleting device drivers, adding and exporting data instances, adding graphical display instances, adding service component instances, clicking service events, and releasing controller resources. The interface definition is as follows:

 

8.2 assemble and release parts

DeviceController is the instance type of the total controller and inherits from the IDeviceController interface. The constructor is used to initialize the controller. 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();}

The ReleaseDeviceController interface is used to release resources of the total 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();}

Releasing resources when the software exits is much more complex than loading resources when the software starts. This involves two aspects: (1) Releasing resources in order. If resources are released in advance, therefore, the following code may fail to reference object Resources during execution, resulting in unexpected results. Therefore, you must judge the availability of the instance. (2) the transactional thread cannot exit normally, causing the software interface to be closed, but the background process has always existed. Especially for thread exit processing, the Framework platform adopts 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; // this thread is the background thread this. _ RunThread. name = "RunThread"; this. _ RunThread. start () ;}} private void RunThead () {while (! _ IsExit) {if (_ IsExit) // if it is identified as true, exit the loop and exit the thread {break;} // transaction processing} public void StopThead () {if (this. _ RunThread! = Null & this. _ RunThread. IsAlive) {this. _ IsExit = true; // identifies the current thread as an exited thread. This. _ RunThread. join (1000); // block the calling thread until a thread is terminated or after a specified time. try {_ RunThread. abort (); // force termination may cause file damage to prevent the thread from exiting} catch {}}}
8.3 Event Response

Device events are bound to and unbound from device drivers. The Code is as follows:

dev.DeviceRuningLogHandler += new DeviceRuningLogHandler(DeviceRuningLogHandler);dev.UpdateContainerHandler += new UpdateContainerHandler(UpdateContainerHandler);dev.DeviceObjectChangedHandler += new DeviceObjectChangedHandler(DeviceObjectChangedHandler);dev.ReceiveDataHandler += new ReceiveDataHandler(ReceiveDataHandler);dev.SendDataHandler += new SendDataHandler(SendDataHandler);dev.COMParameterExchangeHandler += new COMParameterExchangeHandler(COMParameterExchangeHandler);dev.DeleteDeviceHandler += new DeleteDeviceHandler(DeleteDeviceHandler);

For details, see "3rd Event Response design" in Chapter 3.12 device driver design. The response code for the COMParameterExchangeHandler event that changes the serial parameter is 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) {// -------------- process the old serial port ---------------- // IRunDevice [] oldCOMDevList = this. _ devList. getDevices (e. oldCOM. toString (), CommunicationType. COM); // --------------- check the current number of serial devices ------------ // int existCOMCount = 0; for (int I = 0; I <oldCOMDevList. length; I ++) {if (oldCOMDevList [I]. getHashCode ()! = Dev. getHashCode () {existCOMCount ++ ;}/// assets // if (existCOMCount <= 0) // No device is available for this serial port {IIOController oldCOMController = IOControllerManager. getInstance (). getController (SessionCom. formatKey (e. oldCOM); if (oldCOMController! = Null) {_ ioController. closeController (oldCOMController. key);} else {DeviceMonitorLog. writeLog (e. deviceName, "the serial port controller of the device is empty"); }}// -------------- process 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) {newCOMController. startService (); _ ioController. addController (newCOMController. key. toString (), newCOMController);} else {DeviceMonitorLog. writeLog (e. deviceName, "failed to create the 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. IOSettings (e. newBaud); if (success) {DeviceMonitorLog. writeLog (e. deviceName, String. format ("the baud rate of the serial port {0} has been changed from {1} to {2}", e. oldCOM. toString (), e. oldBaud. toString (), e. newBaud. toString ();} else {DeviceMonitorLog. writeLog (e. deviceName, String. format ("failed to change the baud rate of serial port {0} from {1} to {2}", e. oldCOM. toString (), e. oldBaud. toString (), e. newBaud. toString () ;}}}} else {DeviceMonitorLog. writeLog (e. deviceName, "not a serial device ");}}}

It also includes GraphicsShowClosedHandler and MouseRightContextMenuHandler. When the display view is disabled, the GraphicsShowClosedHandler event is triggered, the current view is removed from the manager, and resources are released. When you right-click the display view, the MouseRightContextMenuHandler event is triggered, to call the context menu of the device.

Conclusion 8.4

The overall controller is not required. The host program can directly interact with the IO, device driver, display, data export, and service components. However, in order to make the structure clear and easy to expand, an overall coordination layer is added in the middle.

 

Author: Wei Xiaozhi

Email: 504547114@qq.com

QQ: 504547114

. NET Development Technology Alliance: 54256083

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

Http://www.bmpj.net

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.