0

I'm using a 3rd party DLL which connects to an RFID reader via ethernet, now I've got this working without issue to an individual RFID reader using the following:

private static readonly CRRU4 myReader = new CRRU4();

I then pass it connection information which includes IP address and different parameters needed.

Now my question is, how would I do this for a number of IP addresses? I need to refer to each reader to read the RFID tags and handle them separately.

I have thought about cycling through each RFID reader, connecting doing a read and then disconnecting but this is not feasible based on time. I'm wanting to provide updates every second, the connection can take 2-3 seconds to complete.

Would multi-threading work for something like this? For each IP spawn a new thread and tell it to handle a specific IP?

Pete O'Connell
  • 143
  • 1
  • 2
  • 8
  • You could have a list of readers. You can add to a list quite easily to so if you need to add a new one you can. – Pheonyx Oct 20 '14 at 15:43
  • 1
    Is the third party library thread safe and does it implement concurrent access? How many IP addresses? One IP address per thread will not scale very well, a thread pool may be better. – Polyfun Oct 20 '14 at 15:53
  • the third party dll does support concurrent access as I've created 2 "readers" fine. I've not spoken to the developers of the 3rd party dll and their documentation is somewhat lacking and support is slow to say the least. It'd be quicker for me to test it rather than asking them. – Pete O'Connell Oct 21 '14 at 07:41
  • Also we believe the maximum readers will be around 20, 20 separate threads shouldn't be too much of an issue will it? – Pete O'Connell Oct 21 '14 at 07:59

1 Answers1

1

I would recommend creating a list of readers, and a Timer for each reader. Something like:

class Reader
{
    // other stuff
    Timer _updateTimer;

    public void Connect(ipAddress, TimeSpan pollingFrequency)
    {
        // Do the connection

        // Then set up the timer
        _updateTimer = new Timer(UpdateProc, null, 
            pollingFrequency, pollingFrequency);
    }

    private void UpdateProc(object state)
    {
        // poll the device here, and do any update
    }
}

And to create the readers:

List<Reader> _readersList = new List<Reader>();
for all devices
    var reader = new Reader();
    reader.Connect(ipAddress, TimeSpan.FromSeconds(1));
    _readersList.Add(reader);

Now, each reader will be polled every second.

If the polling/update process could potentially take more than one second, you need to modify the timer update proc so that it doesn't allow concurrent entry. That is, if one polling operation takes longer than a second, the timer is going to fire again and then you'll have the problem of two threads trying to poll the same device. Probably the easiest way to prevent this is with a Monitor:

private object _updateLock = new object();
private void UpdateProc(object state)
{
    if (!Monitor.TryEnter(_updateLock)) return;
    try
    {
        // poll and update
    }
    finally
    {
        Monitor.Exit(_updateLock);
    }
}
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351