修改了解析北斗报文和数据报文部分
This commit is contained in:
parent
38b1f9eafe
commit
f954de289d
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -205,21 +205,24 @@ namespace ZTTMS_Manage_ZDPLFZBD_20230301
|
||||
//数据接收
|
||||
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
|
||||
{
|
||||
byte[] data = new byte[512];
|
||||
int i = 0;
|
||||
//byte[] data = new byte[512];
|
||||
//int i = 0;
|
||||
|
||||
while (this.serialPort1.BytesToRead > 0)
|
||||
//while (this.serialPort1.BytesToRead > 0)
|
||||
//{
|
||||
// data[i] = (byte)serialPort1.ReadByte();
|
||||
// i++;
|
||||
//}
|
||||
|
||||
byte[] data = new byte[serialPort1.BytesToRead];
|
||||
int num=serialPort1.Read(data, 0, data.Length);
|
||||
|
||||
byte[] receiveData = new byte[num];
|
||||
for (int j = 0; j < num; j++)
|
||||
{
|
||||
data[i] = (byte)serialPort1.ReadByte();
|
||||
i++;
|
||||
receiveData[j] = data[j];
|
||||
}
|
||||
|
||||
byte[] receiveData = new byte[data.Length];
|
||||
for (int j = 0; j < data.Length; j++)
|
||||
{
|
||||
receiveData[i] = data[i];
|
||||
}
|
||||
|
||||
|
||||
Thread trdDataAnalysis = new Thread(DataAnalysis);
|
||||
trdDataAnalysis.IsBackground = true;
|
||||
trdDataAnalysis.Start(receiveData);
|
||||
@ -244,16 +247,16 @@ namespace ZTTMS_Manage_ZDPLFZBD_20230301
|
||||
{
|
||||
byte[] newDataTemp = o as byte[];
|
||||
|
||||
//将byte数组转为16进制string(带-)
|
||||
string strData = BitConverter.ToString(newDataTemp, 1, newDataTemp.Length - 5);
|
||||
string[] data = strData.Split('-');
|
||||
////将byte数组转为16进制string(带-)
|
||||
//string strData = BitConverter.ToString(newDataTemp, 1, newDataTemp.Length - 5);
|
||||
//string[] data = strData.Split('-');
|
||||
|
||||
//16进制string
|
||||
string data1 = "";
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
data1 += data[i];
|
||||
}
|
||||
string data1 = ByteToString(newDataTemp);
|
||||
//for (int i = 0; i < data.Length; i++)
|
||||
//{
|
||||
// data1 += data[i];
|
||||
//}
|
||||
|
||||
//string 实际发的字符串
|
||||
string data2 = HexToString(data1);
|
||||
@ -267,39 +270,51 @@ namespace ZTTMS_Manage_ZDPLFZBD_20230301
|
||||
//电文内容拆分
|
||||
string[] content = data4.Split('*');
|
||||
|
||||
//异或校验字节
|
||||
string code = content[1].Substring(0, 2);
|
||||
byte checkCode = byte.Parse(code);
|
||||
//16进制string转byte[]
|
||||
byte[] recviveData = StringToBytes(content[0]);
|
||||
|
||||
//电文内容string
|
||||
string data5 = HexToString(content[0]);
|
||||
//接收数据校验
|
||||
byte checkCode = recviveData[recviveData.Length - 1];
|
||||
|
||||
//发的是字符串 a实际是其ascii码表示的byte
|
||||
|
||||
//string转byte[]
|
||||
byte[] checkTemp = Encoding.Default.GetBytes(data5);
|
||||
byte[] checkData = new byte[checkTemp.Length - 7];
|
||||
for (int i = 0; i < checkTemp.Length - 7; i++)
|
||||
byte[] buffer = new byte[recviveData.Length - 1];
|
||||
for (int i = 0; i < recviveData.Length - 1; i++)
|
||||
{
|
||||
checkData[i] = checkTemp[i];
|
||||
buffer[i] = recviveData[i];
|
||||
}
|
||||
|
||||
string[] recvData = data4.Split(' ');
|
||||
////异或校验字节
|
||||
//string code = content[1].Substring(0, 2);
|
||||
//byte checkCode = byte.Parse(code);
|
||||
|
||||
//电文内容string
|
||||
string data5 = HexToString(ByteToString(recviveData));
|
||||
|
||||
////发的是字符串 a实际是其ascii码表示的byte
|
||||
|
||||
////string转byte[]
|
||||
//byte[] checkTemp = Encoding.Default.GetBytes(data5);
|
||||
//byte[] checkData = new byte[checkTemp.Length - 7];
|
||||
//for (int i = 0; i < checkTemp.Length - 7; i++)
|
||||
//{
|
||||
// checkData[i] = checkTemp[i];
|
||||
//}
|
||||
|
||||
string[] recvData = data5.Split(' ');
|
||||
|
||||
//byte[] dataTemp = Encoding.Default.GetBytes(data4);
|
||||
|
||||
string path = Environment.CurrentDirectory;
|
||||
string savePath = path + "/" + "WaringData.txt";
|
||||
|
||||
for (int i = 0; i < data3.Length; i++)
|
||||
{
|
||||
Console.WriteLine(data3[i]);
|
||||
}
|
||||
//for (int i = 0; i < data3.Length; i++)
|
||||
//{
|
||||
// Console.WriteLine(data3[i]);
|
||||
//}
|
||||
|
||||
//发信人地址
|
||||
sqlTerminalEquipmentId = returnDeviceId(data3[2]);
|
||||
|
||||
if (newDataTemp[0] == 0x24 && newDataTemp[newDataTemp.Length - 1] == 0x0A && newDataTemp[newDataTemp.Length - 2] == 0x0D && CheckSum(newDataTemp) == checkCode && CheckSum(checkData) == byte.Parse(recvData[recvData.Length - 2]) && recvData[recvData.Length - 1] == "AABB" && sqlTerminalEquipmentId >= 0)
|
||||
if (CRC(ByteToString(buffer)) == checkCode.ToString())
|
||||
{
|
||||
sqlMessageType = recvData[0];
|
||||
sqlMessageId = recvData[1];
|
||||
@ -342,7 +357,7 @@ namespace ZTTMS_Manage_ZDPLFZBD_20230301
|
||||
link.LinkClose();
|
||||
#endregion
|
||||
|
||||
if (sqlMessageType == "Position" && sqlMessageId == "01" && recvData.Length == 7)
|
||||
if (sqlMessageType == "Position" && sqlMessageId == "01" && recvData.Length == 6)
|
||||
{
|
||||
//位置信息
|
||||
sqlDeviceName = "位置信息";
|
||||
@ -390,7 +405,7 @@ namespace ZTTMS_Manage_ZDPLFZBD_20230301
|
||||
}
|
||||
link.LinkClose();
|
||||
}
|
||||
else if (sqlMessageType == "Imu" && sqlMessageId == "01" && recvData.Length == 8)
|
||||
else if (sqlMessageType == "Imu" && sqlMessageId == "01" && recvData.Length == 7)
|
||||
{
|
||||
//姿态信息
|
||||
sqlDeviceName = "姿态信息";
|
||||
@ -487,7 +502,7 @@ namespace ZTTMS_Manage_ZDPLFZBD_20230301
|
||||
link.LinkClose();
|
||||
}
|
||||
}
|
||||
else if (sqlMessageType == "Imu" && sqlMessageId == "02" && recvData.Length == 8)
|
||||
else if (sqlMessageType == "Imu" && sqlMessageId == "02" && recvData.Length == 7)
|
||||
{
|
||||
//姿态信息
|
||||
sqlDeviceName = "姿态信息";
|
||||
@ -510,7 +525,7 @@ namespace ZTTMS_Manage_ZDPLFZBD_20230301
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Data2Error:" + ex.ToString());
|
||||
Console.WriteLine("TerminalAttitudeData2Error:" + ex.ToString());
|
||||
}
|
||||
|
||||
sqlRecordTime = DateTime.Now;
|
||||
@ -533,7 +548,7 @@ namespace ZTTMS_Manage_ZDPLFZBD_20230301
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Data2Error:" + ex.ToString());
|
||||
Console.WriteLine("TerminalAttitudeData2Error:" + ex.ToString());
|
||||
}
|
||||
|
||||
if (num > 0)
|
||||
@ -565,7 +580,7 @@ namespace ZTTMS_Manage_ZDPLFZBD_20230301
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Data2Error:" + ex.ToString());
|
||||
Console.WriteLine("TerminalAttitudeData2Error:" + ex.ToString());
|
||||
}
|
||||
|
||||
if (num > 0)
|
||||
@ -584,7 +599,7 @@ namespace ZTTMS_Manage_ZDPLFZBD_20230301
|
||||
link.LinkClose();
|
||||
}
|
||||
}
|
||||
else if (sqlMessageType == "Imu" && sqlMessageId == "03" && recvData.Length == 8)
|
||||
else if (sqlMessageType == "Imu" && sqlMessageId == "03" && recvData.Length == 7)
|
||||
{
|
||||
//姿态信息
|
||||
sqlDeviceName = "姿态信息";
|
||||
@ -607,7 +622,7 @@ namespace ZTTMS_Manage_ZDPLFZBD_20230301
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Data3Error:" + ex.ToString());
|
||||
Console.WriteLine("TerminalAttitudeData3Error:" + ex.ToString());
|
||||
}
|
||||
|
||||
sqlRecordTime = DateTime.Now;
|
||||
@ -630,7 +645,7 @@ namespace ZTTMS_Manage_ZDPLFZBD_20230301
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Data3Error:" + ex.ToString());
|
||||
Console.WriteLine("TerminalAttitudeData3Error:" + ex.ToString());
|
||||
}
|
||||
|
||||
if (num > 0)
|
||||
@ -662,7 +677,7 @@ namespace ZTTMS_Manage_ZDPLFZBD_20230301
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Data3Error:" + ex.ToString());
|
||||
Console.WriteLine("TerminalAttitudeData3Error:" + ex.ToString());
|
||||
}
|
||||
|
||||
if (num > 0)
|
||||
@ -681,7 +696,7 @@ namespace ZTTMS_Manage_ZDPLFZBD_20230301
|
||||
link.LinkClose();
|
||||
}
|
||||
}
|
||||
else if (sqlMessageType == "ManyPara" && sqlMessageId == "01" && recvData.Length == 8)
|
||||
else if (sqlMessageType == "ManyPara" && sqlMessageId == "01" && recvData.Length == 7)
|
||||
{
|
||||
//多参数
|
||||
sqlDeviceName = "多参数数据";
|
||||
@ -704,7 +719,7 @@ namespace ZTTMS_Manage_ZDPLFZBD_20230301
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Data1Error:" + ex.ToString());
|
||||
Console.WriteLine("TerminalMultiparameterData1Error:" + ex.ToString());
|
||||
}
|
||||
|
||||
sqlRecordTime = DateTime.Now;
|
||||
@ -749,7 +764,6 @@ namespace ZTTMS_Manage_ZDPLFZBD_20230301
|
||||
{
|
||||
while (cycleTime < 3)
|
||||
{
|
||||
string sqlId = IdHelper.GetId();
|
||||
int num = 0;
|
||||
|
||||
string sql = $"update ztms_observe_zhedapiaoliufuzi_data set temperature='{temperature}', ph='{ph}', conductivity='{conductivity}' where device_name='{sqlMessageType}' and serialnumber='{sqlMessageNum}';";
|
||||
@ -778,7 +792,7 @@ namespace ZTTMS_Manage_ZDPLFZBD_20230301
|
||||
link.LinkClose();
|
||||
}
|
||||
}
|
||||
else if (sqlMessageType == "ManyPara" && sqlMessageId == "02" && recvData.Length == 8)
|
||||
else if (sqlMessageType == "ManyPara" && sqlMessageId == "02" && recvData.Length == 7)
|
||||
{
|
||||
//多参数
|
||||
sqlDeviceName = "多参数数据";
|
||||
@ -875,14 +889,14 @@ namespace ZTTMS_Manage_ZDPLFZBD_20230301
|
||||
link.LinkClose();
|
||||
}
|
||||
}
|
||||
else if (sqlMessageType == "InPara" && sqlMessageId == "01" && recvData.Length == 10)
|
||||
else if (sqlMessageType == "InPara" && sqlMessageId == "01" && recvData.Length == 8)
|
||||
{
|
||||
sqlDeviceName = "工作环境参数";
|
||||
string etemperature = recvData[3];
|
||||
string humidity = recvData[4];
|
||||
string pressure = recvData[5];
|
||||
string dryBatteryVoltage = recvData[6];
|
||||
string destrcutionState = recvData[7];
|
||||
//string destrcutionState = recvData[7];
|
||||
//string destructionState1 = "";
|
||||
//if (destrcutionState == "0")
|
||||
// destructionState1 = "关闭";
|
||||
@ -988,25 +1002,95 @@ namespace ZTTMS_Manage_ZDPLFZBD_20230301
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串形式的多位字节信息转换为对应的字节数组
|
||||
/// </summary>
|
||||
/// <param name="s"></param>
|
||||
/// <returns></returns>
|
||||
public static byte[] StringToBytes(string s)
|
||||
{
|
||||
string[] str = s.Split(' ');
|
||||
int n = str.Length;
|
||||
|
||||
byte[] cmdBytes = null;
|
||||
int p = 0;
|
||||
|
||||
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
int sLen = str[k].Length;
|
||||
int bytesLen = sLen / 2;
|
||||
int position = 0;
|
||||
byte[] bytes = new byte[bytesLen];
|
||||
for (int i = 0; i < bytesLen; i++)
|
||||
{
|
||||
string abyte = str[k].Substring(position, 2);
|
||||
bytes[i] = Convert.ToByte(abyte, 16);
|
||||
position += 2;
|
||||
}
|
||||
|
||||
if (position >= 2)
|
||||
{
|
||||
byte[] cmdBytes2 = new byte[p + bytesLen];
|
||||
if (cmdBytes != null)
|
||||
{
|
||||
Array.Copy(cmdBytes, 0, cmdBytes2, 0, p);
|
||||
}
|
||||
Array.Copy(bytes, 0, cmdBytes2, p, bytesLen);
|
||||
cmdBytes = cmdBytes2;
|
||||
p += bytesLen;
|
||||
}
|
||||
}
|
||||
|
||||
return cmdBytes;
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region 计算校验码
|
||||
private byte CheckSum(byte[] buffer)
|
||||
private static string CRC(string cmdString)
|
||||
{
|
||||
byte sum = 0;
|
||||
foreach (byte b in buffer)
|
||||
try
|
||||
{
|
||||
if (b == 0x24 || b == 0x21)
|
||||
continue;
|
||||
if (b == 0x2A)
|
||||
break;
|
||||
sum ^= b;
|
||||
//CRC寄存器
|
||||
int CRCCode = 0;
|
||||
//将字符串拆分成为16进制字节数据然后两位两位进行异或校验
|
||||
for (int i = 1; i < cmdString.Length / 2; i++)
|
||||
{
|
||||
string cmdHex = cmdString.Substring(i * 2, 2);
|
||||
if (i == 1)
|
||||
{
|
||||
string cmdPrvHex = cmdString.Substring((i - 1) * 2, 2);
|
||||
CRCCode = (byte)Convert.ToInt32(cmdPrvHex, 16) ^ (byte)Convert.ToInt32(cmdHex, 16);
|
||||
}
|
||||
else
|
||||
{
|
||||
CRCCode = (byte)CRCCode ^ (byte)Convert.ToInt32(cmdHex, 16);
|
||||
}
|
||||
}
|
||||
return Convert.ToString(CRCCode, 16).ToUpper();//返回16进制校验码
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw;
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
public static string ByteToString(byte[] bytes)
|
||||
{
|
||||
string length = BitConverter.ToString(bytes, 0, bytes.Length);
|
||||
string[] length1 = length.Split('-');
|
||||
string length2 = "";
|
||||
for (int i = 0; i < length1.Length; i++)
|
||||
{
|
||||
length2 += length1[i];
|
||||
}
|
||||
return length2;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Binary file not shown.
Loading…
Reference in New Issue
Block a user