/// <summary> /// 解释数据包 /// </summary> /// <param name="obj"></param> public void AnalyData(HRTCPServer.HRMessagePack msgTran, string ipAddr) { if (msgTran.PacketType != 0x1B) { return; } switch (msgTran.Cmd) { case 0x39: ProcessInventoryReal(msgTran, ipAddr); break; default: break; } }
private void ProcessInventoryReal(HRTCPServer.HRMessagePack msgTran, string ipAddr) { string strDeviceAddr = ""; int lastRecord; DateTime lastTime; DateTime nowTime = DateTime.Now; if (msgTran.AryData.Length > 3) { try { int nLength = msgTran.AryData.Length; int nEpcLength = Convert.ToInt32(msgTran.AryData[0]); string strEPC = ByteArrayToString(msgTran.AryData, 4, nEpcLength - 4); string strPC = ByteArrayToString(msgTran.AryData, 2, 2); strDeviceAddr = string.Format("{0:X2}", msgTran.Opcode); string strRSSI = ""; double intRSSI = 0; int RSSI_M, RSSI_E, rssidBm = 0; rssidBm = msgTran.AryData[1]; RSSI_M = (rssidBm & 0X07); RSSI_E = (rssidBm & 0XF8); intRSSI = -(0X6E - 20 * Math.Log10((2 ^ RSSI_E) * (1 + (RSSI_M / 2 ^ 3)))); strRSSI = Convert.ToString(intRSSI).Substring(0, 3) + "dBm"; byte btAntId = msgTran.AryData[nEpcLength]; string StrTime = ""; StrTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); //盘点时间 string strAntId = (btAntId + 1).ToString(); string temRecord = strDeviceAddr + strEPC + StrTime; string identity = strDeviceAddr + strEPC; lastRecord = stringContain(temp, identity, index - 1); /******************************************************************************************/ /**这一段是写入数据库前的必要操作**/ Record rd = new Record(); string strSerialNo = ""; //流水号 string strNow = DateTime.Now.ToString("yyyyMMdd"); //获取当前日期 string strFormer = ""; string strLatter = ""; int number; /**开始获取流水号**/ strSerialNo = RecordMgr.GetLastNo(); //监控记录表的最后一位流水号 if (strSerialNo != "") { strFormer = strSerialNo.Substring(0, 8); //流水号前8位 strLatter = strSerialNo.Substring(8); //流水号后8位 if (strFormer == strNow) { number = Convert.ToInt32(strLatter) + 1; strLatter = string.Format("{0:00000000}", number); strSerialNo = strNow + strLatter; } else { strLatter = "00000001"; strSerialNo = strNow + strLatter; } } //用于第一次记录数据 else { strLatter = "00000001"; strSerialNo = strNow + strLatter; } //开始获取数据 rd.SerialNo = strSerialNo; rd.EPC = strEPC; rd.KeepPlace = ""; rd.MtNo = strDeviceAddr; rd.Time = StrTime; rd.Activity = "进入"; rd.Processor = 0000; /********************************************************************************************/ if (strEPC != "00 00 00 00 00 00 00 00 00 00 00 00 " && strEPC != "" && -1 == lastRecord) { temp[index] = temRecord; index = (index + 1) % BUFFSIZE; /* * 写入数据库 */ RecordMgr.AddRecord(rd); } if (strEPC != "00 00 00 00 00 00 00 00 00 00 00 00 " && strEPC != "" && lastRecord != -1) { lastTime = Convert.ToDateTime(temp[lastRecord].Substring(strDeviceAddr.Length + strEPC.Length, StrTime.Length)); if (timeErr(nowTime, lastTime) > 3) { /* * 写入数据库 */ RecordMgr.AddRecord(rd); temp[index] = temRecord; index = (index + 1) % BUFFSIZE; } else { temp[index] = temRecord; index = (index + 1) % BUFFSIZE; } } } catch (System.Exception ex) { //MessageBox.Show(ex.Message); } } }
public void RunReceiveDataCallback(byte[] btAryReceiveData, string ipAddr) { byte[] temp = new byte[1]; try { int nCount = btAryReceiveData.Length; byte[] btAryBuffer = new byte[nCount + m_nLenth]; Array.Copy(m_btAryBuffer, btAryBuffer, m_nLenth); Array.Copy(btAryReceiveData, 0, btAryBuffer, m_nLenth, btAryReceiveData.Length); //分析接收数据,以0x1B为数据起点,以协议中数据长度为数据终止点 int nIndex = 0; //当数据中存在1B时,记录数据的终止点 int nMarkIndex = 0; //当数据中不存在1B时,nMarkIndex等于数据组最大索引 for (int nLoop = 0; nLoop < btAryBuffer.Length; nLoop++) { if (btAryBuffer.Length > nLoop + 6) { if (btAryBuffer[nLoop] == 0x1B || btAryBuffer[nLoop] == 0x43) { int nLen = Convert.ToInt32(btAryBuffer[nLoop + 4] + btAryBuffer[nLoop + 5] * 256) + 6; if (nLoop + nLen < btAryBuffer.Length) { byte[] btAryAnaly = new byte[nLen + 1]; Array.Copy(btAryBuffer, nLoop, btAryAnaly, 0, nLen + 1); HRTCPServer.HRMessagePack msgTran = new HRTCPServer.HRMessagePack(btAryAnaly); if (AnalyCallback != null) { AnalyCallback(msgTran, ipAddr); } nLoop += nLen; nIndex = nLoop + 1; } else { nLoop += nLen; } } else { nMarkIndex = nLoop; } } } if (nIndex < nMarkIndex) { nIndex = nMarkIndex + 1; } if (nIndex < btAryBuffer.Length) { m_nLenth = btAryBuffer.Length - nIndex; Array.Clear(m_btAryBuffer, 0, 8192); Array.Copy(btAryBuffer, nIndex, m_btAryBuffer, 0, btAryBuffer.Length - nIndex); } else { m_nLenth = 0; } } catch (System.Exception ex) { } }