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