private async void OperateQJDatagram(VCParamsDatagramModel data) { try { await Task.Factory.StartNew(() => { Data.UDPData UDPData = new Data.UDPData(); UDPData.DeviceID = "QJ" + data.DeviceID; UDPData.QINGJIAO_X = data.QingQiao[0, 0]; UDPData.QINGJIAO_Y = data.QingQiao[0, 1]; UDPData.Time = data.Time; UDPData.Temprature = data.Temprature; try { m_DataGridViewerWnd.m_QingJiao_DataViewWnd.UpdateListView(UDPData); if (MapDataColorAdjustHandler != null) { object ID = UDPData.DeviceID; MapDataColorAdjustHandler(ID); } } catch (Exception) { } }); } catch (Exception) { } }
private async void OperateSZDatagram(VCParamsDatagramModel data) { try { await Task.Factory.StartNew(() => { Data.UDPData UDPData = new Data.UDPData(); UDPData.DeviceID = "SZ" + data.DeviceID; //UDPData.ZHENDONG = data.ZHENDONG[0]; UDPData.CHENJIANG = data.CHENJIANG[0]; UDPData.Time = data.Time; //UDPData.Temprature = data.Temprature; try { m_DataGridViewerWnd.m_ShuiZhun_DataViewWnd.UpdateListView(UDPData); if (MapDataColorAdjustHandler != null) { object ID = UDPData.DeviceID; MapDataColorAdjustHandler(ID); } } catch (Exception) { } }); } catch (Exception) { } }
private int Analyze(string context, int offset = 0) { //查找数据报开头 int index = context.IndexOf(HEADERCONTEXT, offset); if (-1 == index) { return(offset); } int byteCount = 2;//7E7E(2字节) index += HEADERCONTEXT.Length; string sDeviceID = context.Substring(index, 16); Byte[] DeviceIDByte = Data.Funs.SToBa(sDeviceID); byteCount += 8;//8字节设备唯一编号 index += 16; string sUserID = context.Substring(index, 10); Byte[] UserIDByte = Data.Funs.SToBa(sUserID); byteCount += 5;//5字节用户编号 index += 10; string sDataStyle = context.Substring(index, 2); Byte[] DataStyleByte = Data.Funs.SToBa(sDataStyle); byteCount += 1;//1字节功能码 index += 2; string sDataLength = context.Substring(index, 4); byte[] DataLengthByte = Data.Funs.SToBa(sDataLength); int nDataLength = Data.Funs.ByteToInt(DataLengthByte); nDataLength = nDataLength & 0xFFF; byteCount += 2;//2字节报文长度 index += 4; string sDataBegin = context.Substring(index, 2); byte[] DataBeginByte = Data.Funs.SToBa(sDataBegin); byteCount += 1;//1字节起始符 byteCount += nDataLength; byteCount += 1; offset = index; byte[] FlowIDByte; byte[] TimeByte; if (sDataBegin == "16") { throw new Exception("Capture 16"); } if (sDataBegin == "02") { VCParamsDatagramModel tDatagramModel = new VCParamsDatagramModel(); tDatagramModel.DeviceID = sDeviceID; index += 2; string sFlowID = context.Substring(index, 4); FlowIDByte = Data.Funs.SToBa(sFlowID);//2字节流水号 index += 4; //index += 12;//6字节发送时间 string sTime = context.Substring(index, 12); TimeByte = Data.Funs.SToBa(sTime); string TimeYear = context.Substring(index, 2); int nTimeYear = Convert.ToByte(TimeYear); string sTimeYear = string.Format("{0}{1}", 20, nTimeYear); nTimeYear = Convert.ToInt16(sTimeYear); index += 2; string sTimeMonth = context.Substring(index, 2); int nTimeMonth = Convert.ToByte(sTimeMonth); index += 2; string sTimeDay = context.Substring(index, 2); int nTimeDay = Convert.ToByte(sTimeDay); index += 2; string sTimeHour = context.Substring(index, 2); int nTimeHour = Convert.ToByte(sTimeHour); index += 2; string sTimeMinute = context.Substring(index, 2); int nTimeMinute = Convert.ToByte(sTimeMinute); index += 2; string sTimeSecond = context.Substring(index, 2); int nTimeSecond = Convert.ToByte(sTimeSecond); tDatagramModel.Time = new DateTime(nTimeYear, nTimeMonth, nTimeDay, nTimeHour, nTimeMinute, nTimeSecond); index += 2; index += 12;//6字节采集时间 string sStationStyle = context.Substring(index, 2); int nStationStyle = Convert.ToByte(sStationStyle); tDatagramModel.StationType = nStationStyle; //1字节测站类型 int nDataParaLength = (nDataLength - 15) / 8; //8字节数据(2要素编码地址 2数据类型 4数据域) index += 2; for (int i = 0; i < nDataParaLength; i++) { string sDataParaStyle = context.Substring(index, 4); byte[] DataParaStyle = Data.Funs.SToBa(sDataParaStyle); int nDataParaStyle = Data.Funs.ByteToInt(DataParaStyle); int nDataParaStyleIndex = nDataParaStyle & 0x1F; nDataParaStyle = nDataParaStyle >> 5; index += 4; //2要素编码地址 string sDataParaFloat = context.Substring(index, 2); byte[] nDataParaFloat = Data.Funs.SToBa(sDataParaFloat); //高4位FLOAT 低4位正负、幂 int nDataParaF = nDataParaFloat[0] >> 4; //float int nDataPara = nDataParaFloat[0] & 0x0F; int nDataParaZ = nDataPara >> 3; //正负0为正,1为负。 int nDataParaM = nDataPara & 0x08; //幂 index += 4; //2数据类型(低1字节保留) string sDataParaValue = context.Substring(index, 8); index += 8; //4数据域 float fDataParaValue = 0; if (nDataParaF == 1) { byte[] DataParaValueByte = Data.Funs.SToBa(sDataParaValue); fDataParaValue = Data.Funs.ByteToFloat(DataParaValueByte); } if (nDataParaF == 0) { byte[] DataParaValueByte = Data.Funs.SToBa(sDataParaValue); fDataParaValue = Data.Funs.ByteToInt(DataParaValueByte); } if (nDataParaZ == 1) { fDataParaValue = -fDataParaValue; } fDataParaValue = fDataParaValue * (float)Math.Pow(10, nDataParaM); if (tDatagramModel.TempraturedataDic.ContainsKey(nDataParaStyle)) { tDatagramModel.Temprature = fDataParaValue; } if (tDatagramModel.QingQiaodataDic.ContainsKey(nDataParaStyle)) { tDatagramModel.QingQiao[nDataParaStyleIndex, tDatagramModel.QingQiaodataDic[nDataParaStyle]] = fDataParaValue; if ((nDataParaStyleIndex + 1) > tDatagramModel.QingQiaodataCount) { tDatagramModel.QingQiaodataCount = nDataParaStyleIndex + 1; } } if (tDatagramModel.JIASUDUdataDic.ContainsKey(nDataParaStyle)) { tDatagramModel.JIASUDU[nDataParaStyleIndex, tDatagramModel.JIASUDUdataDic[nDataParaStyle]] = fDataParaValue; if ((nDataParaStyleIndex + 1) > tDatagramModel.JIASUDUdataCount) { tDatagramModel.JIASUDUdataCount = nDataParaStyleIndex + 1; } } if (tDatagramModel.ZHENDONGdataDic.ContainsKey(nDataParaStyle)) { tDatagramModel.ZHENDONG[nDataParaStyleIndex] = fDataParaValue; if ((nDataParaStyleIndex + 1) > tDatagramModel.ZHENDONGdataCount) { tDatagramModel.ZHENDONGdataCount = nDataParaStyleIndex + 1; } } if (tDatagramModel.CHENJIANGdataDic.ContainsKey(nDataParaStyle)) { tDatagramModel.CHENJIANG[nDataParaStyleIndex] = fDataParaValue; if ((nDataParaStyleIndex + 1) > tDatagramModel.CHENJIANGdataCount) { tDatagramModel.CHENJIANGdataCount = nDataParaStyleIndex + 1; } } } string sEnd = context.Substring(index, 2); if (sEnd == "03") { //解析数据报 if (null != OnAnalyzed) { VCDatagramEventArg m = new VCDatagramEventArg(tDatagramModel); OnAnalyzed(this, m); Byte[] data = new byte[30]; data[0] = 0x7E; data[1] = 0x7E; Array.Copy(DeviceIDByte, 0, data, 2, DeviceIDByte.Length); Array.Copy(UserIDByte, 0, data, 2 + DeviceIDByte.Length, UserIDByte.Length); data[15] = DataStyleByte[0]; data[16] = 0x80; data[17] = 0x08; data[18] = 0x02; Array.Copy(FlowIDByte, 0, data, 19, FlowIDByte.Length); DateTime Date = DateTime.Now; byte[] dataTime = new byte[6]; string sYear = Date.Year.ToString(); byte[] byteYear = Data.Funs.SToBa(sYear); dataTime[0] = byteYear[1]; string sMonth = Date.Month.ToString(); dataTime[1] = Convert.ToByte(sMonth, 16); string sDay = Date.Day.ToString(); dataTime[2] = Convert.ToByte(sDay, 16); string sHour = Date.Hour.ToString(); dataTime[3] = Convert.ToByte(sHour, 16); string sMinute = Date.Minute.ToString(); dataTime[4] = Convert.ToByte(sMinute, 16); string sSecond = Date.Second.ToString(); dataTime[5] = Convert.ToByte(sSecond, 16); Array.Copy(dataTime, 0, data, 19 + FlowIDByte.Length, dataTime.Length); data[27] = 0x04; //Create CRC UInt16 crc = Data.Funs.calculateCRC(data, Convert.ToUInt16(data.Length - 8), 6); byte[] byteData = BitConverter.GetBytes((int)crc); data[data.Length - 2] = byteData[0]; data[data.Length - 1] = byteData[1]; //string scrc = GetCRC16(data); //byte[] crcByte = SToBa(scrc); //Array.Copy(crcByte, 0, data, 28, crcByte.Length); SendAnswerData(data); } } index += 2; index += 4; } else { return(offset); } //重新赋值下一个数据报开始位置 //并递归调用解析数据报 offset = index + 1; return(Analyze(context, offset)); }
public VCDatagramEventArg(VCParamsDatagramModel data) { Data = data; }
//public event EventHandler<VCDatagramEventArg> OnAnalyzed; private int Analyze(string context, int offset = 0) { string sDeviceAdr; //查找数据报开头 int index = context.IndexOf(HEADERCONTEXT, offset); if (-1 == index) { index = context.IndexOf(HEADERCONTEXT2, offset + 1); if (-1 == index) { return(offset); } index -= 2; sDeviceAdr = context.Substring(index, 2); index += 2; index += HEADERCONTEXT2.Length; string sDataParaI = context.Substring(index, 4); Byte[] DataParaI = Data.Funs.SToBa(sDataParaI); int nDataParaI = Data.Funs.ByteToInt(DataParaI); index += 4; string sDataParaF = context.Substring(index, 4); Byte[] DataParaF = Data.Funs.SToBa(sDataParaF); int nDataParaF = Data.Funs.ByteToInt(DataParaF); float fDataParaF = nDataParaF / 65535; float fDataPara = nDataParaI + fDataParaF; { VCParamsDatagramModel tDatagramModel = new VCParamsDatagramModel(); int DeviceAdr = Int32.Parse(sDeviceAdr, System.Globalization.NumberStyles.HexNumber); //if(DeviceAdr < 10) //{ // tDatagramModel.DeviceID = sDeviceAdr; //} //else { tDatagramModel.DeviceID = DeviceAdr.ToString(); } tDatagramModel.CHENJIANGdataCount = 1; tDatagramModel.CHENJIANG[0] = fDataPara; tDatagramModel.Time = DateTime.Now; VCDatagramEventArg m = new VCDatagramEventArg(tDatagramModel); m_DataReceiverWnd.DatagramAnalyzerCtrl_OnAnalyzed(null, m); } index += 4; string sEnd = context.Substring(index, 4); } else { //int byteCount = 2;//680D(2字节) index += HEADERCONTEXT.Length; sDeviceAdr = context.Substring(index, 2); //Byte[] DeviceID = SToBa(sDeviceAdr); index += 2; string sDataStyle = context.Substring(index, 2); //Byte[] DataStyleByte = SToBa(sDataStyle); if (sDataStyle == "84") { index += 2; string sDataParaXZ = context.Substring(index, 2); byte[] nDataParaFloatX = Data.Funs.SToBa(sDataParaXZ);//高4位正负 int nDataParaXZ = nDataParaFloatX[0] >> 4; int nDataParaX = nDataParaFloatX[0] & 0x0F; int nDataParaXI1 = nDataParaX * 10; index += 2; string sDataParaXI = context.Substring(index, 2); Byte[] DataParaXI = Data.Funs.SToBa(sDataParaXI); int nDataParaXI0 = DataParaXI[0] >> 4; int nDataParaXI = nDataParaXI1 + nDataParaXI0; int nDataParaXF1 = DataParaXI[0] & 0x0F; double fDataParaXF1 = nDataParaXF1 * 0.1; //int nDataParaXI = Funs.Bcd2Int(DataParaXI, 0, 1); index += 2; string sDataParaXF1 = context.Substring(index, 2); Byte[] DataParaXF1 = Data.Funs.SToBa(sDataParaXF1); int nDataParaXF2 = Data.Funs.Bcd2Int(DataParaXF1, 0, 1); double fDataParaXF2 = nDataParaXF2 * 0.001; //index += 2; //string sDataParaXF2 = context.Substring(index, 2); //Byte[] DataParaXF2 = SToBa(sDataParaXF2); //int nDataParaXF2 = Funs.Bcd2Int(DataParaXF2, 0, 1); //double fDataParaXF2 = nDataParaXF2 * 0.0001; double fDataParaXF = nDataParaXI + fDataParaXF1 + fDataParaXF2; if (nDataParaXZ == 1) { fDataParaXF = -fDataParaXF; } //index += 2; //string sDataParaYZ = context.Substring(index, 2); //index += 2; //string sDataParaYI = context.Substring(index, 2); //Byte[] DataParaYI = SToBa(sDataParaYI); //int nDataParaYI = Funs.Bcd2Int(DataParaYI, 0, 1); //index += 2; //string sDataParaYF1 = context.Substring(index, 2); //Byte[] DataParaYF1 = SToBa(sDataParaYF1); //int nDataParaYF1 = Funs.Bcd2Int(DataParaYF1, 0, 1); //double fDataParaYF1 = nDataParaYF1 * 0.01; //index += 2; //string sDataParaYF2 = context.Substring(index, 2); //Byte[] DataParaYF2 = SToBa(sDataParaYF2); //int nDataParaYF2 = Funs.Bcd2Int(DataParaYF2, 0, 1); //double fDataParaYF2 = nDataParaYF2 * 0.0001; //double fDataParaYF = nDataParaYI + fDataParaYF1 + fDataParaYF2; //if (sDataParaYZ == "10") //{ // fDataParaYF = -fDataParaYF; //} index += 2; string sDataParaYZ = context.Substring(index, 2); byte[] nDataParaFloatY = Data.Funs.SToBa(sDataParaYZ);//高4位正负 int nDataParaYZ = nDataParaFloatY[0] >> 4; int nDataParaY = nDataParaFloatY[0] & 0x0F; int nDataParaYI1 = nDataParaY * 10; index += 2; string sDataParaYI = context.Substring(index, 2); Byte[] DataParaYI = Data.Funs.SToBa(sDataParaYI); int nDataParaYI0 = DataParaYI[0] >> 4; int nDataParaYI = nDataParaYI1 + nDataParaYI0; int nDataParaYF1 = DataParaYI[0] & 0x0F; double fDataParaYF1 = nDataParaYF1 * 0.1; //int nDataParaXI = Funs.Bcd2Int(DataParaXI, 0, 1); index += 2; string sDataParaYF1 = context.Substring(index, 2); Byte[] DataParaYF1 = Data.Funs.SToBa(sDataParaYF1); int nDataParaYF2 = Data.Funs.Bcd2Int(DataParaYF1, 0, 1); double fDataParaYF2 = nDataParaYF2 * 0.001; //index += 2; //string sDataParaXF2 = context.Substring(index, 2); //Byte[] DataParaXF2 = SToBa(sDataParaXF2); //int nDataParaXF2 = Funs.Bcd2Int(DataParaXF2, 0, 1); //double fDataParaXF2 = nDataParaXF2 * 0.0001; double fDataParaYF = nDataParaYI + fDataParaYF1 + fDataParaYF2; if (nDataParaYZ == 1) { fDataParaYF = -fDataParaYF; } index += 2; string sDataParaTZ = context.Substring(index, 2); byte[] nDataParaFloatT = Data.Funs.SToBa(sDataParaTZ);//高4位正负 int nDataParaTZ = nDataParaFloatT[0] >> 4; int nDataParaT = nDataParaFloatT[0] & 0x0F; int nDataParaTI1 = nDataParaT * 10; index += 2; string sDataParaTI = context.Substring(index, 2); Byte[] DataParaTI = Data.Funs.SToBa(sDataParaTI); int nDataParaTI0 = DataParaTI[0] >> 4; int nDataParaTI = nDataParaTI1 + nDataParaTI0; int nDataParaTF1 = DataParaTI[0] & 0x0F; double fDataParaTF1 = nDataParaTF1 * 0.1; //int nDataParaXI = Funs.Bcd2Int(DataParaXI, 0, 1); index += 2; string sDataParaTF1 = context.Substring(index, 2); Byte[] DataParaTF1 = Data.Funs.SToBa(sDataParaTF1); int nDataParaTF2 = Data.Funs.Bcd2Int(DataParaTF1, 0, 1); double fDataParaTF2 = nDataParaTF2 * 0.001; //index += 2; //string sDataParaXF2 = context.Substring(index, 2); //Byte[] DataParaXF2 = SToBa(sDataParaXF2); //int nDataParaXF2 = Funs.Bcd2Int(DataParaXF2, 0, 1); //double fDataParaXF2 = nDataParaXF2 * 0.0001; double fDataParaTF = nDataParaTI + fDataParaTF1 + fDataParaTF2; if (nDataParaTZ == 1) { fDataParaTF = -fDataParaTF; } { VCParamsDatagramModel tDatagramModel = new VCParamsDatagramModel(); int DeviceAdr = Int32.Parse(sDeviceAdr, System.Globalization.NumberStyles.HexNumber); //if (DeviceAdr < 10) //{ // tDatagramModel.DeviceID = sDeviceAdr; //} //else { tDatagramModel.DeviceID = DeviceAdr.ToString(); } int indexOfFirstNotUse = -1; for (int i = 0; i < UserHelper.DeviceDataAdrList.Count; ++i) { if (UserHelper.DeviceDataAdrList[i].Key == DeviceAdr) { indexOfFirstNotUse = i; if (UserHelper.DeviceDataAdrList[indexOfFirstNotUse].Value == 0) { continue; } break; } } if (UserHelper.DeviceDataAdrList[indexOfFirstNotUse].Value == 1) { tDatagramModel.QingQiaodataCount = 1; tDatagramModel.QingQiao[0, 0] = (float)fDataParaXF; tDatagramModel.QingQiao[0, 1] = (float)fDataParaYF; } if (UserHelper.DeviceDataAdrList[indexOfFirstNotUse].Value == 2) { tDatagramModel.JIASUDUdataCount = 1; tDatagramModel.JIASUDU[0, 0] = (float)fDataParaXF; tDatagramModel.JIASUDU[0, 1] = (float)fDataParaYF; } tDatagramModel.Temprature = (float)fDataParaTF; tDatagramModel.Time = DateTime.Now; VCDatagramEventArg m = new VCDatagramEventArg(tDatagramModel); m_DataReceiverWnd.DatagramAnalyzerCtrl_OnAnalyzed(null, m); } } index += 2; string sEnd = context.Substring(index, 2); } //重新赋值下一个数据报开始位置 //并递归调用解析数据报 offset = index + 1; return(Analyze(context, offset)); }