⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rfid.cs

📁 手持式RFID及条码车辆识别系统手持机代码
💻 CS
字号:
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;

//
using Intermec.DataCollection.RFID;


namespace RFID_CAR
{
    /// <summary>
    /// RFID 的摘要说明。
    /// </summary>
    public class RFIDtags
    {
        public BRIReader m_Reader = null;
        public bool m_fConnected = false;
        private Control m_Owner = null;
        public string sCurrentTagType = "";


        public RFIDtags(Control owner)
        {
            m_Owner = owner;


            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        public void Dispose()
        {
            if (this.m_Reader != null)
            {
                this.m_Reader.Dispose();
                this.m_Reader = null;
            }

        }
        bool IsDceTransport(string aReaderURI)
        {
            return (aReaderURI == null || aReaderURI.IndexOf("127.0.0.1") != -1) ?
                true : false;
        }
        public bool OpenRFIDReader(System.String sURIAddress)
        {
            bool fSuccess = true;
            //
            // Enable the DCE transport on the host computer (series 700 device).
            //
            if (IsDceTransport(sURIAddress) && this.EnableDCETransport() == false)
                return false;

            try
            {	// v2.4: Enable logging to a file using the AdvancedBRI class library.
                //
                BRIReader.LoggerOptionsAdv logOpts = new BRIReader.LoggerOptionsAdv();
                logOpts.LogEnable = true;
                logOpts.LogFilePath = @"\MySampleLog1.txt";
                logOpts.TimeStampEnable = true;
                logOpts.ShowNonPrintableChars = true;
                logOpts.MaxFileSize = 200000;
                //
                // Open a connection to an IP4 reader using the AdvancedBRI class library.
                //
                this.m_Reader = new BRIReader(m_Owner, sURIAddress, logOpts);
                if (this.m_Reader.IsConnected)
                {
                }
            }
            catch (BasicReaderException e)
            {
                fSuccess = false;
                string sErr = e.Message.ToString();
                if (null != this.m_Reader)
                {
                    this.m_Reader.Dispose();
                    this.m_Reader = null;
                }
            }
            catch (System.SystemException e)
            {
                fSuccess = false;
                string sErr = e.Message.ToString();
                if (null != this.m_Reader)
                {
                    this.m_Reader.Dispose();
                    this.m_Reader = null;
                }
            }
            catch (Exception e)
            {
                fSuccess = false;
                string sErr = e.Message.ToString();
                if (null != this.m_Reader)
                {
                    this.m_Reader.Dispose();
                    this.m_Reader = null;
                }
            }
            this.m_fConnected =
                (null != this.m_Reader && this.m_Reader.IsConnected) ? fSuccess : false;
            return this.m_fConnected;
        }
        private bool EnableDCETransport()
        {
            bool fSuccess = false;
            try
            {
                BRIReader DCEConfig = new BRIReader(null, "TCP://127.0.0.1:2188");
                string sr = DCEConfig.Execute("device 1 attrib adminstatus");
                if (sr.IndexOf("ADMINSTATUS=ON", 0) == -1)
                {	//
                    // The DCE's IP4 reader connection is not configured, so enable it now.
                    //
                    sr = DCEConfig.Execute("device 1 attrib adminstatus=on");
                }
                if (sr.IndexOf("ERR") == -1)
                    fSuccess = true;
                else
                    fSuccess = false;

                DCEConfig.Dispose();
            }
            catch (BasicReaderException bex)
            {
                fSuccess = false;
            }
            catch (Exception ex)
            {
                fSuccess = false;
            }
            return fSuccess;
        }
        public bool SetReaderTagType(string sType)
        {
            bool fResult = false;

            if (this.m_Reader.IsConnected)
            {
                int iType = -1;
                switch (sType.ToUpper())
                {
                    case "ISO":
                        iType = ReaderAttributes.TagTypeMask.ISO6B_G2;
                        sCurrentTagType = "ISO";
                        break;
                    case "GEN2":
                        iType = ReaderAttributes.TagTypeMask.EPC_CLASS1_G2;
                        sCurrentTagType = "GEN2";
                        break;
                    case "UCODE119":
                        iType = ReaderAttributes.TagTypeMask.ICODE119;
                        sCurrentTagType = "UCODE119";
                        break;
                    default:
                        sCurrentTagType = "ISO";
                        iType = ReaderAttributes.TagTypeMask.MIXED;
                        break;
                }
                if (-1 != iType)
                    fResult = this.m_Reader.Attributes.SetTagTypes(iType);
            }

            return fResult;
        }
        public bool SearchTags()
        {
            string sSchema = null, sFilter = null;
            bool fSuccess = false;

            if (this.m_Reader == null)
            {
                return false;
            }
            // Do not permit writes with data-fields when the reader is 
            // configured for gen2 tags or code 119 tags.
            //
            if (this.sCurrentTagType == "GEN2" || this.sCurrentTagType == "UCODE119")
            {
                // EPC-Gen2 tags do no support data fields, though some accept 4 bytes 
                // in bank2.
                //
                sSchema = null; sFilter = null;
            }
            else
            {
                // Create a BRI schema used to read two 4-byte strings from an 
                // ISO tag starting respectively at offsets 18 and 22.
                //
                sSchema = "";
                sFilter = "TAGID=H????????????????";
            }
            try
            {
                if (null == sSchema)
                {
                    // No data fields exist for this tag type, just get each 
                    // tag's tag-key value.
                    //
                    this.m_Reader.DefaultFieldSchema = null;
                    fSuccess = m_Reader.Read();
                }
                else
                {	// Use data fields and a filter for this tag type.
                    // The first argument is a TagKey filter, and the second 
                    // argument is the schema string that defines the fields 
                    // we want to read from the tag. Upon success, the 
                    // BRIReader.Tags[] array will contain one Tag object for 
                    // each tag that was read, and each Tag object will contain 
                    // TagField elements containing data read from the physical 
                    // tag.
                    //
                    fSuccess = m_Reader.Read(sFilter, sSchema);
                }
                if (m_Reader.TagCount == 0)
                {
                    return false;
                }
                if (this.m_Reader.IsTagError)
                {
                    return false;
                }
            }
            catch (Intermec.DataCollection.RFID.BasicReaderException e)
            {
                fSuccess = false;
            }
            catch (Intermec.DataCollection.RFID.BRIParserException e)
            {
                fSuccess = false;
            }
            catch (SystemException e)
            {
                fSuccess = false;
            }
            if (m_Reader.TagCount > 1) fSuccess = false;
            return fSuccess;
        }

        public String DisplayTagsAsStrings()
        {
            String rst = "";

            

            foreach (Tag tt in m_Reader.Tags)
            {	//
                // Populate each Tag node in the TreeView with its unique tag key.
                //

                rst += tt.ToString();
            }
            return (rst);
        }
        public String GetTagsFiledAsString()
        {
            Tag tt = (Tag)(m_Reader.Tags.GetValue(0));
            string sField = "";

            if (tt.TagFields.ItemCount > 0)
            {
                foreach (TagField tf in tt.TagFields.FieldArray)
                {
                    // Populate the Tag node in the treeview with each of its fields.
                    //
                    sField = tf.ToString();	// TagField data as a string.
                }
            }
            return sField;
        }

        public bool ReadTags(String TagID, int start, int len)
        {
            string sSchema = null, sFilter = null;
            bool fSuccess = false;

            if (this.m_Reader == null)
            {
                return false;
            }
            // Do not permit writes with data-fields when the reader is 
            // configured for gen2 tags or code 119 tags.
            //
            if (this.sCurrentTagType == "GEN2" || this.sCurrentTagType == "UCODE119")
            {
                // EPC-Gen2 tags do no support data fields, though some accept 4 bytes 
                // in bank2.
                //
                sSchema = null; sFilter = null;
            }
            else
            {
                // Create a BRI schema used to read two 4-byte strings from an 
                // ISO tag starting respectively at offsets 18 and 22.
                //
                sSchema = "string(" + start + "," + len + ")";
                sFilter = "TAGID=H" + TagID;
            }
            try
            {
                if (null == sSchema)
                {
                    // No data fields exist for this tag type, just get each 
                    // tag's tag-key value.
                    //
                    this.m_Reader.DefaultFieldSchema = null;
                    fSuccess = m_Reader.Read();
                }
                else
                {	// Use data fields and a filter for this tag type.
                    // The first argument is a TagKey filter, and the second 
                    // argument is the schema string that defines the fields 
                    // we want to read from the tag. Upon success, the 
                    // BRIReader.Tags[] array will contain one Tag object for 
                    // each tag that was read, and each Tag object will contain 
                    // TagField elements containing data read from the physical 
                    // tag.
                    //
                    fSuccess = m_Reader.Read(sFilter, sSchema);
                }
                if (m_Reader.TagCount == 0)
                {
                    return false;
                }
                if (this.m_Reader.IsTagError)
                {
                    return false;
                }
            }
            catch (Intermec.DataCollection.RFID.BasicReaderException e)
            {
                fSuccess = false;
            }
            catch (Intermec.DataCollection.RFID.BRIParserException e)
            {
                fSuccess = false;
            }
            catch (SystemException e)
            {
                fSuccess = false;
            }
            return fSuccess;
        }

        public bool WriteTags(String TagID, int start, int len, String data)
        {
            string sSchema = null, sFilter = null;
            bool fSuccess = false;
            string writecmd = null;
            //TagField tf = new TagField();
            writecmd = "write string(" + start.ToString() + "," + len.ToString() + ")=\"" + data + "\"";

            byte [] retBuffer = new byte [2048];
			
            try
            {
                m_Reader.Execute(System.Text.ASCIIEncoding.ASCII.GetBytes(writecmd), retBuffer);
                
                string xxx = System.Text.ASCIIEncoding.ASCII.GetString(retBuffer, 0, 2048);
                string[] x = xxx.Split(' ');
                if (x[1].StartsWith("WROK"))
                    if (ReadTags(TagID, start, len))
                        if (string.Equals(GetTagsFiledAsString(), data))
                            return true;

                //Tag tt = (Tag)(m_Reader.Tags.GetValue(0));
				
                //if(tt.TagFields.ItemCount >= 0 )
                //{
                //    tt.TagFields.GetField(0).SetDataString (data,0x20,data.Length);
                //    fSuccess = m_Reader.Update();
                //}

                if( this.m_Reader.IsTagError )
                {
                    return false;
                }
            }
            catch( Intermec.DataCollection.RFID.BasicReaderException e )
            {
                return false;
            }
            catch( Intermec.DataCollection.RFID.BRIParserException e )
            {
				
                return false;
            }
            catch( SystemException e )
            {
                return false;
            }
             

            return false;
            
        }
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -