2015-12-22 127 views
1

我是RFID閱讀器編程新手。我有摩托羅拉MC9090G1,我的任務 - 讀取RFID標籤並將其寫入列表中。在摩托羅拉EMDK for .NET是一個例子,這是我的目標。但問題是,添加到標籤列表不是即時的和顯着的延遲(15-30秒)。如何解決它?RFID閱讀程序故障

下面是代碼:

/// <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); 
      } 
     } 

    } 
} 

}

最初認爲在計時器的問題:

// Set timer which handles sending further reads until trigger is released 
     this.ReaderTimer.Interval = 2000; 
     this.ReaderTimer.Enabled = true; 

然而,設置的間隔0不解決問題。通過按下「觸發器」開始讀取,直到讀取列表中的標籤,標籤讀取爲20次以上。

回答

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性質列出它們在你的(如標籤識別屬性)

最後退訂的事件處理程序,並斷開你的讀者正確

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

rfidReader.Disconnect(); 

就是這樣

我希望這可幫助

+0

謝謝你對這個例子的詳細解答。 –