예제 #1
0
        public bool ReadEpcInfo()
        {
            bool blRet = false;

            try
            {
                byte   MaskMem  = 0;
                byte[] MaskAdr  = new byte[2];
                byte   MaskLen  = 0;
                byte[] MaskData = new byte[100];
                byte   MaskFlag = 0;
                byte   AdrTID   = 0;
                byte   LenTID   = 0;
                byte   TIDFlag  = 0;
                byte   Ant      = 0;

                blScan = true;
                int    CardCount = 0;              //输出变量,电子标签的张数
                int    EpcLen    = 0;              //EPC 的字节数
                byte[] bEpcArray = new byte[5000]; //指向输出数组变量 是读到的电子标签的EPC数据,是一张标签的EPC长度+一张标签的EPC号,依此累加


                //int fCmdRet = StaticClassReaderB.Inventory_G2(ref fComAdr, MaskMem, MaskLen, MaskFlag);
                //if ((fCmdRet==0)|| (fCmdRet == 1) || (fCmdRet == 2) || (fCmdRet == 3) || (fCmdRet == 4) || (fCmdRet == 0xFB))//代表已查找结束,
                //{ }
                //else
                //{
                //    iReadEpcErr++;
                //    blRet = false;
                //}

                int fCmdRet = StaticClassReaderB.Inventory_G2(ref fComAdr, MaskMem, MaskAdr, MaskLen, MaskData, MaskFlag, AdrTID, LenTID, TIDFlag, bEpcArray, ref Ant, ref EpcLen, ref CardCount, frmcomportindex); // 8616
                if ((fCmdRet == 1) || (fCmdRet == 2) || (fCmdRet == 3) || (fCmdRet == 4) || (fCmdRet == 0xFB))                                                                                                      //代表已查找结束,
                {
                    iReadEpcErr = 0;
                    blRet       = true;
                    if (CardCount == 0)
                    {
                        blScan = false;
                        return(blRet);
                    }
                    byte[] bEpcCopy = new byte[EpcLen];
                    Array.Copy(bEpcArray, bEpcCopy, EpcLen);
                    string strEpcAll = SerialPortUtil.ByteToStr(bEpcCopy);

                    int iEpcIndex = 0;//单张EPC的长度在bEpcCopy中的位置
                    for (int i = 0; i < CardCount; i++)
                    {
                        int    iEpcLen = bEpcCopy[iEpcIndex];                                 //epc的长度
                        string strEpc  = strEpcAll.Substring(iEpcIndex * 2 + 2, iEpcLen * 2); //得到一个EPC数据
                        iEpcIndex = iEpcIndex + iEpcLen + 1;                                  //下一个EPC标签的 标签长度指示位置

                        if (strEpc.Length != 12)
                        {
                            break;
                        }
                        bool blHas = false;
                        if (dicEpcInfo.ContainsKey(strEpc))
                        {//存在标签信息
                            #region 顺序读取
                            if (usingReadNo == DeviceUsing.启用)
                            {
                                if (dicEpcInfo[strEpc].IntAnt != Ant)
                                {         //查询到标签的天线和原来的不一样
                                    if ((dicEpcInfo[strEpc].IntAnt & 0x01) == 0x01 || (dicEpcInfo[strEpc].IntAnt & 0x02) == 0x02)
                                    {     //归还 原来是1# 2#天线扫描到
                                        if ((Ant & 0x04) == 0x04 || (Ant & 0x08) == 0x08)
                                        { //现在是3# 4#天线扫描到
                                            dicEpcInfo[strEpc].BorrOrRet   = ToolsBorrRet.归还;
                                            dicEpcInfo[strEpc].DtReadedEpc = DateTime.Now;
                                            RfidManage.QEpcInfo.Enqueue(dicEpcInfo[strEpc]);
                                        }
                                    }
                                    else
                                    {         //借出
                                        if ((dicEpcInfo[strEpc].IntAnt & 0x04) == 0x04 || (dicEpcInfo[strEpc].IntAnt & 0x08) == 0x08)
                                        {     //原来是3# 4#天线扫描到
                                            if ((Ant & 0x01) == 0x01 || (Ant & 0x02) == 0x02)
                                            { //现在是1# 2#天线扫描到
                                                dicEpcInfo[strEpc].BorrOrRet   = ToolsBorrRet.借出;
                                                dicEpcInfo[strEpc].DtReadedEpc = DateTime.Now;
                                                RfidManage.QEpcInfo.Enqueue(dicEpcInfo[strEpc]);
                                            }
                                        }
                                    }
                                    dicEpcInfo[strEpc].IntAnt = Ant;
                                }
                                dicEpcInfo[strEpc].UsingReadNo = DeviceUsing.启用;
                            }
                            blHas = true;
                            #endregion
                        }
                        else
                        {
                            EpcInfo ei = new EpcInfo(strEpc, Ant);
                            ei.TimeLastRead = DateTime.Now;
                            if (usingReadNo == DeviceUsing.启用)
                            {
                                ei.UsingReadNo = DeviceUsing.启用;
                            }
                            ei.BorrOrRet = ToolsBorrRet.未知;
                            ei.IsEpcRead = IsReadShow.未读;
                            dicEpcInfo.Add(strEpc, ei);
                        }
                    }
                }
                else
                {
                    iReadEpcErr++;
                    blRet = false;
                }
                blScan = false;
            }
            catch (Exception ee)
            {
                if (frmMain.blDebug)
                {
                    MessageUtil.ShowTips(ee.Message);
                }
            }
            return(blRet);
        }
예제 #2
0
        public bool ReadEpc()
        {
            bool blRet = false;

            try
            {
                byte   MaskMem  = 0;
                byte[] MaskAdr  = new byte[2];
                byte   MaskLen  = 0;
                byte[] MaskData = new byte[100];
                byte   MaskFlag = 0;
                byte   AdrTID   = 0;
                byte   LenTID   = 0;
                byte   TIDFlag  = 0;
                byte   Ant      = 0;

                blScan = true;
                int    CardCount = 0;                                                                                                                                                                               //输出变量,电子标签的张数
                int    EpcLen    = 0;                                                                                                                                                                               //EPC 的字节数
                byte[] bEpcArray = new byte[5000];                                                                                                                                                                  //指向输出数组变量 是读到的电子标签的EPC数据,是一张标签的EPC长度+一张标签的EPC号,依此累加

                int fCmdRet = StaticClassReaderB.Inventory_G2(ref fComAdr, MaskMem, MaskAdr, MaskLen, MaskData, MaskFlag, AdrTID, LenTID, TIDFlag, bEpcArray, ref Ant, ref EpcLen, ref CardCount, frmcomportindex); // 8616
                if ((fCmdRet == 1) || (fCmdRet == 2) || (fCmdRet == 3) || (fCmdRet == 4) || (fCmdRet == 0xFB))                                                                                                      //代表已查找结束,
                {
                    blRet = true;
                    if (CardCount == 0)
                    {
                        blScan = false;
                        return(blRet);
                    }
                    byte[] bEpcCopy = new byte[EpcLen];
                    Array.Copy(bEpcArray, bEpcCopy, EpcLen);
                    string strEpcAll = SerialPortUtil.ByteToStr(bEpcCopy);

                    int iEpcIndex = 0;//单张EPC的长度在bEpcCopy中的位置
                    for (int i = 0; i < CardCount; i++)
                    {
                        int    iEpcLen = bEpcCopy[iEpcIndex];                                 //epc的长度
                        string strEpc  = strEpcAll.Substring(iEpcIndex * 2 + 2, iEpcLen * 2); //得到一个EPC数据
                        iEpcIndex = iEpcIndex + iEpcLen + 1;                                  //下一个EPC标签的 标签长度指示位置

                        if (strEpc.Length != 12)
                        {
                            break;
                        }
                        bool blHas = false;
                        for (int iIndex = 0; iIndex < listEpc.Count; iIndex++)
                        {
                            if (listEpc[iIndex].StrEpc == strEpc)
                            {
                                #region  顺序读取

                                if (usingReadNo == DeviceUsing.启用)
                                {
                                    if (listEpc[iIndex].IntAnt != Ant)
                                    {
                                        //归还
                                        if (((listEpc[iIndex].IntAnt & 0x01) == 0x01 || (listEpc[iIndex].IntAnt & 0x02) == 0x02))
                                        //if (((listEpc[iIndex].IntAnt & 0x01) == 0x01 || (listEpc[iIndex].IntAnt & 0x02) == 0x02) &&
                                        //    ((listEpc[iIndex].IntAnt & 0x04) != 0x04 && (listEpc[iIndex].IntAnt & 0x08) != 0x08))
                                        {
                                            if ((Ant & 0x4) == 0x04 || (Ant & 0x8) == 0x08)
                                            {
                                                //TimeSpan span = DateTime.Now - listEpc[iIndex].TimeLastRead;
                                                //if (span.TotalSeconds < 5)
                                                //{
                                                //    lock (listEpc)
                                                //    {
                                                //        listEpc[iIndex].BorrOrRet = ToolsBorrRet.归还;
                                                //    }
                                                //}

                                                lock (listEpc)
                                                {
                                                    listEpc[iIndex].BorrOrRet = ToolsBorrRet.归还;
                                                }
                                            }
                                        }
                                        else if (((listEpc[iIndex].IntAnt & 0x04) == 0x04 || (listEpc[iIndex].IntAnt & 0x08) == 0x08))
                                        //else if (((listEpc[iIndex].IntAnt & 0x04) == 0x04 || (listEpc[iIndex].IntAnt & 0x08) == 0x08) &&
                                        //  ((listEpc[iIndex].IntAnt & 0x01) != 0x01 && (listEpc[iIndex].IntAnt & 0x02) != 0x02))
                                        {
                                            if ((Ant & 0x01) == 0x01 || (Ant & 0x02) == 0x02)
                                            {
                                                //TimeSpan span = DateTime.Now - listEpc[iIndex].TimeLastRead;
                                                //if (span.TotalSeconds < 5)
                                                //{
                                                //    lock (listEpc)
                                                //    {
                                                //        listEpc[iIndex].BorrOrRet = ToolsBorrRet.借出;
                                                //    }
                                                //}
                                                lock (listEpc)
                                                {
                                                    listEpc[iIndex].BorrOrRet = ToolsBorrRet.借出;
                                                }
                                            }
                                        }

                                        lock (listEpc)
                                        {
                                            listEpc[iIndex].IntAnt = Ant;//| listEpc[iIndex].IntAnt
                                        }


                                        //lock (listEpc)
                                        //{
                                        //    listEpc[iIndex].TimeLastRead = DateTime.Now;
                                        //}
                                    }
                                    if (listEpc[iIndex].UsingReadNo != DeviceUsing.启用)
                                    {
                                        lock (listEpc)
                                        {
                                            listEpc[iIndex].UsingReadNo = DeviceUsing.启用;
                                        }
                                    }
                                }

                                #endregion

                                #region  常规读取

                                //else
                                //{
                                //    lock (listEpc)
                                //    {
                                //        listEpc[iIndex].TimeLastRead = DateTime.Now;
                                //    }
                                //}

                                #endregion

                                blHas = true;
                                break;
                            }
                        }
                        if (blHas == false)
                        {
                            clsReadEpc clsEpc = new clsReadEpc(strEpc, Ant);
                            clsEpc.TimeLastRead = DateTime.Now;
                            if (usingReadNo == DeviceUsing.启用)
                            {
                                clsEpc.UsingReadNo = DeviceUsing.启用;
                            }
                            clsEpc.BorrOrRet = ToolsBorrRet.未知;
                            clsEpc.IsEpcRead = IsReadShow.未读;

                            lock (listEpc)
                            {
                                listEpc.Add(clsEpc);
                            }
                        }
                    }
                }
                else
                {
                    blRet = false;
                }
                blScan = false;
            }
            catch (Exception ee)
            {
                if (frmMain.blDebug)
                {
                    MessageUtil.ShowTips(ee.Message);
                }
            }
            return(blRet);
        }