一、须知
1.手持机(PDA)必须有GPS模块,才能通过代码使用串口通信获取GPS相关信息
2.要清楚自己手持机(PDA)固定的GPS通信串口号,如我们公司的手持机获取GPS信息的串口为COM4
3.测试手持机(PDA)是否获取到GPS相关信息,要到室外才能测试出来。
二、制作步骤
(1)新建好win ce项目
(2)封装类
1.CLR_GPS类
class CLR_GPS
{ public float latitude = ; //纬度 public float longitude = ; //经度 public float speed = ; public float status = ; public float direction = ; public float geoideAltitude = ; private System.IO.Ports.SerialPort serialPort; CLR_Regex gpsRegex=new CLR_Regex(); private bool isOpen = false; #region
private delegate void myTestEventHandler();
public delegate void LocationEventHandler(string Latitude, string Longitude,string status,bool isNorth,bool isEast,string time);
public delegate void serialEventHandler(string data);
public delegate void TimeEventHandler(string time);
public delegate void DateEventHandler(string date);
public delegate void satelliteEventHandlde(string n);
public delegate void altitudeEventHandler(string altitude);
public delegate void satelliteNumberEventHandler(string num); private string serialData = string.Empty; /// <summary>
/// refresh serial data 更新串口数据
/// </summary>
public event serialEventHandler serialportData; /// <summary>
/// Refresh Location 更新经纬度
/// </summary>
public event LocationEventHandler refreshLocation; /// <summary>
/// Refresh Time 更新时间
/// </summary>
public event TimeEventHandler refreshTime; /// <summary>
/// Refresh Date 更新日期
/// </summary>
public event DateEventHandler refreshDate; /// <summary>
/// Satellite Total 可视卫星数量
/// </summary>
public event satelliteNumberEventHandler refreshAllNumber; /// <summary>
/// Refresh effective satellite 更新卫星数量
/// </summary>
public event satelliteEventHandlde refreshSatellite; /// <summary>
/// Refresh Altitude 更新海拔
/// </summary>
public event altitudeEventHandler refreshAltitude; #endregion public CLR_GPS()
{
serialPort = new System.IO.Ports.SerialPort("COM4", ); //此处可根据实际情况设置串口
serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort_DataReceived); } public CLR_GPS(string portName)
{
serialPort = new System.IO.Ports.SerialPort(portName, ); //此处可根据实际情况设置串口
serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort_DataReceived); } /// <summary>
/// stop Receive data 停止接收数据
/// </summary>
public void stopReceive()
{
isOpen = false;
Thread.Sleep();
} /// <summary>
/// start Receiving 开始接收数据
/// </summary>
public bool startReceive()
{
try
{
isOpen = true;
if (!serialPort.IsOpen)
{
serialPort.Open();
}
return true;
}
catch (System.Exception)
{
isOpen = false;
return false;
} } /// <summary>
/// initialization 初始化
/// </summary>
/// <returns></returns>
public bool InitModule()
{
return true; //此处可根据实际情况进行GPS模块的上电 } public void FreeModule()
{
try
{ isOpen = false; if (serialPort.IsOpen)
{
serialPort.Close();
} }
catch (Exception)
{ }
//此处可根据实际情况进行GPS模块的下电 } void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
serialData = serialData + serialPort.ReadExisting();
if (serialData.Length > )
{
serialData = serialData.Substring(serialData.LastIndexOf('$'));
} if (isOpen)
{
if (serialportData != null)
{
serialportData(serialData);
}
AnalysisGPSData(serialData); }
}
catch { } } private void AnalysisGPGGA(string scr)
{
string altitude = string.Empty;
string[] fields = scr.Split(','); if (fields.Length>)
{
string num = fields[].Trim();
if (num.Length > && gpsRegex.isNumber(num))
{
//satelliteNumber = Convert.ToInt32(fields[7].Trim());
if (refreshSatellite != null)
{
//Effective Satellite 更新使用卫星数量
refreshSatellite(num);
}
} if (fields[].Trim().Length > )
{
//refresh Altitude 更新海拔
if (refreshAltitude != null)
{
refreshAltitude(fields[].Trim());
}
}
} } private bool AnalysisGPRMC(string scr)
{
int latiDegree = , longiDegree = ;
float latiMinute = , longiMinute = , speedTemp = , directionTemp = direction;
string statusTemp = string.Empty;
float latitudeTemp = , longitudeTemp = ;
bool paramChanged = false; bool isNortHemisphere = true;
bool isEastHemisphere=true;
string time = string.Empty; string[] fields = scr.Split(','); if (fields.GetLength() < )
return false;
// 状态 string[] utc = fields[].Trim().Split('.'); if (utc[].Trim().Length == )
{
time = utc[].Trim();
if (refreshTime!=null)
{
refreshTime(time);
}
} if (fields[]=="A")
{
// //= Convert.ToChar(fields[2]);
//char d = Convert.ToChar(fields[2]);
statusTemp = fields[];
}
// Lati
if (fields[].Length > )
{
if (System.Text.RegularExpressions.Regex.Match(fields[], @"^[0-9]+[.]?([\d]+)?").Value == fields[])
{
latiDegree = Convert.ToInt32(fields[].Substring(, ), );
latiMinute = Convert.ToSingle(fields[].Substring(, fields[].Length - ));
paramChanged = true;
}
}
// NSHemisphere NSHemisphere 南北半球
if (fields[].Length == )
{
//'s' 'S'
if (string.Equals(fields[], "S", StringComparison.CurrentCultureIgnoreCase))
{
isNortHemisphere = false; //南半球
latitudeTemp = -latitudeTemp;
}
}
// long
if (fields[].Length > )
{
if (System.Text.RegularExpressions.Regex.Match(fields[], @"^[0-9]+[.]?([\d]+)?").Value == fields[])
{
longiDegree = Convert.ToInt32(fields[].Substring(, ), );
longiMinute = Convert.ToSingle(fields[].Substring(, fields[].Length - ));
paramChanged = true;
}
}
// EWHemisphere 东经 西经
if (fields[].Length == )
{
//'w' 'W'
if (string.Equals(fields[], "W", StringComparison.CurrentCultureIgnoreCase))
{
isEastHemisphere=false; //西半球
longitudeTemp = -longitudeTemp;
}
}
// speed 速率
if (fields[].Length > )
{
if (System.Text.RegularExpressions.Regex.Match(fields[], @"^[0-9]+[.]?([\d]+)?").Value == fields[])
{
speedTemp = (float)(Convert.ToSingle(fields[]) * 1.852);
paramChanged = true;
}
}
// direction 地面航向 正北为零度
if (fields[].Length > )
{
if (System.Text.RegularExpressions.Regex.Match(fields[], @"^[0-9]+[.]?([\d]+)?").Value == fields[])
{
directionTemp = Convert.ToSingle(fields[]);
paramChanged = true;
}
} if (fields[].Trim().Length==)
{
if (refreshDate!=null)
{
refreshDate(trandforDate(fields[].Trim()));
}
}
if ((latiDegree != ) && (latiMinute != (float)400.0))
{
latitudeTemp = (float)(latiDegree + latiMinute / 60.0);
}
if ((longiDegree != ) && (longiMinute != (float)400.0))
{
longitudeTemp = (float)(longiDegree + longiMinute / 60.0);
}
if (paramChanged)
{
if (refreshLocation != null)
{
refreshLocation(fields[]+":"+latitudeTemp.ToString(), fields[]+":"+longitudeTemp.ToString(), statusTemp,isNortHemisphere,isEastHemisphere, time);
}
return true;
}
else
return false;
} /// <summary>
/// data for date 将GPS数据转换成日期
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
string trandforDate(string date)
{
return ""+date.Substring(, ) + "-" + date.Substring(, ) + "-" + date.Substring(, );
} void AnalysisGPGSV(string scr)
{
string[] fields = scr.Split(','); if (fields.Length>)
{
string AllNum = fields[].Trim(); if (AllNum.Length > && gpsRegex.isNumber(AllNum) && refreshAllNumber != null)
{
//Satellite Total
refreshAllNumber(fields[]);
}
} // 状态
} private void AnalysisGPSData(string sGpsData)
{
string[] gpsDataSplit = sGpsData.Trim().Split('$'); foreach (string gps in gpsDataSplit)
{
if (gps.StartsWith("GPRMC"))
{
AnalysisGPRMC(gps);
}
else if (gps.StartsWith("GPVTG"))
{
AnalysisGPVTG(gps);
}
else if (gps.StartsWith("GPGGA"))
{
AnalysisGPGGA(gps);
}
else if (gps.StartsWith("GPGSV"))
{
AnalysisGPGSV(gps);
}
else if (gps.StartsWith("GPGLL"))
{ }
}
return;
} public void AnalysisGPVTG(string sGpsData)
{ } }
2.CLR_Regex类
class CLR_Regex
{
//public bool includeXing(string msg)
//{
// if (Regex.Match(msg, @"^[0-9]+[*]?([\d]+)?").Value == msg)
// {
// return true;
// }
// else
// return false;
//} /// <summary>
/// is number ? 是否全是数字
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public bool isNumber(string msg)
{ if (Regex.Match(msg, "^[0-9]*$").Value == msg)
{
return true;
}
else
return false;
} /// <summary>
/// is real 是否为实数
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public bool isRealData(string msg)
{
if (msg.StartsWith("-"))
{
msg = msg.Substring();
}
if (Regex.Match(msg, "^[0-9]+(.[0-9]+)$").Value == msg)
{
return true;
}
else
return false; } }
3.GPS类
class GPS
{
public string PortNum;
public int BaudRate;
public byte ByteSize;
public byte Parity; // 0-4=no,odd,even,mark,space
public byte StopBits; // 0,1,2 = 1, 1.5, 2
public int ReadTimeout; //comm port win32 file handle
private int hComm = -; public bool Opened = false; //win32 api constants
private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const int OPEN_EXISTING = ;
private const int INVALID_HANDLE_VALUE = -; [StructLayout(LayoutKind.Sequential)]
public struct DCB
{
//taken from c struct in platform sdk
public int DCBlength; // sizeof(DCB)
public int BaudRate; // 指定当前波特率 current baud rate
// these are the c struct bit fields, bit twiddle flag to set
public int fBinary; // 指定是否允许二进制模式,在windows95中必须主TRUE binary mode, no EOF check
public int fParity; // 指定是否允许奇偶校验 enable parity checking
public int fOutxCtsFlow; // 指定CTS是否用于检测发送控制,当为TRUE是CTS为OFF,发送将被挂起。 CTS output flow control
public int fOutxDsrFlow; // 指定CTS是否用于检测发送控制 DSR output flow control
public int fDtrControl; // DTR_CONTROL_DISABLE值将DTR置为OFF, DTR_CONTROL_ENABLE值将DTR置为ON, DTR_CONTROL_HANDSHAKE允许DTR"握手" DTR flow control type
public int fDsrSensitivity; // 当该值为TRUE时DSR为OFF时接收的字节被忽略 DSR sensitivity
public int fTXContinueOnXoff; // 指定当接收缓冲区已满,并且驱动程序已经发送出XoffChar字符时发送是否停止。TRUE时,在接收缓冲区接收到缓冲区已满的字节XoffLim且驱动程序已经发送出XoffChar字符中止接收字节之后,发送继续进行。FALSE时,在接收缓冲区接收到代表缓冲区已空的字节XonChar且驱动程序已经发送出恢复发送的XonChar之后,发送继续进行。XOFF continues Tx
public int fOutX; // TRUE时,接收到XoffChar之后便停止发送接收到XonChar之后将重新开始 XON/XOFF out flow control
public int fInX; // TRUE时,接收缓冲区接收到代表缓冲区满的XoffLim之后,XoffChar发送出去接收缓冲区接收到代表缓冲区空的XonLim之后,XonChar发送出去 XON/XOFF in flow control
public int fErrorChar; // 该值为TRUE且fParity为TRUE时,用ErrorChar 成员指定的字符代替奇偶校验错误的接收字符 enable error replacement
public int fNull; // eTRUE时,接收时去掉空(0值)字节 enable null stripping
public int fRtsControl; // RTS flow control
/*RTS_CONTROL_DISABLE时,RTS置为OFF
RTS_CONTROL_ENABLE时, RTS置为ON
RTS_CONTROL_HANDSHAKE时,
当接收缓冲区小于半满时RTS为ON
当接收缓冲区超过四分之三满时RTS为OFF
RTS_CONTROL_TOGGLE时,
当接收缓冲区仍有剩余字节时RTS为ON ,否则缺省为OFF*/
public int fAbortOnError; // TRUE时,有错误发生时中止读和写操作 abort on error
public int fDummy2; // 未使用 reserved public uint flags;
public ushort wReserved; // 未使用,必须为0 not currently used
public ushort XonLim; // 指定在XON字符发送这前接收缓冲区中可允许的最小字节数 transmit XON threshold
public ushort XoffLim; // 指定在XOFF字符发送这前接收缓冲区中可允许的最小字节数 transmit XOFF threshold
public byte ByteSize; // 指定端口当前使用的数据位 number of bits/byte, 4-8
public byte Parity; // 指定端口当前使用的奇偶校验方法,可能为:EVENPARITY,MARKPARITY,NOPARITY,ODDPARITY 0-4=no,odd,even,mark,space
public byte StopBits; // 指定端口当前使用的停止位数,可能为:ONESTOPBIT,ONE5STOPBITS,TWOSTOPBITS 0,1,2 = 1, 1.5, 2
public char XonChar; // 指定用于发送和接收字符XON的值 Tx and Rx XON character
public char XoffChar; // 指定用于发送和接收字符XOFF值 Tx and Rx XOFF character
public char ErrorChar; // 本字符用来代替接收到的奇偶校验发生错误时的值 error replacement character
public char EofChar; // 当没有使用二进制模式时,本字符可用来指示数据的结束 end of input character
public char EvtChar; // 当接收到此字符时,会产生一个事件 received event character
public ushort wReserved1; // 未使用 reserved; do not use
}
[StructLayout(LayoutKind.Sequential)]
private struct COMMTIMEOUTS
{
public int ReadIntervalTimeout;
public int ReadTotalTimeoutMultiplier;
public int ReadTotalTimeoutConstant;
public int WriteTotalTimeoutMultiplier;
public int WriteTotalTimeoutConstant;
}
[StructLayout(LayoutKind.Sequential)]
private struct OVERLAPPED
{
public int Internal;
public int InternalHigh;
public int Offset;
public int OffsetHigh;
public int hEvent;
} [DllImport("coredll.dll")]
private static extern int CreateFile(
string lpFileName, // 要打开的串口名称
uint dwDesiredAccess, // 指定串口的访问方式,一般设置为可读可写方式
int dwShareMode, // 指定串口的共享模式,串口不能共享,所以设置为0
int lpSecurityAttributes, // 设置串口的安全属性,WIN9X下不支持,应设为NULL
int dwCreationDisposition, // 对于串口通信,创建方式只能为OPEN_EXISTING
int dwFlagsAndAttributes, // 指定串口属性与标志,设置为FILE_FLAG_OVERLAPPED(重叠I/O操作),指定串口以异步方式通信
int hTemplateFile // 对于串口通信必须设置为NULL
);
[DllImport("coredll.dll")]
private static extern bool GetCommState(
int hFile, //通信设备句柄
ref DCB lpDCB // 设备控制块DCB
);
[DllImport("coredll.dll")]
private static extern bool BuildCommDCB(
string lpDef, // 设备控制字符串
ref DCB lpDCB // 设备控制块
);
[DllImport("coredll.dll")]
private static extern bool SetCommState(
int hFile, // 通信设备句柄
ref DCB lpDCB // 设备控制块
);
[DllImport("coredll.dll")]
private static extern bool GetCommTimeouts(
int hFile, // 通信设备句柄 handle to comm device
ref COMMTIMEOUTS lpCommTimeouts // 超时时间 time-out values
);
[DllImport("coredll.dll")]
private static extern bool SetCommTimeouts(
int hFile, // 通信设备句柄 handle to comm device
ref COMMTIMEOUTS lpCommTimeouts // 超时时间 time-out values
);
[DllImport("coredll.dll")]
private static extern bool ReadFile(
int hFile, // 通信设备句柄 handle to file
byte[] lpBuffer, // 数据缓冲区 data buffer
int nNumberOfBytesToRead, // 多少字节等待读取 number of bytes to read
ref int lpNumberOfBytesRead, // 读取多少字节 number of bytes read
ref OVERLAPPED lpOverlapped // 溢出缓冲区 overlapped buffer
);
[DllImport("coredll.dll")]
private static extern bool WriteFile(
int hFile, // 通信设备句柄 handle to file
byte[] lpBuffer, // 数据缓冲区 data buffer
int nNumberOfBytesToWrite, // 多少字节等待写入 number of bytes to write
ref int lpNumberOfBytesWritten, // 已经写入多少字节 number of bytes written
ref OVERLAPPED lpOverlapped // 溢出缓冲区 overlapped buffer
);
[DllImport("coredll.dll")]
private static extern bool CloseHandle(
int hObject // handle to object
);
[DllImport("coredll.dll")]
private static extern uint GetLastError(); public void Open()
{ DCB dcbCommPort = new DCB();
COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS(); // 打开串口 OPEN THE COMM PORT.
hComm = CreateFile(PortNum, GENERIC_READ | GENERIC_WRITE, , , OPEN_EXISTING, , );
// 如果串口没有打开,就打开 IF THE PORT CANNOT BE OPENED, BAIL OUT.
if (hComm == INVALID_HANDLE_VALUE)
{
throw (new ApplicationException("非法操作,不能打开串口!"));
} // 设置通信超时时间 SET THE COMM TIMEOUTS.
GetCommTimeouts(hComm, ref ctoCommPort);
ctoCommPort.ReadTotalTimeoutConstant = ReadTimeout;
ctoCommPort.ReadTotalTimeoutMultiplier = ;
ctoCommPort.WriteTotalTimeoutMultiplier = ;
ctoCommPort.WriteTotalTimeoutConstant = ;
SetCommTimeouts(hComm, ref ctoCommPort); // 设置串口 SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS.
GetCommState(hComm, ref dcbCommPort);
dcbCommPort.BaudRate = BaudRate;
dcbCommPort.flags = ;
//dcb.fBinary=1;
dcbCommPort.flags |= ;
if (Parity > )
{
//dcb.fParity=1
dcbCommPort.flags |= ;
}
dcbCommPort.Parity = Parity;
dcbCommPort.ByteSize = ByteSize;
dcbCommPort.StopBits = StopBits;
if (!SetCommState(hComm, ref dcbCommPort))
{
//uint ErrorNum=GetLastError();
throw (new ApplicationException("非法操作,不能打开串口!"));
}
//unremark to see if setting took correctly
//DCB dcbCommPort2 = new DCB();
//GetCommState(hComm, ref dcbCommPort2);
Opened = true;
} public void Close()
{
if (hComm != INVALID_HANDLE_VALUE)
{
CloseHandle(hComm);
}
} public byte[] Read(int NumBytes)
{
byte[] BufBytes;
byte[] OutBytes;
BufBytes = new byte[NumBytes];
if (hComm != INVALID_HANDLE_VALUE)
{
OVERLAPPED ovlCommPort = new OVERLAPPED();
int BytesRead = ;
ReadFile(hComm, BufBytes, NumBytes, ref BytesRead, ref ovlCommPort);
try
{
OutBytes = new byte[BytesRead];
Array.Copy(BufBytes, , OutBytes, , BytesRead);
}
catch
{
return BufBytes;
} }
else
{
throw (new ApplicationException("串口未打开!"));
}
return OutBytes;
// return BufBytes;
} public void Write(byte[] WriteBytes)
{
if (hComm != INVALID_HANDLE_VALUE)
{
OVERLAPPED ovlCommPort = new OVERLAPPED();
int BytesWritten = ;
WriteFile(hComm, WriteBytes, WriteBytes.Length, ref BytesWritten, ref ovlCommPort);
}
else
{
throw (new ApplicationException("串口未打开!"));
}
}
public string GetGPS(string strGPS, string strFind)
{
///从GPS中读取的数据中,找出想要的数据
///GPSstring原始字符串,
///strFind要查找的内容,X:经度,Y:纬度,T:时间,V:速度,是数字从1开始,即以“,”分隔的位置
///返回查找到指定位置的字符串
string handerStr = "$GPRMC";//GPS串头
int findHander = strGPS.IndexOf(handerStr);//看是否含有GPS串头
if (findHander < )
{
return "-1";
}
else
{
strGPS = strGPS.Substring(findHander, strGPS.Length - findHander);
string[] ArryTmp = strGPS.Split(",".ToCharArray());
try
{
if (ArryTmp[] == "V")
{
return "V";//没有信号
}
else
{
switch (strFind)
{
case "X":
return DM2DD(ArryTmp[]); case "Y":
return DM2DD(ArryTmp[]); case "T":
return T2Time(ArryTmp[], ArryTmp[]); case "V":
return Convert.ToString(Convert.ToDouble(ArryTmp[]) * 1.852); default:
return "V"; }
}
}
catch
{
return "V";
}
}
}
public string T2Time(string strDate, string strTime)
{
string dT = "" + strDate.Substring(, ) + "-" + strDate.Substring(, ) + "-" + strDate.Substring(, );
string TT = Convert.ToString(Convert.ToInt32(strTime.Substring(, ))) + ":" + strTime.Substring(, ) + ":" + strTime.Substring(, );
DateTime T = Convert.ToDateTime(dT + " " + TT);
T = T.AddHours();
return T.ToString();
}
public string DM2DD(string DegreeMinutes)
{
//转换NMEA协议的“度分”格式为十进制“度度”格式
string sDegree;
string sMinute;
string sReturn = "";
if (DegreeMinutes.IndexOf(".") == )
{
//DegreeMinutes = Replace(DegreeMinutes, ".", "")
//DM2DD = CDbl(Left(DegreeMinutes, 2)) + CDbl(Left(CStr(CDbl(Right(DegreeMinutes, Len(DegreeMinutes) - 2)) / 60), 8)) / 10000
DegreeMinutes = DegreeMinutes.Replace(".", "");
double sDegree1 = Convert.ToDouble(DegreeMinutes.Substring(, ));
double sDegree2 = Convert.ToDouble(DegreeMinutes.Substring(, DegreeMinutes.Length - ));
string sTmp = Convert.ToString(sDegree2 / );
sDegree2 = Convert.ToDouble(sTmp.Substring(, sTmp.Length));
sDegree2 = sDegree2 / ;
sDegree = Convert.ToString(sDegree1 + sDegree2);
if (sDegree.Length > )
sDegree = sDegree.Substring(, );
sReturn = sDegree;
}
else if (DegreeMinutes.IndexOf(".") == )
{
//DegreeMinutes = Replace(DegreeMinutes, ".", "")
//DM2DD = CDbl(Left(DegreeMinutes, 2)) + CDbl(Left(CStr(CDbl(Right(DegreeMinutes, Len(DegreeMinutes) - 2)) / 60), 8)) / 10000
DegreeMinutes = DegreeMinutes.Replace(".", "");
double sMinute1 = Convert.ToDouble(DegreeMinutes.Substring(, ));
double sMinute2 = Convert.ToDouble(DegreeMinutes.Substring(, DegreeMinutes.Length - ));
string sTmp = Convert.ToString(sMinute2 / );
sMinute2 = Convert.ToDouble(sTmp.Substring(, sTmp.Length));
sMinute2 = sMinute2 / ;
sMinute = Convert.ToString(sMinute1 + sMinute2);
if (sMinute.Length > )
sMinute = sMinute.Substring(, );
sReturn = sMinute;
}
return sReturn;
}
public bool ScanPort()
{ try
{
if (Opened)
{
Close();
Open();
}
else
{
Open();//打开串口 }
byte[] bytRead = Read();
Close();
if (Encoding.ASCII.GetString(bytRead, , bytRead.Length).IndexOf("$GP") >= )
return true;
else
return false;
}
catch
{
return false;
}
}
}
class HexCon
{
// 把十六进制字符串转换成字节型和把字节型转换成十六进制字符串 converter hex string to byte and byte to hex string
public static string ByteToString(byte[] InBytes)
{
string StringOut = "";
foreach (byte InByte in InBytes)
{
StringOut = StringOut + String.Format("{0:X2} ", InByte);
}
return StringOut;
}
public static byte[] StringToByte(string InString)
{
string[] ByteStrings;
ByteStrings = InString.Split(" ".ToCharArray());
byte[] ByteOut;
ByteOut = new byte[ByteStrings.Length - ];
for (int i = ; i == ByteStrings.Length - ; i++)
{
ByteOut[i] = Convert.ToByte(("0x" + ByteStrings[i]));
}
return ByteOut;
}
}
(3)界面设计
(4)获取GPS相关信息
public partial class TestGps : Form
{
#region
private delegate void mySerial();
private delegate void myTestEventHandler();
//private event myTestEventHandler myTest();
private string gprsData = string.Empty; private bool isClosed = false; public delegate void LocationShow(string Latitude, string Longitude, string status, bool isNorth, bool isEast, string time); public delegate void portDataShow(string data); double beginTime = ;
double endTime = ;
CLR_GPS gps; bool firstLocation = false; #endregion public TestGps()
{
InitializeComponent();
gps = new CLR_GPS();
gps.refreshLocation += gps_refreshLocation;
gps.refreshTime += gps_refreshTime;
gps.refreshDate += gps_refreshDate;
//gps.refreshAltitude += new CLR_GPS.altitudeEventHandler(gps_refreshAltitude);
gps.refreshSatellite += gps_refreshSatellite;
gps.refreshAllNumber += gps_refreshAllNumber;
gps.InitModule();
} #region Satellite Total
void gps_refreshAllNumber(string num)
{
if (isClosed)
{
return;
}
Invoke(new portDataShow(showNumber), num);
} void showNumber(string num)
{
textBoxAll.Text = num;
}
#endregion #region Refresh effective satellite
void gps_refreshSatellite(string n)
{
if (isClosed)
{
return;
}
Invoke(new portDataShow(showSatellite), n);
} void showSatellite(string n)
{
textBoxsatellite.Text = n;
}
#endregion #region refresh Altitude
//void gps_refreshAltitude(string altitude)
//{
// if (isClosed)
// {
// return;
// }
// this.Invoke(new portDataShow(showAltitude), altitude);
//} //void showAltitude(string altitude)
//{
// textBoxAltitude.Text = altitude;
//}
#endregion #region refresh date
void gps_refreshDate(string date)
{
if (isClosed)
{
return;
}
Invoke(new portDataShow(showDate), date);
} void showDate(string date)
{
textBoxDate.Text = date;
}
#endregion #region refresh time
void gps_refreshTime(string time)
{
if (isClosed)
{
return;
}
Invoke(new portDataShow(showTime), time);
} void showTime(string time)
{
textBoxTime.Text = time.Substring(, ) + ":" + time.Substring(, ) + ":" + time.Substring(, );
}
#endregion #region refresh location
void gps_refreshLocation(string Latitude, string Longitude, string status, bool isNorth, bool isEast, string time)
{
if (isClosed)
{
return;
}
Invoke(new LocationShow(showLocation), Latitude, Longitude, status, isNorth, isEast, time);
}
private void showLocation(string Latitude, string Longitude, string status, bool isNorth, bool isEast,string time)
{
if (!firstLocation)
{
firstLocation = true;
endTime = DateTime.Now.Minute * + DateTime.Now.Second; textBoxAccess.Text = Convert.ToString(DateTime.Now.TimeOfDay.ToString()); textBoxSpend.Text = Convert.ToString(endTime - beginTime);
}
textBoxLa.Text = Latitude;
textBoxLo.Text = Longitude;
} #endregion #region 窗体加载事件 判断操作系统是中文还是英文 进行控件赋值
private void MainForm_Load(object sender, EventArgs e)
{
int n = ;
if (isEnglish())
{
n = ;
}
labelDate.Text = TextDate[n];
labelTime.Text = TextTime[n];
labelLa.Text = TextLatitude[n];
labelLo.Text = TextLongitude[n];
labelAll.Text = TextSatelliteTotal[n];
labelUsed.Text = TextEffectiveSatellite[n];
//labelAltitude.Text = TextAltitude[n];
labelCurr.Text = TextCurr[n];
labelBegin.Text = TextBegin[n];
labelAccess.Text = TextAcees[n];
labelSpend.Text = TextSpend[n];
labelSecond.Text = TextSecond[n];
}
#endregion #region 获取GPS相应信息
private void btnGet_Click(object sender, EventArgs e)
{
//gps.InitModule();
btnStop.Enabled = true; btnGet.Enabled = false; btnStop.Focus(); if (!gps.startReceive())
{
MessageBox.Show("get data failure, the port is not open.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
return;
}
isClosed = false;
firstLocation = false;
beginTime = System.DateTime.Now.Minute * + DateTime.Now.Second;
textBoxBeginTime.Text = Convert.ToString(DateTime.Now.TimeOfDay.ToString()); }
#endregion #region 读取注册表信息,判断是不是英文操作系统
/// <summary>
/// Read registry information, judge whether English OS 读取注册表信息,判断是不是英文操作系统
/// </summary>
/// <returns></returns>
public static bool isEnglish()
{
try
{
var RegKey = Registry.CurrentUser.OpenSubKey(@"ControlPanel\Appearance");
if ("Windows 标准" == RegKey.GetValue("Current").ToString())
return false;
return true;
}
catch (Exception)
{
return true;
}
}
#endregion #region 停止GPS接收
private void btnStop_Click(object sender, EventArgs e)
{
gps.stopReceive(); isClosed = true;
btnGet.Enabled = true; btnStop.Enabled = false; btnGet.Focus(); }
#endregion #region GPS下电操作
private void MainForm_Closed(object sender, EventArgs e)
{
gps.FreeModule();
}
#endregion #region Interface language 界面语言 private string[] TextDate = new [] { "日期:", "Date:" };
private string[] TextTime = new [] { "UTC时间:", "UTC time:" };
private string[] TextLongitude = new [] { "经度:", "Longitude:" };
private string[] TextLatitude = new [] { "纬度", "Latitude:" };
private string[] TextSatelliteTotal =new []{"可视卫星数量:","Satellite Total:"};
private string[] TextEffectiveSatellite =new []{"可用卫星数量:","Effective number:"};
private string[] TextAltitude = new [] { "海拔:", "Altitude:" };
private string[] TextCurr = new [] { "当前时间等于UTC时间加上当地所属时区", "current time=UTC time +local time zone" };
private string[] TextBegin = new [] { "开始时间:", "Begin time:" };
private string[] TextAcees = new [] { "获取时间", "Access time:" };
private string[] TextSpend = new [] { "有效定位时间", "Spending time:" }; private string[] TextSecond = new string[] { "秒", "second" };
#endregion #region 关闭窗口按钮
private void buttonClosed_Click(object sender, EventArgs e)
{
if (btnStop.Enabled)
{
btnStop_Click(sender, e);
}
Close();
}
#endregion
}