private void ProcessSetWorkAntenna(PR9200Msg msgTran) { //int intCurrentAnt = 0; //intCurrentAnt = PR9200Setting.btWorkAntenna + 1; //string strCmd = "设置工作天线成功,当前工作天线: 天线" + intCurrentAnt.ToString(); //string strErrorCode = string.Empty; //if (msgTran.AryData.Length == 1) //{ // if (msgTran.AryData[0] == ErrorCode.CommandSuccess) // { // PR9200Setting.btReadId = msgTran.ReadId; // ////WriteLog(lrtxtLog, strCmd, 0); // ////校验是否盘存操作 // //if (m_bInventory) // //{ // // RunLoopInventroy(); // //} // return; // } // else // { // strErrorCode = CCommondMethod.FormatErrorCode(msgTran.AryData[0]); // } //} //else //{ // strErrorCode = "未知错误"; //} //string strLog = strCmd + "失败,失败原因: " + strErrorCode; }
private void PacketReceived_WriteTag(PR9200Msg msgTran) { /* * after write tag, should check if the tag is the same tag as read before */ string strErrorCode = string.Empty; if (msgTran.AryData.Length == 1) { strErrorCode = CCommondMethod.FormatErrorCode(msgTran.AryData[0]); string strLog = "读标签失败,失败原因: " + strErrorCode; SetErrorCode(strLog); dlgt_TagIdentifyFail(ActionResault.ReadTIDBankWhenWriteFail); } else { /* response packet byte map * * TagCount DataLen Data ErrorCode AndID WriteCount * 2 byte 1byte Nbyte 1byte 1byte 1byte * */ int nLen = msgTran.AryData.Length; int nDataLen = Convert.ToInt32(msgTran.AryData[2]);//lenght of useful data of a tag(PC+EPC+CRC) int nEpcLen = nDataLen - 4; string strPC = CCommondMethod.ByteArrayToString(msgTran.AryData, 3, 2); string strEPC = CCommondMethod.ByteArrayToString(msgTran.AryData, 5, nEpcLen); string strCRC = CCommondMethod.ByteArrayToString(msgTran.AryData, 5 + nEpcLen, 2); strErrorCode = CCommondMethod.FormatErrorCode(msgTran.AryData[nLen - 3]); byte byTemp = msgTran.AryData[nLen - 2]; byte byAntId = (byte)((byTemp & 0x03) + 1); string strAntId = byAntId.ToString(); string writeCount = msgTran.AryData[nLen - 1].ToString();//the last byte contains write count, according to application note /* * check these things: * 1: writecount should be 1; * 2: error code should be success * 3: epc should equals the one read before * * after all check passed, retun ok */ } }
//private void ProcessReadTag(UHFReader.PR9200Msg msgTran) //{ // string strCmd = "读标签"; // string strErrorCode = string.Empty; // if (msgTran.AryData.Length == 1) // { // strErrorCode = CCommondMethod.FormatErrorCode(msgTran.AryData[0]); // string strLog = strCmd + "失败,失败原因: " + strErrorCode; // //WriteLog(lrtxtLog, strLog, 1); // } // else // { // int nLen = msgTran.AryData.Length; // int nDataLen = Convert.ToInt32(msgTran.AryData[nLen - 3]);//lenght of the data we specified // int nEpcLen = Convert.ToInt32(msgTran.AryData[2]) - nDataLen - 4;//totally data length minus specified data length minus PC&CRC(4 bytes) // string strPC = CCommondMethod.ByteArrayToString(msgTran.AryData, 3, 2); // string strEPC = CCommondMethod.ByteArrayToString(msgTran.AryData, 5, nEpcLen); // string strCRC = CCommondMethod.ByteArrayToString(msgTran.AryData, 5 + nEpcLen, 2); // string strData = CCommondMethod.ByteArrayToString(msgTran.AryData, 7 + nEpcLen, nDataLen);//the specified data // byte byTemp = msgTran.AryData[nLen - 2]; // byte byAntId = (byte)((byTemp & 0x03) + 1); // string strAntId = byAntId.ToString(); // string strReadCount = msgTran.AryData[nLen - 1].ToString(); // if (action_Queue.Count > 0) // { // string sUniqueTID = ""; // bool b_tagValid = true; // switch (action_Queue.Peek()) // { // case ActionType.TagIdentify: // #region MyRegion // m_sTIDType = strData.Substring(0, 11); // sUniqueTID = strData.Substring(12); // if (m_sTIDType.Equals(UHFTIDTypes.Alien_Higgs3)) // { // WriteLog(lrtxtLog, "this is alien higgs 3 chip", 1); // } // else if (m_sTIDType.Equals(UHFTIDTypes.Monza_4D)) // { // WriteLog(lrtxtLog, "this is monza 4d chip", 1); // } // else if (m_sTIDType.Equals(UHFTIDTypes.Monza_4E)) // { // WriteLog(lrtxtLog, "this is monza 4e chip", 1); // } // else if (m_sTIDType.Equals(UHFTIDTypes.Monza_4i)) // { // WriteLog(lrtxtLog, "this is monza 4i chip", 1); // } // else if (m_sTIDType.Equals(UHFTIDTypes.Monza_4QT)) // { // WriteLog(lrtxtLog, "this is monza 4qtchip", 1); // } // else // { // WriteLog(lrtxtLog, "this tag is not valid", 1); // b_tagValid = false; // } // if (b_tagValid) // { // m_sCurrentUniqueTID = sUniqueTID; // GetTIDCallback(); // } // else // { // //tbx_SerialWrite.Enabled = true; EnableControl(1); // EnableControl(1); // } // #endregion // break; // case ActionType.ReadEPC: // WriteLog(lrtxtLog, strData, 0); // break; // case ActionType.ReadTIDBankWhenRead: // #region MyRegion // m_sTIDType = strData.Substring(0, 11); // sUniqueTID = strData.Substring(12); // if (m_sTIDType.Equals(UHFTIDTypes.Alien_Higgs3)) // { // m_userMemorySizeInWord = UHFUserMemorySizeInWord.A_9662; // WriteLog(lrtxtLog, "this is alien higgs 3 chip", 1); // } // else if (m_sTIDType.Equals(UHFTIDTypes.Monza_4D)) // { // m_userMemorySizeInWord = UHFUserMemorySizeInWord.Monza_4D; // WriteLog(lrtxtLog, "this is monza 4d chip", 1); // } // else if (m_sTIDType.Equals(UHFTIDTypes.Monza_4E)) // { // m_userMemorySizeInWord = UHFUserMemorySizeInWord.Monza_4E; // WriteLog(lrtxtLog, "this is monza 4e chip", 1); // } // else if (m_sTIDType.Equals(UHFTIDTypes.Monza_4i)) // { // m_userMemorySizeInWord = UHFUserMemorySizeInWord.Monza_4i; // WriteLog(lrtxtLog, "this is monza 4i chip", 1); // } // else if (m_sTIDType.Equals(UHFTIDTypes.Monza_4QT)) // { // m_userMemorySizeInWord = UHFUserMemorySizeInWord.Monza_4QT; // WriteLog(lrtxtLog, "this is monza 4qtchip", 1); // } // else // { // m_userMemorySizeInWord = 0x00; // WriteLog(lrtxtLog, "this tag is not valid", 1); // b_tagValid = false; // } // if (b_tagValid) // { // m_sCurrentUniqueTID = sUniqueTID; // ReadUserBank(); // } // else // { // //tbx_readSerial.Enabled = true; // EnableControl(2); // } // #endregion // break; // case ActionType.ReadUserBank: // byte[] userdata = new byte[nDataLen]; // msgTran.AryData.CopyTo(userdata, 7 + nEpcLen); // UserBankDataInterperater(userdata); // break; // default: // break; // } // action_Queue.Dequeue(); // } // } //} private void PacketReceived_GetFrequencyRegion(PR9200Msg msgTran) { string strCmd = "取得射频规范"; string strErrorCode = string.Empty; if (msgTran.AryData.Length == 3) {//system frequencies used PR9200Setting.btReadId = msgTran.ReadId; PR9200Setting.btRegion = msgTran.AryData[0]; PR9200Setting.btFrequencyStart = msgTran.AryData[1]; PR9200Setting.btFrequencyEnd = msgTran.AryData[2]; dlgt_GetFrequencyRegion(ActionResault.GetFrequencyRegionSuccess); //WriteLog(lrtxtLog, strCmd, 0); return; } else if (msgTran.AryData.Length == 6) {//user defined frequencies used PR9200Setting.btReadId = msgTran.ReadId; PR9200Setting.btRegion = msgTran.AryData[0]; PR9200Setting.btUserDefineFrequencyInterval = msgTran.AryData[1]; PR9200Setting.btUserDefineChannelQuantity = msgTran.AryData[2]; PR9200Setting.nUserDefineStartFrequency = msgTran.AryData[3] * 256 * 256 + msgTran.AryData[4] * 256 + msgTran.AryData[5]; dlgt_GetFrequencyRegion(ActionResault.GetFrequencyRegionSuccess); //WriteLog(lrtxtLog, strCmd, 0); return; } else if (msgTran.AryData.Length == 1) { strErrorCode = CCommondMethod.FormatErrorCode(msgTran.AryData[0]); } else { strErrorCode = "未知错误"; } string strLog = strCmd + "失败,失败原因: " + strErrorCode; //WriteLog(lrtxtLog, strLog, 1); SetErrorCode(strLog); dlgt_GetFrequencyRegion(ActionResault.GetFrequencyRegionFail); }
private void RunReceiveDataCallback(byte[] btAryReceiveData) { try { if (ReceiveCallback != null) { ReceiveCallback(btAryReceiveData); } 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); //分析接收数据,以0xA0为数据起点,以协议中数据长度为数据终止点 int nIndex = 0; //当数据中存在A0时,记录数据的终止点 int nMarkIndex = 0; //当数据中不存在A0时,nMarkIndex等于数据组最大索引 for (int nLoop = 0; nLoop < btAryBuffer.Length; nLoop++) { if (btAryBuffer.Length > nLoop + 1) { if (btAryBuffer[nLoop] == 0xA0) { int nLen = Convert.ToInt32(btAryBuffer[nLoop + 1]); if (nLoop + 1 + nLen < btAryBuffer.Length) { byte[] btAryAnaly = new byte[nLen + 2]; Array.Copy(btAryBuffer, nLoop, btAryAnaly, 0, nLen + 2); PR9200Msg msgTran = new PR9200Msg(btAryAnaly); if (AnalyCallback != null) { AnalyCallback(msgTran); } nLoop += 1 + nLen; nIndex = nLoop + 1; } else { nLoop += 1 + nLen; } } else { nMarkIndex = nLoop; } } } if (nIndex < nMarkIndex) { nIndex = nMarkIndex + 1; } if (nIndex < btAryBuffer.Length) { m_nLenth = btAryBuffer.Length - nIndex; Array.Clear(m_btAryBuffer, 0, 4096); Array.Copy(btAryBuffer, nIndex, m_btAryBuffer, 0, btAryBuffer.Length - nIndex); } else { m_nLenth = 0; } } catch (System.Exception ex) { } }
public byte CheckValue(byte[] btAryData) { PR9200Msg msgTran = new PR9200Msg(); return(msgTran.CheckSum(btAryData, 0, btAryData.Length)); }
private int SendMessage(byte btReadId, byte btCmd, byte[] btAryData) { PR9200Msg msgTran = new PR9200Msg(btReadId, btCmd, btAryData); return(SendMessage(msgTran.AryTranData)); }
//private void TagIdentify(PR9200Msg msgTran) //{ // string strErrorCode = string.Empty; // if (msgTran.AryData.Length == 1) // { // strErrorCode = CCommondMethod.FormatErrorCode(msgTran.AryData[0]); // string strLog = "读标签失败,失败原因: " + strErrorCode; // SetErrorCode(strLog); // readTIDBankWhenWriteFailDelegate(ActionResault.ReadTIDBankWhenWriteFail); // } // else // { // int nLen = msgTran.AryData.Length; // int nDataLen = Convert.ToInt32(msgTran.AryData[nLen - 3]);//lenght of the data we specified // int nEpcLen = Convert.ToInt32(msgTran.AryData[2]) - nDataLen - 4;//totally data length minus specified data length minus PC&CRC(4 bytes) // string strPC = CCommondMethod.ByteArrayToString(msgTran.AryData, 3, 2); // string strEPC = CCommondMethod.ByteArrayToString(msgTran.AryData, 5, nEpcLen); // string strCRC = CCommondMethod.ByteArrayToString(msgTran.AryData, 5 + nEpcLen, 2); // string strData = CCommondMethod.ByteArrayToString(msgTran.AryData, 7 + nEpcLen, nDataLen);//the specified data // byte byTemp = msgTran.AryData[nLen - 2]; // byte byAntId = (byte)((byTemp & 0x03) + 1); // string strAntId = byAntId.ToString(); // string strReadCount = msgTran.AryData[nLen - 1].ToString(); // string sUniqueTID = ""; // bool b_tagValid = true; // m_sTIDType = strData.Substring(0, 11); // sUniqueTID = strData.Substring(12); // if (m_sTIDType.Equals(UHFTIDTypes.Alien_Higgs3)) // { // //WriteLog(lrtxtLog, "this is alien higgs 3 chip", 1); // } // else if (m_sTIDType.Equals(UHFTIDTypes.Monza_4D)) // { // //WriteLog(lrtxtLog, "this is monza 4d chip", 1); // } // else if (m_sTIDType.Equals(UHFTIDTypes.Monza_4E)) // { // //WriteLog(lrtxtLog, "this is monza 4e chip", 1); // } // else if (m_sTIDType.Equals(UHFTIDTypes.Monza_4i)) // { // //WriteLog(lrtxtLog, "this is monza 4i chip", 1); // } // else if (m_sTIDType.Equals(UHFTIDTypes.Monza_4QT)) // { // //WriteLog(lrtxtLog, "this is monza 4qtchip", 1); // } // else // { // string err = "标签类型未知"; // SetErrorCode(err); // readTIDBankWhenWriteFailDelegate(ActionResault.ReadTIDBankWhenWriteFail); // //WriteLog(lrtxtLog, "this tag is not valid", 1); // b_tagValid = false; // } // if (b_tagValid) // { // tagUid = sUniqueTID; // byte btMemBank = MemoryBank.USERBank; // byte btWordAdd = 0x00; // byte btWordCnt = 0x00; // byte[] btAryPwd = new byte[] { 0x00, 0x00, 0x00, 0x00 }; // //byte[] btAryData = Encoding.ASCII.GetBytes(data); // Int16 dataLen = Convert.ToInt16(btWriteDataBuffer); // byte[] btLen = BitConverter.GetBytes(dataLen); // byte[] btAryDataAndLen = new byte[btWriteDataBuffer.Length + 2]; // btLen.CopyTo(btAryDataAndLen, 0); //the first 2 bytes contains data length // btWriteDataBuffer.CopyTo(btAryDataAndLen, 2); // btWordCnt = Convert.ToByte(btAryDataAndLen.Length / 2 + btAryDataAndLen.Length % 2);//1 word equals 2 bytes // /* // * finnaly, we got the data to be write // */ // actionType = ActionType.WriteTag; // WriteTag(PR9200Setting.btReadId, btAryPwd, btMemBank, btWordAdd, btWordCnt, btAryDataAndLen); // } // } //} private void PacketReceived_TagIdentify(PR9200Msg msgTran) { string strErrorCode = string.Empty; if (msgTran.AryData.Length == 1) { strErrorCode = CCommondMethod.FormatErrorCode(msgTran.AryData[0]); string strLog = "读标签失败,失败原因: " + strErrorCode; SetErrorCode(strLog); dlgt_TagIdentifyFail(ActionResault.ReadTIDBankWhenWriteFail); } else { int nLen = msgTran.AryData.Length; int nDataLen = Convert.ToInt32(msgTran.AryData[nLen - 3]); //lenght of the data we specified, stored in the third byte from last, according to application note int nEpcLen = Convert.ToInt32(msgTran.AryData[2]) - nDataLen - 4; //totally data length minus specified data length minus PC&CRC(4 bytes) string strPC = CCommondMethod.ByteArrayToString(msgTran.AryData, 3, 2); string strEPC = CCommondMethod.ByteArrayToString(msgTran.AryData, 5, nEpcLen); string strCRC = CCommondMethod.ByteArrayToString(msgTran.AryData, 5 + nEpcLen, 2); string strData = CCommondMethod.ByteArrayToString(msgTran.AryData, 7 + nEpcLen, nDataLen);//the specified data byte byTemp = msgTran.AryData[nLen - 2]; byte byAntId = (byte)((byTemp & 0x03) + 1); string strAntId = byAntId.ToString(); string strReadCount = msgTran.AryData[nLen - 1].ToString(); string sUniqueTID = ""; bool b_tagValid = true; m_sTIDType = strData.Substring(0, 11); sUniqueTID = strData.Substring(12); if (m_sTIDType.Equals(UHFTIDTypes.Alien_Higgs3)) { //WriteLog(lrtxtLog, "this is alien higgs 3 chip", 1); } else if (m_sTIDType.Equals(UHFTIDTypes.Monza_4D)) { //WriteLog(lrtxtLog, "this is monza 4d chip", 1); } else if (m_sTIDType.Equals(UHFTIDTypes.Monza_4E)) { //WriteLog(lrtxtLog, "this is monza 4e chip", 1); } else if (m_sTIDType.Equals(UHFTIDTypes.Monza_4i)) { //WriteLog(lrtxtLog, "this is monza 4i chip", 1); } else if (m_sTIDType.Equals(UHFTIDTypes.Monza_4QT)) { //WriteLog(lrtxtLog, "this is monza 4qtchip", 1); } else { string err = "标签类型未知"; SetErrorCode(err); dlgt_TagIdentifyFail(ActionResault.ReadTIDBankWhenWriteFail); //WriteLog(lrtxtLog, "this tag is not valid", 1); b_tagValid = false; } if (b_tagValid) { tagUid = sUniqueTID; byte btMemBank = MemoryBank.USERBank; byte btWordAdd = 0x00; byte btWordCnt = 0x00; byte[] btAryPwd = new byte[] { 0x00, 0x00, 0x00, 0x00 }; //byte[] btAryData = Encoding.ASCII.GetBytes(data); Int16 dataLen = Convert.ToInt16(btWriteDataBuffer); byte[] btLen = BitConverter.GetBytes(dataLen); byte[] btAryDataAndLen = new byte[btWriteDataBuffer.Length + 2]; btLen.CopyTo(btAryDataAndLen, 0); //the first 2 bytes contains data length btWriteDataBuffer.CopyTo(btAryDataAndLen, 2); btWordCnt = Convert.ToByte(btAryDataAndLen.Length / 2 + btAryDataAndLen.Length % 2);//1 word equals 2 bytes /* * finnaly, we got the data to be write, go to step 2: writing data to tag */ actionType = ActionType.WriteTag; WriteTag(PR9200Setting.btReadId, btAryPwd, btMemBank, btWordAdd, btWordCnt, btAryDataAndLen); } } }
private void ParseResponsePacket(byte[] btAryReceiveData) { #region build response packets List <PR9200Msg> lstResponsePacket = new List <PR9200Msg>(); for (int i = 0; i < btAryReceiveData.Length; i++) { if (btAryReceiveData.Length > i + 1) { if (btAryReceiveData[i] == 0xA0) { int nLen = Convert.ToInt32(btAryReceiveData[i + 1]); if (i + 1 + nLen < btAryReceiveData.Length) { byte[] btAryAnaly = new byte[nLen + 2]; Array.Copy(btAryReceiveData, i, btAryAnaly, 0, nLen + 2); lstResponsePacket.Add(new PR9200Msg(btAryAnaly)); i += 1 + nLen; } else { i += 1 + nLen; } } } } #endregion #region dispatch response packet to related functions switch (actionType) { case ActionType.None: break; case ActionType.GetFrequencyRegion: if (lstResponsePacket.Count == 1) { ProcessGetFrequencyRegion(lstResponsePacket[0]); } else { SetErrorCode("返回数据包格式不正确,请重试!"); getFrequencyRegionCallback(ActionResault.GetFrequencyRegionFail); } break; case ActionType.ReadTIDBankWhenWrite: if (lstResponsePacket.Count == 1) { ProcessGetFrequencyRegion(lstResponsePacket[0]); } else if (lstResponsePacket.Count == 0) { SetErrorCode("未找到标签,请重试!"); readTIDBankWhenWriteFailDelegate(ActionResault.ReadTIDBankWhenWriteFail); } else if (lstResponsePacket.Count == 0) { SetErrorCode("找到多个标签,请将多余标签拿走!"); readTIDBankWhenWriteFailDelegate(ActionResault.ReadTIDBankWhenWriteFail); } break; case ActionType.ReadEPC: break; case ActionType.ReadTIDBankWhenRead: break; case ActionType.ReadUserBank: break; case ActionType.WriteTag: break; default: break; } #endregion if (lstResponsePacket.Count == 0) { } else if (lstResponsePacket.Count > 1) { } else { /* * just make sure only one tag been writen */ PR9200Msg msgTran = lstResponsePacket[0]; if (msgTran.PacketType != 0xA0) { return; } switch (msgTran.Cmd) { case 0x81: ProcessReadTag(msgTran); break; case 0x82: ProcessWriteTag(msgTran); break; default: break; } } }