Exemplo n.º 1
0
        public void DeviceRecvData(byte [] recvData, int offset, int size)
        {
            _dataBuffer.DataIn(recvData, offset, size);

            // try to analysis first packet
            if (_dataBuffer.length >= 8)
            {
                byte[] header = _dataBuffer.DataPreOut();

                switch (header[0])
                {
                case 0x70:      // register read packet
                {
                    byte[] registerPacket = _dataBuffer.DataOut(8);

                    UInt16 add  = (UInt16)(registerPacket[3] << 8 | registerPacket[2]);
                    UInt32 data = (UInt16)(registerPacket[7] << 24 | registerPacket[6] << 16 | registerPacket[5] << 8 | registerPacket[4]);

                    _registerData[add] = data;
                }
                break;

                case 0x00:
                {
                    byte[] registerPacket = _dataBuffer.DataOut(8);

                    if (registerPacket[1] == 0x70)
                    {
                        //cycle end
                    }
                }
                break;

                case 0x01:      //
                {
                    byte[] CommandPacket = _dataBuffer.DataOut(8);
                }
                break;

                case 0x02:      //
                {
                    byte[] CommandPacket = _dataBuffer.DataOut(8);

                    if (CommandPacket[3] == 0x80)           // Command packet
                    {
                        _dataBuffer.DataOut(8);
                    }
                    else
                    {
                        break;
                    }
                    switch (CommandPacket[2])
                    {
                    case 0x00:                  // Command Begin
                        break;

                    case 0x01:                  // Command End
                        break;

                    default:                    // Error Code
                        break;
                    }
                }
                break;

                case 0x03:      // inventory
                {
                    byte[] tagPacketHeader = _dataBuffer.DataOut(8);

                    var pkt_ver    = tagPacketHeader[0];
                    var flags      = tagPacketHeader[1];
                    var pkt_type   = (UInt16)(tagPacketHeader[2] | (tagPacketHeader[3] << 8));
                    var pkt_len    = (UInt16)(tagPacketHeader[4] | (tagPacketHeader[5] << 8));
                    var extdatalen = (pkt_len) * 4 - ((flags >> 6) & 3);

                    switch (pkt_type)
                    {
                    case 0x8005:                /// inventory
                    {
                        int pktByteLen = pkt_len * 4;

                        if (_dataBuffer.length < pktByteLen)
                        {
                            _dataBuffer.Clear();
                            return;
                        }

                        byte[] tagPacketBody = _dataBuffer.DataOut(pktByteLen);

                        if (OnAsyncCallback != null)
                        {
                            CSLibrary.Events.TagCallbackInfo info = new CSLibrary.Events.TagCallbackInfo();
                            CSLibrary.Events.CallbackType    type = CSLibrary.Events.CallbackType.TAG_RANGING;

                            info.ms_ctr      = (UInt32)(tagPacketBody[0] | tagPacketBody[1] << 8 | tagPacketBody[2] << 16 | tagPacketBody[3] << 24);
                            info.rssi        = (Single)(tagPacketBody[5] * 0.8);
                            info.freqChannel = tagPacketBody[10];
                            info.antennaPort = (UInt16)(tagPacketBody[10] | tagPacketBody[11] << 8);
                            info.pc          = (UInt16)(tagPacketBody[12] << 8 | tagPacketBody[13]);
                            info.epcstrlen   = (UInt16)((((pkt_len - 3) * 4) - ((flags >> 6) & 3) - 4));
                            info.crc16       = (UInt16)(tagPacketBody[14 + info.epcstrlen] << 8 | tagPacketBody[15 + info.epcstrlen]);

                            byte[] byteEpc = new byte[info.epcstrlen];
                            Array.Copy(tagPacketBody, 14, byteEpc, 0, (int)info.epcstrlen);

                            info.epc = (ushort[])CSLibrary.Tools.Hex.ToUshorts(byteEpc).Clone();

                            CSLibrary.Events.OnAsyncCallbackEventArgs callBackData = new Events.OnAsyncCallbackEventArgs(info, type);
                            OnAsyncCallback(_deviceHandler, callBackData);
                        }
                    }
                    break;

                    default:                    // unknown packet
                        break;
                    }
                }
                break;

                default:        // skip invalid packet
                    _dataBuffer.Clear();
                    break;
                }
            }


            /*
             *          switch (_readerStatus)
             *          {
             *              case RFIDREADERCMDSTATUS.IDLE:
             *                  break;
             *
             *              case RFIDREADERCMDSTATUS.GETREGISTER:
             *                  if (_dataBuffer.length >= 8)
             *                  {
             *                      byte [] header = _dataBuffer.DataPreOut(4);
             *                      if (Array.Equals(header, new byte []{0x01, 0x02, 0x03, 0x04 }))
             *                      {
             *                          byte [] getRegiterPacket = _dataBuffer.DataOut(8);
             *                      }
             *                  }
             *                  _readerStatus = RFIDREADERCMDSTATUS.IDLE;
             *                  break;
             *
             *              case RFIDREADERCMDSTATUS.EXECCMD:  // Receive command begin packet
             *                  if (_dataBuffer.length >= 16)
             *                  {
             *
             *                  }
             *                  break;
             *
             *              case RFIDREADERCMDSTATUS.INVENTORY:  // Receive inventory packet
             *                  if (_dataBuffer.length >= 16)
             *                  {
             *
             *                  }
             *                  break;
             *
             *              case RFIDREADERCMDSTATUS.ABORT: // Receive bbort response
             *                  if (_dataBuffer.length >= 8)
             *                  {
             *                      byte[] packetData = _dataBuffer.DataPreOut(8);
             *                      if (Array.Equals(packetData, new byte[] { 0x43, 0x02, 0x03, 0x04 }))
             *                      {
             *                          _dataBuffer.Skip (8);
             *                      }
             *                  }
             *                  _readerStatus = RFIDREADERCMDSTATUS.IDLE;
             *                  break;
             *          }
             *
             */
        }
Exemplo n.º 2
0
        /// <summary>
        /// Transfer BT API packet to R2000 packet
        /// </summary>
        /// <param name="recvData"></param>
        /// <param name="offset"></param>
        /// <param name="size"></param>
        public void DeviceRecvData(byte[] recvData)
        {
            if (_dataBuffer.length == 0)
            {
                // first packet
                byte header = recvData[10];

                switch (header)
                {
                case 0x70:                          // register read packet
                {
                    UInt16 add  = (UInt16)(recvData[10 + 3] << 8 | recvData[10 + 2]);
                    UInt32 data = (UInt16)(recvData[10 + 7] << 24 | recvData[10 + 6] << 16 | recvData[10 + 5] << 8 | recvData[10 + 4]);

                    _registerData[add] = data;
                }
                break;

                case 0x00:
                {
                    if (recvData[8] == 0x70)
                    {
                        //cycle end
                    }
                }
                break;

                case 0x01:                          //
                {
                    if (recvData[8] == 0x70)
                    {
                        //cycle end
                    }
                }
                break;

                case 0x02:                          //
                {
                    if (recvData[10 + 3] == 0x80)   // Command packet
                    {
                    }
                    else
                    {
                        break;
                    }
                    switch (recvData[10 + 3])
                    {
                    case 0x00:                                                  // Command Begin
                        break;

                    case 0x01:                                                  // Command End
                        break;

                    default:                                                    // Error Code
                        break;
                    }
                }
                break;

                case 0x03:                          // inventory
                {
                    var pkt_ver    = recvData[10 + 0];
                    var flags      = recvData[10 + 1];
                    var pkt_type   = (UInt16)(recvData[10 + 2] | (recvData[10 + 3] << 8));
                    var pkt_len    = (UInt16)(recvData[10 + 4] | (recvData[10 + 5] << 8));
                    var extdatalen = (pkt_len) * 4 - ((flags >> 6) & 3);

                    switch (pkt_type)
                    {
                    case 0x8005:                                                /// inventory
                    {
                        if (OnAsyncCallback != null)
                        {
                            CSLibrary.Events.TagCallbackInfo info = new CSLibrary.Events.TagCallbackInfo();
                            CSLibrary.Events.CallbackType    type = CSLibrary.Events.CallbackType.TAG_RANGING;

                            info.ms_ctr      = (UInt32)(recvData[18 + 0] | recvData[18 + 1] << 8 | recvData[18 + 2] << 16 | recvData[18 + 3] << 24);
                            info.rssi        = (Single)(recvData[18 + 5] * 0.8);
                            info.freqChannel = recvData[18 + 10];
                            info.antennaPort = (UInt16)(recvData[18 + 10] | recvData[18 + 11] << 8);
                            info.pc          = (UInt16)(recvData[18 + 12] << 8 | recvData[18 + 13]);
                            info.epcstrlen   = (UInt16)((((pkt_len - 3) * 4) - ((flags >> 6) & 3) - 4));
                            info.crc16       = (UInt16)(recvData[18 + 14 + info.epcstrlen] << 8 | recvData[18 + 15 + info.epcstrlen]);

                            byte[] byteEpc = new byte[info.epcstrlen];
                            Array.Copy(recvData, 18 + 14, byteEpc, 0, (int)info.epcstrlen);

                            info.epc = (ushort[])CSLibrary.Tools.Hex.ToUshorts(byteEpc).Clone();

                            CSLibrary.Events.OnAsyncCallbackEventArgs callBackData = new Events.OnAsyncCallbackEventArgs(info, type);
                            OnAsyncCallback(_deviceHandler, callBackData);
                        }
                    }
                    break;

                    default:                                                    // unknown packet
                        break;
                    }
                }
                break;

                case 0x40:                          // Abort packet
                    break;

                case 0x34:                          // Unknow command
                default:                            // skip invalid packet
                    break;
                }
            }
            else
            {
                _dataBuffer.DataIn(recvData, 10, recvData[2]);
            }
        }