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次以上。
謝謝你對這個例子的詳細解答。 –