C# RFID读取程序故障

C# RFID读取程序故障,c#,rfid,motorola,motorola-emdk,C#,Rfid,Motorola,Motorola Emdk,我是编程RFID阅读器的新手。我有摩托罗拉MC90G1,我的任务读取RFID标签,并写在清单上。在摩托罗拉,EMDK for.NET就是一个例子,这是我的目标。但问题是,添加到标签列表中并不是即时的,而且会有明显的延迟(15-30秒)。如何修复它 代码如下: /// <summary> /// The main entry point for the application. /// </summary> static void Main(

我是编程RFID阅读器的新手。我有摩托罗拉MC90G1,我的任务读取RFID标签,并写在清单上。在摩托罗拉,EMDK for.NET就是一个例子,这是我的目标。但问题是,添加到标签列表中并不是即时的,而且会有明显的延迟(15-30秒)。如何修复它

代码如下:

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main() 
    {
        ReaderForm rf = new ReaderForm();
        rf.DoScale();
        Application.Run(rf);
    }

    /// <summary>
    /// Occurs before the form is displayed for the first time.
    /// </summary>
    private void ReaderForm_Load(object sender, System.EventArgs e)
    {
        // Add MainMenu if Pocket PC
        if (Symbol.Win32.PlatformType.IndexOf("PocketPC") != -1)
        {
            this.Menu = new MainMenu();
        }

        // If we can initialize the Reader
        if ( !this.InitReader() )
        {
            MessageBox.Show("Unable to initialize RFID!","Error");

            // If not, close this form
            this.Close();

            return;
        }
    }
    /// <summary>
    /// Application is closing
    /// </summary>
    protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
    {
        // Terminate reader
        this.TermReader();

        base.OnClosing(e);
    }

    /// <summary>
    /// Initialize the reader.
    /// </summary>
    private bool InitReader()
    {
        // If reader is already present then fail initialize
        if ( this.MyReader != null )
        {
            return false;
        }

        try
        {
            // Create new reader, first available reader will be used.
            this.MyReader = new Symbol.RFID.Reader();

            // Create reader data to read upto 20 tags
            this.MyReaderData = new Symbol.RFID.ReaderData(20);

            // Enable reader
            this.MyReader.Actions.Enable();

            // Attach handler for read notification
            this.MyEventHandler = new EventHandler(MyReader_ReadNotify);
            this.MyReader.ReadNotify += this.MyEventHandler;

            // Attach handler for trigger notification
            this.MyTriggerHandler = new Symbol.RFID.Reader.TriggerEventHandler(MyTrigger_Pressed);
            this.MyReader.TriggerNotify += this.MyTriggerHandler;

            // Attach to activate and deactivate events
            this.Activated += new EventHandler(ReaderForm_Activated);
            this.Deactivate +=new EventHandler(ReaderForm_Deactivate);
        }
        catch
        {
            return false;
        }

        return true;
    }

    /// <summary>
    /// Stop reading and disable/close reader
    /// </summary>
    private void TermReader()
    {
        // If we have a reader
        if ( this.MyReader != null )
        {
            // Remove read notification handler
            this.MyReader.ReadNotify -= null;

            // Attempt to stop any pending read
            this.StopRead();

            // Disable reader, with wait cursor
            this.MyReader.Actions.Disable();

            // Free it up
            this.MyReader.Dispose();

            // Indicate we no longer have one
            this.MyReader = null;
        }

        // If we have a reader data
        if ( this.MyReaderData != null )
        {
            // Free it up
            this.MyReaderData.Dispose();

            // Indicate we no longer have one
            this.MyReaderData = null;
        }
    }

    /// <summary>
    /// Start a read on the reader
    /// </summary>
    private void StartRead()
    {
        this.StatusLabel.Text = "Reading ...";

        // If the flag is set then start a new TagList
        if (ClearListFlag)
            this.MyReaderData.ClearInventory = true;
        else
            this.MyReaderData.ClearInventory = false;

        // If we have both a reader and a reader data
        if((this.MyReader != null) && (this.MyReaderData != null))
        {
            // Submit a read
            this.MyReader.Actions.Read(this.MyReaderData);
        }
    }

    /// <summary>
    /// Stop all reads on the reader
    /// </summary>
    private void StopRead()
    {
        // If we have a reader
        if ( this.MyReader != null )
        {
            // Prevent new reads
            this.MyReader.TriggerNotify -= this.MyTriggerHandler;

            // Disable the timer
            this.ReaderTimer.Enabled = false;

            // Try to cancel pending read or wait for it's completion
            this.MyReader.Actions.Flush();
        }
    }

    /// <summary>
    /// Handles Stage2 Notification from the trigger
    /// </summary>
    private void MyTrigger_Pressed(object sender, Symbol.RFID.TriggerEventArgs e)
    {
        if ( e.NewState == Symbol.RFID.TriggerState.STAGE2 )
        {
            this.StatusLabel.Text = "Trigger Pressed. Reader Busy...";

            // If another RFID operation has not already started
            if (!this.MyReader.Info.IsBusy)
            {
                // Start a new read
                this.StartRead();
            }

            // Set timer which handles sending further reads until trigger is released
            this.ReaderTimer.Interval = 2000;
            this.ReaderTimer.Enabled = true;
        }
        else if ( e.NewState == Symbol.RFID.TriggerState.RELEASED )
        {
            this.StatusLabel.Text = "Press trigger to read RFID tags";

            // Disable the timer
            this.ReaderTimer.Enabled = false;
        }
    }

    /// <summary>
    /// Submits a new read at every timer tick
    /// </summary>
    private void ReaderTimer_Tick(object sender, System.EventArgs e)
    {
        // If another RFID operation has not already started
        if (!this.MyReader.Info.IsBusy)
        {
            // Start a new read
            this.StartRead();
        }
    }

    /// <summary>
    /// Read complete or failure notification
    /// </summary>
    private void MyReader_ReadNotify(object sender, EventArgs e)
    {
        // Get the next ReaderData
        Symbol.RFID.ReaderData TheReaderData =
            (Symbol.RFID.ReaderData)this.MyReader.GetNextReaderData();

        // If it is a successful read (as opposed to a failed one)
        if ( TheReaderData.Result == Symbol.Results.SUCCESS )
        {
            // Handle the data from this read
            this.HandleData(TheReaderData);
        }
    }

    /// <summary>
    /// Handle data from the reader
    /// </summary>
    private void HandleData(Symbol.RFID.ReaderData TheReaderData)
    {
        // Clear the previous list
        this.ReaderDataListView.Items.Clear();

        // Populate the list with the updated data
        for (int i=0; i<MyReaderData.TagList.TotalTags; i++)
        {
            string[] sItems = new string[] 
                                {
                                    i.ToString(), 
                                    MyReaderData.TagList[i].ToString(), 
                                    MyReaderData.TagList[i].ReadCount.ToString()
                                };

            this.ReaderDataListView.Items.Add(new ListViewItem(sItems));
        }

        // Clear the flag so that the Taglist is maintained
        ClearListFlag = false;
    }

    /// <summary>
    /// Clears the Inventory list on the form and sets the flag to
    /// start a new inventory.
    /// </summary>
    private void btnClearList_Click(object sender, System.EventArgs e)
    {
        // Clear the previous list
        this.ReaderDataListView.Items.Clear();

        // Set the flag to notify StartRead to start a new inventory
        ClearListFlag = true;
    }

    /// <summary>
    /// Exits the application.
    /// </summary>
    private void btnExit_Click(object sender, System.EventArgs e)
    {
        // Close this form
        this.Close();
    }

    private void btnClearList_KeyDown(object sender, KeyEventArgs e)
    {
        // Checks if the key pressed was an enter button (character code 13)
        if (e.KeyValue == (char)13)
            btnClearList_Click(this, e);
    }

    private void btnExit_KeyDown(object sender, KeyEventArgs e)
    {
        // Checks if the key pressed was an enter button (character code 13)
        if (e.KeyValue == (char)13)
            btnExit_Click(this, e);
    }

    private void ReaderForm_KeyUp(object sender, KeyEventArgs e)
    {
        this.ReaderDataListView.Focus();
    }

    /// <summary>
    /// Called when ReaderForm is activated
    /// </summary>
    private void ReaderForm_Activated(object sender, EventArgs e)
    {
        // Enable the trigger
        if (this.MyReader != null)
            this.MyReader.TriggerNotify += this.MyTriggerHandler;
    }

    /// <summary>
    /// Called when ReaderForm is deactivated
    /// </summary>
    private void ReaderForm_Deactivate(object sender, EventArgs e)
    {
        // Disable the trigger
        if (this.MyReader != null)
            this.MyReader.TriggerNotify -= this.MyTriggerHandler;
    }

    private void ReaderForm_Resize(object sender, EventArgs e)
    {
        if (bInitialScale == true)
        {
            return; // Return if the initial scaling (from scratch)is not complete.
        }

        if (Screen.PrimaryScreen.Bounds.Width > Screen.PrimaryScreen.Bounds.Height) // If landscape orientation
        {
            if (bPortrait != false) // If an orientation change has occured to landscape
            {
                bPortrait = false; // Set the orientation flag accordingly.
                bInitialScale = true; // An initial scaling is required due to orientation change.
                Scale(this); // Scale the GUI.
            }
            else
            {   // No orientation change has occured
                bSkipMaxLen = true; // Initial scaling is now complete, so skipping the max. length restriction is now possible.
                Scale(this); // Scale the GUI.
            }
        }
        else
        {
            // Similarly for the portrait orientation...
            if (bPortrait != true)
            {
                bPortrait = true;
                bInitialScale = true;
                Scale(this);
            }
            else
            {
                bSkipMaxLen = true;
                Scale(this);
            }
        }

    }
}

但是,设置间隔0并不能解决问题。对于通过按下“触发器”开始读取,直到读取列表中的标记,标记读取为20+次

您必须更具体地说明您当前使用的资源和目标SDK平台。为了清楚起见,大多数EMDK都有一个版本号,根据版本号和您使用的CF,您的实现将有细微或重大的差异。 但是,一个非常简单的RFID标签读取和列表过程的实现将非常简单

连接到读卡器:

        string hostName = "localhost 
        rfidReader = new RFIDReader(hostName, 0, 0);
        try
        {
            rfidReader.Connect();

            AccessComplete = new AutoResetEvent(false);

            rfidReader.Events.NotifyInventoryStartEvent = true;
            rfidReader.Events.NotifyAccessStartEvent = true;
            rfidReader.Events.NotifyAccessStopEvent = true;
            rfidReader.Events.NotifyInventoryStopEvent = true;
            rfidReader.Events.NotifyAntennaEvent = true;
            rfidReader.Events.NotifyBufferFullWarningEvent = true;
            rfidReader.Events.NotifyBufferFullEvent = true;
            rfidReader.Events.NotifyGPIEvent = true;
            rfidReader.Events.NotifyReaderDisconnectEvent = true;
            rfidReader.Events.NotifyReaderExceptionEvent = true;
            rfidReader.Events.AttachTagDataWithReadEvent = false; //true if ReadNotify is ON
            // Regitering for status event notification
            rfidReader.Events.StatusNotify += new Events.StatusNotifyHandler(Events_StatusNotify);

        // Exclude the events each other for best results in my case I use Status Notify only

            //// Registering for read tag event notification
            //rfidReader.Events.ReadNotify += new Events.ReadNotifyHandler(Events_ReadNotify);

        }
        catch (OperationFailureException operationFailureException)
        {
           // Handle this exception type
        }
        catch (Exception ex)
        {
            // Handle general exception type
        }
然后使用触发器按下并释放事件执行库存

        #region Postfilters

        // Add postfilters if you need them
        PostFilter posFilter = new PostFilter();
        posFilter.UseRSSIRangeFilter = true;
        posFilter.RssiRangeFilter.MatchRange = MATCH_RANGE.WITHIN_RANGE;
        posFilter.RssiRangeFilter.PeakRSSILowerLimit = lowLimit;
        posFilter.RssiRangeFilter.PeakRSSIUpperLimit = upLimit;

        #endregion

        #region TriggerInfo

        // Control Inventory using Handheld Trigger Events
        TriggerInfo triggerInfo = new TriggerInfo();
        triggerInfo.TagReportTrigger = 0;
        triggerInfo.StartTrigger.Type = START_TRIGGER_TYPE.START_TRIGGER_TYPE_HANDHELD;
        triggerInfo.StartTrigger.Handheld.HandheldEvent = HANDHELD_TRIGGER_EVENT_TYPE.HANDHELD_TRIGGER_PRESSED;
        triggerInfo.StopTrigger.Type = STOP_TRIGGER_TYPE.STOP_TRIGGER_TYPE_HANDHELD_WITH_TIMEOUT;
        triggerInfo.StopTrigger.Handheld.HandheldEvent = HANDHELD_TRIGGER_EVENT_TYPE.HANDHELD_TRIGGER_RELEASED;
        triggerInfo.StopTrigger.Handheld.Timeout = 0;

        #endregion

        //Perform inventory
        rfidReader.Actions.Inventory.Perform(posFilter, triggerInfo, null);
然后实现状态通知功能

    private void Events_StatusNotify(object sender, Events.StatusEventArgs e)
    {
        if (e.StatusEventData.StatusEventType == Symbol.RFID3.Events.STATUS_EVENT_TYPE.INVENTORY_STOP_EVENT)
        {
            TagData[] inventoryTags = rfidReader.Actions.GetReadTags(5); // set number of tags you want to save
然后,您就可以使用inventoryTags数组来设置所需的标记

使用TagData属性将它们列在列表中(例如TagID属性)

最后取消订阅EventHandler并正确断开读卡器的连接

rfidReader.Events.StatusNotify -= new Events.StatusNotifyHandler(Events_StatusNotify);

rfidReader.Disconnect();
就这样


我希望这可能会有所帮助

谢谢您对示例的详细回答。
rfidReader.Events.StatusNotify -= new Events.StatusNotifyHandler(Events_StatusNotify);

rfidReader.Disconnect();