RFID的麻烦 - 阅读程序

时间:2015-12-22 06:11:46

标签: c# rfid motorola motorola-emdk

我是编程RFID读者的新手。我有摩托罗拉MC9090G1,我的任务 - 阅读RFID标签并将其写在列表中。在摩托罗拉EMDK for .NET就是一个例子,这是我的目标。但问题是标签列表的添加不是即时的,而是一个显着的延迟(15-30秒)。如何解决?

以下是代码:

git clean  -d  -fx ""

}

最初认为计时器中的问题:

    /// <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次以上。

1 个答案:

答案 0 :(得分:2)

您必须更具体地了解您目前使用的资源和目标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
        }

然后使用Trigger按下和释放事件

执行清单
        #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();

就是这样

我希望这可能有所帮助