2015-12-22 2 views
1

Je suis nouveau dans la programmation de lecteurs RFID. J'ai Motorola MC9090G1, ma tâche - lire RFID - tag et l'écrire dans la liste. Dans Motorola EMDK pour .NET est un exemple de cela est mon objectif. Mais le problème est que l'ajout à la liste des tags n'est pas instantané et un retard important (15-30sec). Comment le réparer?Problème avec le programme de lecture RFID

Voici le code:

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

    } 
} 

}

d'abord pensé que le problème de la minuterie:

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

Cependant, réglage de l'intervalle 0 ne résout pas le problème. Et pour le début de la lecture en appuyant sur le "déclencheur" jusqu'à lire les balises dans la liste, les balises lues comme 20+ fois.

Répondre

2

Vous devez être un peu plus précis sur les ressources que vous utilisez actuellement et sur la plate-forme SDK que vous ciblez. Afin d'être clair, la plupart des EMDK ont un numéro de version et, en fonction de cela et de la FC que vous utilisez, votre implémentation aura des différences mineures ou majeures. Mais une implémentation très simple d'un processus de lecture et de listage des tags RFID sera.

Se connecter au lecteur:

 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 
     } 

Ensuite, allez et exécuter l'inventaire à l'aide de l'événement presse et relâchez la gâchette

 #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); 

Ensuite, mettre en œuvre votre statut fonction Notify

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 

Ensuite, vous avez stockTags array les Tags dont vous avez besoin.

Utiliser les propriétés tagData les énumérer sur votre (par exemple la propriété TagID)

Et enfin désabonnement le gestionnaire d'événements et débranchez votre lecteur correctement

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

rfidReader.Disconnect(); 

Et voilà

J'espère que cela peut help

+0

Merci pour cette réponse détaillée à l'exemple. –