110,536
社区成员
发帖
与我相关
我的任务
分享
public byte[] Read(int NumBytes)
{
byte[] BufBytes;
byte[] OutBytes;
BufBytes = new byte[NumBytes]; if (hComm != INVALID_HANDLE_VALUE)
{
OVERLAPPED ovlCommPort = new OVERLAPPED();
int BytesRead = 0;
ReadFile(hComm, BufBytes, NumBytes, ref BytesRead, ref ovlCommPort);
try
{
OutBytes = new byte[BytesRead];
Array.Copy(BufBytes, 0, OutBytes, 0, BytesRead);
}
catch
{
return BufBytes;
}
}
else
{
throw (new ApplicationException("串口未打开!"));
}
return OutBytes;
// return BufBytes;
}
public string T2Time(string strDate, string strTime)
{
string dT = "20" + strDate.Substring(4, 2) + "-" + strDate.Substring(2, 2) + "-" + strDate.Substring(0, 2);
string TT = Convert.ToString(Convert.ToInt32(strTime.Substring(0, 2))) + ":" + strTime.Substring(2, 2) + ":" + strTime.Substring(4, 2);
DateTime T = Convert.ToDateTime(dT + " " + TT);
T = T.AddHours(8);
return T.ToString();
}
public string DM2DD(string DegreeMinutes)
{
//转换NMEA协议的“度分”格式为十进制“度度”格式
string sDegree;
string sMinute;
string sReturn = "";
if (DegreeMinutes.IndexOf(".") == 4)
{
//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(0, 2));
double sDegree2 = Convert.ToDouble(DegreeMinutes.Substring(2, DegreeMinutes.Length - 2));
string sTmp = Convert.ToString(sDegree2 / 60);
sDegree2 = Convert.ToDouble(sTmp.Substring(0, sTmp.Length));
sDegree2 = sDegree2 / 10000;
if (sDegree2 > 1)//接收为4位、6位小数,当4为除以10000正确,当6位时要再除以100 如:{E:10338.9163 N:2501.3304 、E:10338.925024 N:2501.255818}
{
sDegree2 = sDegree2 / 100;
}
sDegree = Convert.ToString(sDegree1 + sDegree2);
if (sDegree.Length > 11)
sDegree = sDegree.Substring(0, 11);
sReturn = sDegree;
}
else if (DegreeMinutes.IndexOf(".") == 5)
{
//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(0, 3));
double sMinute2 = Convert.ToDouble(DegreeMinutes.Substring(3, DegreeMinutes.Length - 3));
string sTmp = Convert.ToString(sMinute2 / 60);
sMinute2 = Convert.ToDouble(sTmp.Substring(0, sTmp.Length));
sMinute2 = sMinute2 / 10000;
if (sMinute2 > 1)//接收为4位、6位小数,当4为除以10000正确,当6位时要再除以100 如:{E:10338.9163 N:2501.3304 、E:10338.925024 N:2501.255818}
{
sMinute2 = sMinute2 / 100;
}
sMinute = Convert.ToString(sMinute1 + sMinute2);
if (sMinute.Length > 10)
sMinute = sMinute.Substring(0, 10);
sReturn = sMinute;
}
return sReturn;
}
public bool ScanPort()
{
try
{
if (Opened)
{
Close();
Open();
}
else
{
Open();//打开串口
}
byte[] bytRead = Read(512);
Close();
if (Encoding.ASCII.GetString(bytRead, 0, bytRead.Length).IndexOf("$GP") >= 0)
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 - 1];
for (int i = 0; i == ByteStrings.Length - 1; i++)
{
ByteOut[i] = Convert.ToByte(("0x" + ByteStrings[i]));
}
return ByteOut;
}
public void Open()
{
DCB dcbCommPort = new DCB();
COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS();
// 打开串口 OPEN THE COMM PORT.
hComm = CreateFile(PortNum, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
// 如果串口没有打开,就打开 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 = 0;
ctoCommPort.WriteTotalTimeoutMultiplier = 0;
ctoCommPort.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(hComm, ref ctoCommPort);
// 设置串口 SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS.
GetCommState(hComm, ref dcbCommPort);
dcbCommPort.BaudRate = BaudRate;
dcbCommPort.flags = 0;
//dcb.fBinary=1;
dcbCommPort.flags |= 1;
if (Parity > 0)
{
//dcb.fParity=1
dcbCommPort.flags |= 2;
}
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);
Opened = false;
}
}
public byte[] Read(int NumBytes)
{
byte[] BufBytes;
byte[] OutBytes;
BufBytes = new byte[NumBytes];
if (hComm != INVALID_HANDLE_VALUE)
{
OVERLAPPED ovlCommPort = new OVERLAPPED();
int BytesRead = 0;
ReadFile(hComm, BufBytes, NumBytes, ref BytesRead, ref ovlCommPort);
try
{
OutBytes = new byte[BytesRead];
Array.Copy(BufBytes, 0, OutBytes, 0, 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 = 0;
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:速度,M:海拔 是数字从1开始,即以“,”分隔的位置
///返回查找到指定位置的字符串
string handerStrGPRMC = "$GPRMC";//GPS串头
string handerStrGPGGA = "$GPGGA";
int findHanderGPRMC = strGPS.IndexOf(handerStrGPRMC);//看是否含有GPS串头
int findHanderGPGGA = strGPS.IndexOf(handerStrGPGGA);//看是否含有GPS串头
if (strGPS.IndexOf(handerStrGPRMC) < 0 && strGPS.IndexOf(handerStrGPGGA) < 0)
{
return "-1";
}
else
{
if (findHanderGPGGA > 0)//GPGGA
{
strGPS = strGPS.Substring(findHanderGPGGA, strGPS.Length - findHanderGPGGA);
string[] ArryTmp = strGPS.Split(",".ToCharArray());
try
{
if (ArryTmp[2] == "V")
{
return "V";
//没有信号
}
else
{
switch (strFind)
{
case "X":
return DM2DD(ArryTmp[4]);
//return ArryTmp[4];
case "Y":
return DM2DD(ArryTmp[2]);
//return ArryTmp[2];
case "M":
return ArryTmp[9];
case "T":
return T2Time(ArryTmp[9], ArryTmp[1]);
case "V":
return Convert.ToString( Convert.ToDouble( ArryTmp[7] )* 1.852 );
default:
return "V";
}
}
}
catch
{
return "V";
}
}
else//GPRMC
{
strGPS = strGPS.Substring(findHanderGPRMC, strGPS.Length - findHanderGPRMC);
string[] ArryTmp = strGPS.Split(",".ToCharArray());
try
{
if (ArryTmp[2] == "V")
{
return "V";//没有信号
}
else
{
switch (strFind)
{
case "X":
return DM2DD(ArryTmp[5]);
//return ArryTmp[5];
case "Y":
return DM2DD(ArryTmp[3]);
//return ArryTmp[3];
case "T":
return T2Time(ArryTmp[9], ArryTmp[1]);
case "V":
return Convert.ToString(Convert.ToDouble(ArryTmp[7]) * 1.852);
default:
return "V";
}
}
}
catch
{
return "V";
}
}
}
}