Click here to Skip to main content
15,888,116 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am developing winforms application using C#. This app uses ThingMagic USB Plus RFID "M5e" reader in order to scan the tags that found. I have the manual of the reader which is Mercury API and it provides codelet sample. In the attached samples, there is not any Pseudo Asynchronous example, but I understand the concept, it needs multi-threading and I do not have experience in threading.

Summary of the interface that I have now, it has Gridview that will display the scanned tags using Synchronous approachable "Which result in read once", I need it to be Pseudo Asynchronous approach. So, Just I want to make the reading operation done in separate thread.

This is the capture from the manual.

Pseudo-Asynchronous Reading
In pseudo-asynchronous reading a synchronous search is looped over and over again
running indefinitely in a separate thread. Tags are off-loaded once every synchronous
search is completed. i.e., read listeners will be called once for every "/reader/read/
asyncOnTime" milliseconds. On all readers except the M6 and M6e pseudoasynchronous
reading is the only implementation used for background reading
operations. 





Also see this link, it is similar issue but i did not able to implement that.

c# - Using begin/end invoke with continuous RFID read - Stack Overflow[^]

What I have tried:

<pre>public Reader objReader = null;  // Create reader object that reads the scanned tags.
        private ReadTags[] arr1 = new ReadTags[0];
        private ReadTags[] arr = null;

        public MyCart()
        {
            InitializeComponent();
            //UpdateComport();
            dataGrid1.DataSource = arr1;
            generatedatagrid(); 

        }


     public void generatedatagrid()
        {
            DataGridTableStyle tableStyle = new DataGridTableStyle(); 
            tableStyle.MappingName = arr1.GetType().Name; // record instance 
            

            DataGridTextBoxColumn tbcName = new DataGridTextBoxColumn(); 
            tbcName.Width = dataGrid1.Width * 50 / 100;
            tbcName.MappingName = "ProName"; 
            tbcName.HeaderText = "Product Name";
            tableStyle.GridColumnStyles.Add(tbcName);

            tbcName = new DataGridTextBoxColumn();
            tbcName.Width = dataGrid1.Width * 25 / 100;
            tbcName.MappingName = "ProPrice";
            tbcName.HeaderText = "Price";
            tableStyle.GridColumnStyles.Add(tbcName);

            tbcName = new DataGridTextBoxColumn();
            tbcName.Width = dataGrid1.Width * 25 / 100;
            tbcName.MappingName = "ReadCount";
            tbcName.HeaderText = "Quantity";
            tableStyle.GridColumnStyles.Add(tbcName);

            //tbcName = new DataGridTextBoxColumn();
            //tbcName.Width = dataGrid1.Width * 12 / 100;
            //tbcName.MappingName = "";
            //tbcName.HeaderText = "Total";
            //tableStyle.GridColumnStyles.Add(tbcName);

            dataGrid1.TableStyles.Clear();
            dataGrid1.TableStyles.Add(tableStyle); // bind
        }

<pre>     private void btnReadOnce_Click(object sender, EventArgs e)
        {
            try
            {
                //int TotalTagCount = 0;
                btnReadOnce.Enabled = false;

              
               TagReadData[] tagID = objReader.Read(int.Parse(tbxReadTimeout.Text));
                    arr = new ReadTags[tagID.Length];
                    for (int i = 0; i < tagID.Length; i++)
                    {
                        arr[i] = new ReadTags(tagID[i]);
                        //TotalTagCount = TotalTagCount + tagID[i].ReadCount;
                    }
                    dataGrid1.DataSource = arr;
                    generatedatagrid();
                    //lblTotalTagCount.Text = TotalTagCount.ToString();
                    //lblUniqueTagCount.Text = tagID.Length.ToString();
                    btnReadOnce.Enabled = true;
                    btnReadOnce.Focus();
             

            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.Message, "Error");
                objReader.Destroy();
                objReader = null;
                btnReadOnce.Enabled = false;
                btnConnect.Text = "Connect";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Reader Message");
                btnReadOnce.Enabled = true;
            }
        }



        private void btnConnect_Click(object sender, EventArgs e)
        {

            try
            {
                if (btnConnect.Text.Equals("Connect"))
                {
                    String model = string.Empty;
                    string readeruri = "COM3";
                        //comboBox1.SelectedItem.ToString(); 
                    objReader = Reader.Create(string.Concat("eapi:///", readeruri));
                    objReader.Connect();
                    model = (string)objReader.ParamGet("/reader/version/model");
                    Reader.Region regionToSet = (Reader.Region)objReader.ParamGet("/reader/region/id");
                    if (objReader is SerialReader)
                    {
                        //if (regionToSet == Reader.Region.UNSPEC)
                        //{
                        //    if (model.Equals("M6e PRC"))
                        //    {
                        //        regionToSet = Reader.Region.PRC;
                        //    }
                        //    else
                        //    {
                        //        regionToSet = Reader.Region.NA;
                        //    }
                        regionToSet = Reader.Region.OPEN;
                    }
                    objReader.ParamSet("/reader/region/id", regionToSet);

                    if (model.Equals("M5e"))
                    {
                        SimpleReadPlan plan = new SimpleReadPlan(new int[] { 1 }, TagProtocol.GEN2);
                        objReader.ParamSet("/reader/read/plan", plan);
                    }
                    //if (model.Equals("M6e Nano"))
                    //{
                    //    SimpleReadPlan plan = new SimpleReadPlan(new int[] { 1 }, TagProtocol.GEN2);
                    //    objReader.ParamSet("/reader/read/plan", plan);
                    //}
                    btnConnect.Text = "Disconnect";
                    btnReadOnce.Enabled = true;
                    comboBox1.Enabled = false;
                    btnRefresh.Enabled = false;
                    lblReadTimeout.Enabled = true;
                    tbxReadTimeout.Enabled = true;
                    UpdateGrid();
                }
                else
                {
                    objReader.Destroy();
                    objReader = null;
                    btnReadOnce.Enabled = false;
                    comboBox1.Enabled = true;
                    lblReadTimeout.Enabled = false;
                    tbxReadTimeout.Enabled = false;
                    btnConnect.Text = "Connect";
                    btnRefresh.Enabled = true;
                    lblTotalTagCount.Text = "0";
                    lblUniqueTagCount.Text = "0";
                }
            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.Message, "Error");
                objReader.Destroy();
                objReader = null;
                btnReadOnce.Enabled = false;
                btnConnect.Text = "Connect";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Reader message");
            }

        }


    public class ReadTags
    {
        private string epc;     // hexa ID
        //private string timestamp;
        private string readcount;   // Quantity 
        //public string rssi;
        private string proName;   // pro name
        private string proPrice;  // pro proice
        private int idDec;    // Converted id 

        public ReadTags()
        {
        }

        public ReadTags(TagReadData adddata)
        {
            epc = adddata.EpcString;
            idDec = int.Parse(epc, System.Globalization.NumberStyles.HexNumber);
            //timestamp = adddata.Time.ToString();
            readcount = adddata.ReadCount.ToString();
            //rssi = adddata.Rssi.ToString();
}
Posted
Updated 31-Mar-17 21:48pm
v2

1 solution

I think you first need to read up on Tasks (which is the modern way of threads).
Here is a good article about it: Task Parallel Library: 1 of n[^]
As you already mentioned "Pseudo", it is probably best to not let all Tasks access the same USB port simultaneously.
When working with Tasks you can e.g. use .ContinueWith to start another Task when a Task is finished.

Oh, and I would also recommend using a DataSource or BindingList for your DataGrid, here is an excellent article about it: A-Detailed-Data-Binding-Tutorial
Your task could then keep running on the background (long running Task) and update the DataSource which automagically updates the DataGrid.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900