protected ENUM_CMD Regular( [In, Out] ref byte[] RxBuf, [In] bool bCheckTimeOut ) { ENUM_CMD result = ENUM_CMD.NOTHING; TRANS_DEV_TYPE enumDev = clsPacket.TRANS_API_AskDevType(); RxCmd = ENUM_CMD.NOTHING; //Check time out if (bCheckTimeOut == true) { TimeSpan ts = new TimeSpan(DateTime.Now.Ticks - m_timeBefReg.Ticks); if (ts.TotalMilliseconds > 1500) { #if _TRACE_OUT_PUT PrintMagToTxt("Regular: TimeOut"); #endif RxStep = ENMU_PKT_FLOW.STEP_HEADER_1; return ENUM_CMD.TIME_OUT; } } #if _TRACE_OUT_PUT PrintMagToTxt("Regular"); #endif UInt32 RevCount = clsPacket.TRANS_API_AskRevCount(); //USB receives one report at one time. Don't need to check every byte. if (enumDev == TRANS_DEV_TYPE.USB) { if ( 0 == clsPacket.TRANS_API_Read(RxBuf, RevCount) ) return result; //USB receives one report at one time. Don't need to find Pkt ID and Header. RxStep = ENMU_PKT_FLOW.STEP_HEADER_1; if (PutPktToData(enumDev, RxBuf) == ENMU_REV_STATE.REVEICE_DONE) { result = RxCmd; } return result; } for (int i = 0; i < RevCount; i++) { switch (RxStep) { case ENMU_PKT_FLOW.STEP_HEADER_1: RxCnt = 0; usDataSingleLenDw = 0; usPktSingleLenBt = 0; usDivideNum = 0; usDivideSeq = 0; usRxCRC = 0; RxCmd = ENUM_CMD.NOTHING; result = ENUM_CMD.NOTHING; Array.Clear(RxBuf, 0, RxBuf.Length); //RS232 need to check every byte. clsPacket.TRANS_API_Read(RxByte, 1); RxBuf[RxCnt] = RxByte[0]; //Check Header switch (RxByte[0]) { case (byte)'B': //Begin case (byte)'E': //End case (byte)'I': //Inventory case (byte)'A': //Tag Access case (byte)'R': //Common Response case (byte)'S': //Update Common Response case (byte)'F': //Update Enter Firmware configuration RxCnt++; RxStep++; break; default: break; } break; case ENMU_PKT_FLOW.STEP_HEADER_2: //Header //RS232 need to check every byte. clsPacket.TRANS_API_Read(RxByte, 1); RxBuf[RxCnt] = RxByte[0]; if (RxByte[0] == 'I') { RxCnt++; RxStep++; } else { RxStep = ENMU_PKT_FLOW.STEP_HEADER_1; } break; case ENMU_PKT_FLOW.STEP_HEADER_3: //Header //RS232 need to check every byte. clsPacket.TRANS_API_Read(RxByte, 1); RxBuf[RxCnt] = RxByte[0]; if (RxByte[0] == 'T') { RxCnt++; RxStep++; } else { RxStep = ENMU_PKT_FLOW.STEP_HEADER_1; } break; case ENMU_PKT_FLOW.STEP_HEADER_4: //Header //RS232 need to check every byte. clsPacket.TRANS_API_Read(RxByte, 1); RxBuf[RxCnt] = RxByte[0]; if (RxByte[0] == 'M') { RxCnt++; RxStep++; } else { RxStep = ENMU_PKT_FLOW.STEP_HEADER_1; } break; case ENMU_PKT_FLOW.STEP_HANDLE: if (PutPktToData(enumDev, RxBuf) == ENMU_REV_STATE.REVEICE_DONE) { result = RxCmd; return result; } break; default: RxStep = ENMU_PKT_FLOW.STEP_HEADER_1; break; }//end switch }//end for return result; }
//Reset receive flow protected void ResetRxFlow(ENMU_PKT_TYPE r_Type) { RxPktTpye = r_Type; RxStep = ENMU_PKT_FLOW.STEP_HEADER_1; m_timeBefReg = DateTime.Now; //Reset flag while restart tag access/inventory if (RxPktTpye == ENMU_PKT_TYPE.TAG_ACCESS) { bCheckTimeOut = false; enumINVENTORY_STATUS = ENUM_FLOW_CONTROL.NOTHING; } }
protected ENMU_REV_STATE PutPktToData( [In] TRANS_DEV_TYPE enumDevice, [In] byte[] RxBuf ) { int Length = 0; int CrcLen = 0; UInt16 usCRC = 0; #if _TRACE_OUT_PUT PrintMagToTxt("PutPktToData"); #endif try { //Get Per Packet size and remaining bytes switch (RxBuf[0]) { //Tag Access===================== case (byte)'B': //Begin case (byte)'E': //End RxPktTpye = ENMU_PKT_TYPE.TAG_ACCESS; usPktSingleLenBt = 24; Length = usPktSingleLenBt - (int)ENMU_TAG_PKT_INDEX.PKT_DEVIDE_NUM; break; case (byte)'I': //Inventory case (byte)'A': //Tag Access RxPktTpye = ENMU_PKT_TYPE.TAG_ACCESS; usPktSingleLenBt = 64; Length = usPktSingleLenBt - (int)ENMU_TAG_PKT_INDEX.PKT_DEVIDE_NUM; break; //Common========================== case (byte)'R': //Common RxPktTpye = ENMU_PKT_TYPE.COMMON; usPktSingleLenBt = 16; Length = (int)ENMU_PKT_INDEX.END - (int)ENMU_PKT_INDEX.PKT_DEVIDE_ID; break; //Update========================== case (byte)'F': //Update firmware mode RxPktTpye = ENMU_PKT_TYPE.UPDATE_ENTER; usPktSingleLenBt = 24; Length = usPktSingleLenBt - (int)ENMU_TAG_PKT_INDEX.PKT_DEVIDE_NUM; break; case (byte)'S': //Update Common RxPktTpye = ENMU_PKT_TYPE.UPDATE_COMMON; usPktSingleLenBt = 64; Length = usPktSingleLenBt - (int)ENMU_UPDATE_INDEX.PKT_DEVIDE_ID; break; default: RxStep = ENMU_PKT_FLOW.STEP_HEADER_1; return ENMU_REV_STATE.RECEIVE_FAULT; break; } //Receive data if (enumDevice == TRANS_DEV_TYPE.SERIAL) { //Data is not ready. Break and wait for next loop to get. if (clsPacket.TRANS_API_AskRevCount() < Length) { System.Threading.Thread.Sleep(5); return ENMU_REV_STATE.WAIT_DATA; } byte[] tmp = new byte[Length]; Array.Clear(tmp, 0, tmp.Length); //Receive data fail. if (Length != clsPacket.TRANS_API_Read(tmp, (uint)Length)) { RxStep = ENMU_PKT_FLOW.STEP_HEADER_1; return ENMU_REV_STATE.RECEIVE_FAULT; } Array.Copy(tmp, 0, RxBuf, RxCnt, tmp.Length); } RxCnt += Length; RxStep = ENMU_PKT_FLOW.STEP_HEADER_1; //Handle Data switch (RxPktTpye) { case ENMU_PKT_TYPE.COMMON: { RxCmd = (ENUM_CMD)RxBuf[(int)ENMU_PKT_INDEX.PKT_COMMAND_ID]; usRxCRC = BitConverter.ToUInt16(RxBuf, (int)ENMU_PKT_INDEX.PKT_CRC); CrcLen = (int)(ENMU_PKT_INDEX.PKT_CRC - ENMU_PKT_INDEX.PKT_HEADER); break; } case ENMU_PKT_TYPE.TAG_ACCESS: { usDivideNum = RxBuf[(int)ENMU_TAG_PKT_INDEX.PKT_DEVIDE_NUM]; usDivideSeq = RxBuf[(int)ENMU_TAG_PKT_INDEX.PKT_DEVIDE_SEQ]; RxCmd = (ENUM_CMD)(BitConverter.ToInt16 (RxBuf, (int)ENMU_TAG_PKT_INDEX.INFO_TYPE) | 0xF0); /* Mod by james for large user memory, 2012-12-04 */ if (usDivideSeq == 1) { usDataSingleLenDw = BitConverter.ToUInt16(RxBuf, (int)ENMU_TAG_PKT_INDEX.INFO_LENGTH); } else { if (usDivideNum - usDivideSeq == 0) { usDataSingleLenDw = (UInt16)(BitConverter.ToUInt16(RxBuf, (int)ENMU_TAG_PKT_INDEX.INFO_LENGTH) - 12 * (usDivideNum - 1)); } else { usDataSingleLenDw = 12; } } /* End by james for large user memory, 2012-12-04 */ usRxCRC = BitConverter.ToUInt16(RxBuf, usPktSingleLenBt - 2); CrcLen = (ushort)(usPktSingleLenBt - 2); break; } case ENMU_PKT_TYPE.UPDATE_ENTER: { usDivideNum = RxBuf[(int)ENMU_TAG_PKT_INDEX.PKT_DEVIDE_NUM]; usDivideSeq = RxBuf[(int)ENMU_TAG_PKT_INDEX.PKT_DEVIDE_SEQ]; //It uses TAG_ACCESS Packet, but reture 0x300B. Redefine to ENUM_CMD. RxCmd = ENUM_CMD.UPDAUE_ENTER_UPDATE_MODE; usDataSingleLenDw = BitConverter.ToUInt16 (RxBuf, (int)ENMU_TAG_PKT_INDEX.INFO_LENGTH); usRxCRC = BitConverter.ToUInt16(RxBuf, usPktSingleLenBt - 2); CrcLen = (ushort)(usPktSingleLenBt - 2); break; } case ENMU_PKT_TYPE.UPDATE_COMMON: { RxCmd = (ENUM_CMD)(RxBuf[(int)ENMU_UPDATE_INDEX.PKT_DATA_RECMD] | 0xE0); usRxCRC = BitConverter.ToUInt16(RxBuf, usPktSingleLenBt - 2); CrcLen = (ushort)(usPktSingleLenBt - 2); break; } default: break; } //Check CRC. CRC Legnth doesn't include CRC(2 bytes) usCRC = CrcCul(RxBuf, 0, (ushort)(CrcLen * 8)); //8 bits usCRC = (UInt16)(~usCRC & 0xFFFF); } catch (Exception e) { #if _TRACE_OUT_PUT this.PrintMagToTxt("Exception Error : PutPktToData"); #endif clsPacket.TRANS_API_ClearBuffer(); return ENMU_REV_STATE.RECEIVE_FAULT; } if (usCRC == usRxCRC) { return ENMU_REV_STATE.REVEICE_DONE; } else { return ENMU_REV_STATE.RECEIVE_FAULT; } }