00001 using System; 00002 using System.Collections.Generic; 00003 using System.Linq; 00004 using System.Text; 00005 using RS232Port; 00006 00007 namespace GpsModule { 00008 public class GpsStrategy : SerialStrategy { 00009 00010 RmcStrategy _rmc; 00011 public RmcStrategy Rmc { 00012 get { return _rmc; } 00013 } 00014 00015 GgaStrategy _gga; 00016 public GgaStrategy Gga { 00017 get { return _gga; } 00018 } 00019 00020 GpsData _data; 00021 public GpsData Data { 00022 get { return _data; } 00023 } 00024 00025 public GpsStrategy(string portName) { 00026 serialPortName = portName; 00027 _data = new GpsData(); 00028 _rmc = new RmcStrategy(); 00029 _rmc.updateErrorEvent += updateError; 00030 _gga = new GgaStrategy(); 00031 _gga.updateErrorEvent += updateError; 00032 openSerialPortEvent += initializeGpsModulePort; 00033 initializeGpsModulePort(); 00034 } 00035 00036 private void initializeGpsModulePort() { 00037 _rs232 = new RS232MainForm(); 00038 _rs232.updateErrorEvent += updateError; 00039 _rs232.Port.PortName = serialPortName; 00040 _rs232.Show(); 00041 _rs232.Hide(); 00042 _rs232.Port.BaudRate = 38400; 00043 _rs232.receiveDataEvent += receiveData; 00044 _rs232.setSilentModeEvent += updateSilentMode; 00045 _rs232.updatePortSettings(); 00046 _rs232.Text = "GPS Module Serial Port"; 00047 _rs232.DebugMode = RS232MainForm.debugTerminalMode.silent; 00048 _rs232.Port.ReceivedBytesThreshold = 600; 00049 _status = _rs232.initializeComPort(); 00050 00051 } 00052 00053 private void receiveData() { 00054 _data.setBuffer(_rs232.SerialData); 00055 string s = _data.Buffer; 00056 try { 00057 while (s.Contains("$GP") && s.Contains("\r\n") 00058 && s.IndexOf("$GP") < s.LastIndexOf("\r\n")) { 00059 string sentence = s.Substring(s.IndexOf("$GP") + 3 00060 , s.IndexOf("\r\n", s.IndexOf("$GP")) - (s.IndexOf("$GP") + 3)); 00061 s = s.Remove(0, s.IndexOf("\r\n") + 2); 00062 //computeChecksum(sentence); 00063 if (sentence.Contains("GGA")) 00064 _gga.parseSentance(sentence); 00065 else if (sentence.Contains("RMC") && _gga.Data.QualityIndicator > 0) 00066 _rmc.parseSentance(sentence); 00067 } 00068 } catch (Exception e) { 00069 updateError(e); 00070 _data.flushBuffer(); 00071 } 00072 updateData(); 00073 updateUi(); 00074 } 00075 00076 private int computeChecksum(string data) { 00077 //int checksum = 0; 00078 byte testByte; 00079 foreach (char c in data) { 00080 testByte = (byte)c; 00081 } 00082 //checksum += ((int)(byte)c); 00083 00084 return 0; 00085 } 00086 } 00087 }