简体   繁体   English

如何使用 PC 应用程序使用路由器读取三个 TCP/IP RFID 阅读器?

[英]How to read three TCP/IP RFID readers using a router using a PC application?

I have purchased 3 RFID readers which are TCP/IP compatible.我购买了 3 个兼容 TCP/IP 的 RFID 阅读器。 I have started by first connecting one reader to my my router and also connected my PC to the same router.我首先将一个阅读器连接到我的路由器,并将我的 PC 连接到同一个路由器。 I have adjusted the reader's IP to match the routers IP, so the PC can communicate with the reader.我已经调整了阅读器的 IP 以匹配路由器 IP,因此 PC 可以与阅读器通信。 I use the following code to connect to the reader:我使用以下代码连接到阅读器:

private void connect_reader(string IP)
    {
        try
        {
            if (device.ConnectDevice(IP, 4501) == true)
            {
                try
                {
                    Boolean flag = device.StopReadTag();
                    flag = device.StopReadTag();
                    if (true == flag)
                    {
                        MessageBox.Show("Reader connected successfully.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        EnableButtons();
                    }
                    else
                    {
                        MessageBox.Show("Reader not connected successfully.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        device.DisConnectDivice();
                        button_OpenPort.Enabled = false;
                    }
                }
                catch (System.Net.Sockets.SocketException ex)
                {
                    MessageBox.Show("Reader not connected successfully.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    device.DisConnectDivice();
                }
            }
            else
            {
                MessageBox.Show("Reader not connected successfully.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        catch (System.Net.Sockets.SocketException ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

The following code is used to disconnect from the reader:以下代码用于断开与阅读器的连接:

private void disconnect_reader()
    {
        device.DisConnectDivice();
        MessageBox.Show("Reader disconnected successfully.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
        button_OpenPort.Enabled = true;
    }

The reader connects successfully to the PC using the application.阅读器使用该应用程序成功连接到 PC。 My router also has two more additional ethernet ports and therefore I would like to connect another two readers to the router.我的路由器还有两个额外的以太网端口,因此我想将另外两个阅读器连接到路由器。

My application is to extract the ID from the RFID tags when there are read by the RFID readers.我的应用程序是在 RFID 阅读器读取时从 RFID 标签中提取 ID。 I also want to know what reader scanned that particular RFID tag.我还想知道是什么阅读器扫描了那个特定的 RFID 标签。 For example, I have three readers with the following IP addresses: reader1(192.168.4.100), reader2(192.168.4.101), and readers(192.168.4.102).例如,我有三个读卡器,地址如下 IP:reader1(192.168.4.100)、reader2(192.168.4.101) 和 reader(192.168.4.102)。 Now position those three readers at three different positions in an apartment.现在 position 这三个阅读器在公寓的三个不同位置。 A person with an RFID tag walks pass reader2, reader2 then scans the RFID tag and relays the info back to the application.一个带有 RFID 标签的人走过 reader2,reader2 然后扫描 RFID 标签并将信息转发回应用程序。 First, the application can only connect to one antenna only.首先,应用程序只能连接到一个天线。 So what I want to do is to store all the IP addresses of the three readers in an array, as shown below.所以我要做的就是将三个读卡器的所有IP地址存储在一个数组中,如下图。 Then I want to loop through each reader for a period of time.然后我想循环浏览每个读者一段时间。 So in the example above when the RFID tag is detected my reader2, the application will store the RFID tag ID and also assign the RFID tag ID to reader2.因此,在上面的示例中,当我的 reader2 检测到 RFID 标签时,应用程序将存储 RFID 标签 ID 并将 RFID 标签 ID 分配给 reader2。

My question is, therefore, in the above code I was thinking of just storing the IP addresses of each reader in the array and then connect to one reader at a time.因此,我的问题是,在上面的代码中,我只想将每个阅读器的 IP 地址存储在数组中,然后一次连接到一个阅读器。 So for instances:因此,例如:

void reader() //example method
{
   array string reader_ip[] = {192.168.4.100; 192.168.4.101; 192.168.4.102};

   for(int i = 0; i <4; i++) //Loop through all readers by connecting to the reader via their IP addresses for a period of time them disconnect from the reader and then connect to the other reader.
   {
     connect_reader(reader_ip[i]); //Connects to the specific reader 
     readTags(); //readTags that are detected by the connected reader
     disconnect_reader(); disconnects to the specific reader 
     timeDelay(); //time delay of 2secs before reconnecting to the next reader
   }
}

Is this the correct way for switching between the networked readers, in order to obtain the data that is scanned by the different readers?这是在网络阅读器之间切换的正确方法,以获得不同阅读器扫描的数据吗?

Without knowing which manufacturer or SDK you are working with, I can only make you some suggestions in pseudo code.在不知道您正在与哪个制造商或 SDK 合作的情况下,我只能用伪代码给您一些建议。

Main Point: The example code you have provided is limiting you since you have chosen to have one device which I have understood to be an instance of an RFIDReader class.要点:您提供的示例代码限制了您,因为您选择了一个device ,我将其理解为RFIDReader class 的实例。 I would recommend you take an approach of having a collection of RFIDReader , that you connect to a single time at the being of your application, and maintain those connections until you are finished.我建议您采用收集RFIDReader的方法,在您的应用程序运行时连接到一个时间,并保持这些连接直到您完成。 That may look something like this:这可能看起来像这样:

In the following, I use a made up class called RFIDReader .在下文中,我使用了一个名为 RFIDReader 的RFIDReader It can Connect() , Disconnect() , and GrabTags() which simple up dates a the property of Tags , a collection of the tags in detected:它可以Connect()Disconnect()GrabTags()来简单地更新 Tags 的属性,即检测到的Tags的集合:

public class RFIDReader
{
    // Container property for detected tags
    public List<Tag> Tags { get; private set; }

    // Connection Methods
    public void Connect(string ipAdress) { }
    public void Disconnect() { }

    // Polling Method
    public void GrabTags()
    {
        // Tags = GrabMeSomeTags()
    }
}

In the console application example below, (again all pseudo, no error checking etc), I have chosen to use a System.Timers.Timer to "Poll" all three readers in turn, every 2000ms:在下面的控制台应用程序示例中,(同样都是伪的,没有错误检查等),我选择使用System.Timers.Timer轮流“轮询”所有三个阅读器,每 2000 毫秒:

class Program
{
    // An array of your three RFID readers
    static RFIDReader[] rFIDReaders = new RFIDReader[3];
    // An array of the three IPAdresses
    static string[] ipAdress = new string[3]
    {
            "192.168.4.100",
            "192.168.4.101",
            "192.168.4.102"
    };

    static void Main(string[] args)
    {
        // A timer for polling the readers, every 2000ms (2 seconds)
        Timer pollTimer = new Timer(2000);
        pollTimer.Elapsed += PollTimer_Elapsed;

        // Setup
        for(int i = 0; i < 3; i++)
        {
            Console.WriteLine($"Connecting to reader {i} at IPAdress {ipAdress[i]}");
            rFIDReaders[i].Connect(ipAdress[i]);
        }

        // Start Timer
        Console.WriteLine("Starting reader polling...");
        pollTimer.Start();
        Console.WriteLine("System Running.");

        // Wait for user to Exit
        Console.WriteLine("Press any ket to exit...");
        Console.ReadKey();

        // Stop the timer
        pollTimer.Stop();

        // Disconnect the RFIDReaders
        foreach (RFIDReader rFIDReader in rFIDReaders)
        {
            rFIDReader.Disconnect();
        }
    }

    private static void PollTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        foreach(RFIDReader rFIDReader in rFIDReaders)
        {
            rFIDReader.GrabTags();
        }
    }
}

This solution will hold on Console.ReadKey();此解决方案将保留Console.ReadKey(); , mean while, every two seconds the Timer.Elapsed event invokes the handler PollTimer_Elapsed . ,同时,每两秒Timer.Elapsed事件调用处理程序PollTimer_Elapsed You could use any mechanism for your application continuing of course (perhaps you have some Form/WPF GUI?), but key is that the timer will regularly have the RFIDReaders update they're own collection of Tag for your analysis.当然,您可以使用任何机制来继续您的应用程序(也许您有一些 Form/WPF GUI?),但关键是计时器会定期让RFIDReaders更新它们自己的Tag集合以供您分析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM