00001 using System; 00002 using System.Collections; 00003 using System.Collections.Generic; 00004 using System.Diagnostics; 00005 using System.Runtime.InteropServices; 00006 using System.Linq; 00007 using System.Text; 00008 using RS232Port; 00009 00010 namespace RS232Port { 00011 public class SerialStrategy { 00012 00013 public delegate void updateStatusEventHandler(); 00014 public event updateStatusEventHandler updateStatusEvent; 00015 public event updateStatusEventHandler setSilentModeEvent; 00016 public delegate void updateDataEventHandler(); 00017 public event updateDataEventHandler updateDataEvent; 00018 public delegate void updateUiEventHandler(); 00019 public event updateUiEventHandler updateUiEvent; 00020 public delegate void updateErrorEventHandler(Exception error); 00021 public event updateErrorEventHandler updateErrorEvent; 00022 protected delegate void openSerialPortEventHandler(); 00023 protected event openSerialPortEventHandler openSerialPortEvent; 00024 00025 protected BitArray _dataAtom; 00026 protected double _outDouble = 0; 00027 00028 protected string serialPortName; 00029 protected RS232MainForm _rs232; 00030 00031 protected string _status; 00032 public string Status { 00033 get { return _status; } 00034 } 00035 00036 protected string _error; 00037 public string Error { 00038 get { return _error; } 00039 } 00040 00041 protected Stopwatch _watchdogTimer; 00042 public Stopwatch WatchdogTimer { 00043 get { return _watchdogTimer; } 00044 } 00045 00046 public SerialStrategy() { 00047 _watchdogTimer = new Stopwatch(); 00048 } 00049 00050 protected void updateData() { 00051 if (updateDataEvent != null) 00052 updateDataEvent.Invoke(); 00053 } 00054 00055 protected void updateStatus() { 00056 if (updateStatusEvent != null) 00057 updateStatusEvent.Invoke(); 00058 } 00059 00060 protected void updateSilentMode() { 00061 _silent = _rs232.Silent; 00062 if (setSilentModeEvent != null) 00063 setSilentModeEvent.Invoke(); 00064 } 00065 protected void updateUi() { 00066 if (updateUiEvent != null) 00067 updateUiEvent.Invoke(); 00068 } 00069 00070 //called by the rs232 port if there is an error 00071 //chain of responsibility 00072 protected void updateError(Exception e) { 00073 if (updateErrorEvent != null) 00074 updateErrorEvent.Invoke(e); 00075 } 00076 00077 public string openPort() { 00078 return _rs232.initializeComPort(); 00079 } 00080 00081 public string closePort() { 00082 return _rs232.closePort(); 00083 } 00084 00085 public void setSilentMode(bool silent) { 00086 _rs232.Silent = silent; 00087 } 00088 00089 protected bool _silent = true; 00090 public bool Silent { 00091 get { return _silent; } 00092 } 00093 00094 public void showSerialPort() { 00095 if (!_rs232.IsDisposed) 00096 _rs232.Show(); 00097 else { 00098 if (openSerialPortEvent != null) 00099 openSerialPortEvent.Invoke(); 00100 _rs232.Show(); 00101 } 00102 } 00103 00104 } 00105 }