读取连接到Raspberry Pi的两个RFID RC522读卡器的序列号,时间戳等数据

时间:2017-08-18 08:19:19

标签: javascript node.js raspberry-pi rfid

有人可以帮我看看如何读取连接到Raspbery Pi的两个rfid阅读器的序列号,时间戳。在python中有一些解决方案,c#.Net但不是javascript。

我正在尝试使用Node Js,但没有找到任何有用的资源来读取两个rfid读者的数据。

非常感谢提前

2 个答案:

答案 0 :(得分:1)

要从多个RFID阅读器读取数据,您可以使用以下内容:

<强> 1。用于从节点读取数据:

有一些项目的阅读涉及从节点读取RFID标签,例如herehere

另外还有一些npm包,例如rfidreader

甚至有一些特别针对RC522,例如rc522-c7z

<强> 2。对于多个读者:

每个读卡器都应由唯一的串口提供。这需要传递给步骤1中的代码。

答案 1 :(得分:0)

这是在狂暴的pi上Windows 10 uwp应用程序的示例。您必须在uwp概念上谨慎使用异步编程。

using System;   
using System.Collections.ObjectModel;   
using System.Threading;   
using System.Threading.Tasks;   
using Windows.Devices.Enumeration;   
using Windows.Devices.SerialCommunication;   
using Windows.Storage.Streams;   
using Windows.UI.Xaml;   
using Windows.UI.Xaml.Controls;  

namespace Prova_comunicazione_seriale   
{   

     public sealed partial class MainPage : Page   
     {   

         private SerialDevice serialPort = null;  
        DataWriter dataWriteObject = null;   
         DataReader dataReaderObject = null;  
        private ObservableCollection<DeviceInformation> listOfDevices;   
         private CancellationTokenSource ReadCancellationTokenSource;  
        public MainPage()   
         {   
             InitializeComponent();  
            btnAccendiled.IsEnabled = false;   
             btnSpegniled.IsEnabled = false;   
             listOfDevices = new ObservableCollection<DeviceInformation>();   
             ListAvailablePorts();   
         }  

         private async void ListAvailablePorts()   
         {   
             try   
             {   
                 string aqs = SerialDevice.GetDeviceSelector();   
                 var dis = await DeviceInformation.FindAllAsync(aqs);  
                for (int i = 0; i < dis.Count; i++)   
                 {   
                     listOfDevices.Add(dis[i]);   
                 }  
                lstSerialDevices.ItemsSource = listOfDevices;   
                 btnAccendiled.IsEnabled = true;   
                 btnSpegniled.IsEnabled = true;   
                 lstSerialDevices.SelectedIndex = -1;   
             }   
             catch (Exception ex)   
             {   
                 tbkAllarmi.Text = ex.Message;   
             }   
         }  

         private async void ButtonClick(object sender, RoutedEventArgs e)   
         {   
             var buttonClicked = sender as Button;  
            switch(buttonClicked.Name)   
             {   
                 case "btnSerialConnect":   
                     SerialPortConfiguration();   
                     break;  
                case "btnSerialDisconnect":   
                     SerialPortDisconnect();   
                     break;  
                case "btnAccendiled":   
                     if (serialPort != null)   
                     {   
                         dataWriteObject = new DataWriter(serialPort.OutputStream);   
                         await ManageLed("2");   
                     }  
                    if (dataWriteObject != null)   
                     {   
                         dataWriteObject.DetachStream();   
                         dataWriteObject = null;   
                     }  
                    break;  
                case "btnSpegniled":   
                     if (serialPort != null)   
                     {   
                         dataWriteObject = new DataWriter(serialPort.OutputStream);   
                         await ManageLed("1");   
                     }  
                    if (dataWriteObject != null)   
                     {   
                         dataWriteObject.DetachStream();   
                         dataWriteObject = null;   
                     }  
                    break;  
                case "btnPulse1000ms":   
                     if (serialPort != null)   
                     {   
                         dataWriteObject = new DataWriter(serialPort.OutputStream);   
                         await ManageLed("3");   
                     }  
                    if (dataWriteObject != null)   
                     {   
                         dataWriteObject.DetachStream();   
                         dataWriteObject = null;   
                     }  
                    break;  
                case "btnPulse2000ms":   
                     if (serialPort != null)   
                     {   
                         dataWriteObject = new DataWriter(serialPort.OutputStream);   
                         await ManageLed("4");   
                     }  
                    if (dataWriteObject != null)   
                     {   
                         dataWriteObject.DetachStream();   
                         dataWriteObject = null;   
                     }  
                    break;   
             }   
         }  

         private  async void SerialPortConfiguration()   
         {   
             var selection = lstSerialDevices.SelectedItems;  
            if (selection.Count <= 0)   
             {   
                 tbkAllarmi.Text = "Seleziona un oggetto per la connessione seriale!";   
                 return;   
             }  
            DeviceInformation entry = (DeviceInformation)selection[0];  
            try   
             {   
                 serialPort = await SerialDevice.FromIdAsync(entry.Id);   
                 serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);   
                 serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);   
                 serialPort.BaudRate = 9600;   
                 serialPort.Parity = SerialParity.None;   
                 serialPort.StopBits = SerialStopBitCount.One;   
                 serialPort.DataBits = 8;   
                 serialPort.Handshake = SerialHandshake.None;   
                 tbkAllarmi.Text = "Porta seriale correttamente configurata!";  
                ReadCancellationTokenSource = new CancellationTokenSource();  
                Listen();   
             }  
            catch (Exception ex)   
             {   
                 tbkAllarmi.Text = ex.Message;   
                 btnAccendiled.IsEnabled = false;   
                 btnSpegniled.IsEnabled = false;   
             }   
         }  

         private void SerialPortDisconnect()   
         {   
             try   
             {   
                 CancelReadTask();   
                 CloseDevice();   
                 ListAvailablePorts();   
             }   
             catch (Exception ex)   
             {   
                 tbkAllarmi.Text = ex.Message;   
             }   
         }  

         private async Task ManageLed(string value)   
         {   
             var accendiLed = value;  
            Task<UInt32> storeAsyncTask;  
            if (accendiLed.Length != 0)   
             {   
                 dataWriteObject.WriteString(accendiLed);                  
                storeAsyncTask = dataWriteObject.StoreAsync().AsTask();  
                UInt32 bytesWritten = await storeAsyncTask;   
                 if (bytesWritten > 0)   
                 {   
                     tbkAllarmi.Text = "Valore inviato correttamente";   
                 }   
             }  
            else   
             {   
                 tbkAllarmi.Text = "Nessun valore inviato";   
             }   
         }  

         private async void Listen()   
         {   
             try   
             {   
                 if (serialPort != null)   
                 {   
                     dataReaderObject = new DataReader(serialPort.InputStream);  
                    while (true)   
                     {   
                         await ReadData(ReadCancellationTokenSource.Token);   
                     }   
                 }   
             }  
            catch (Exception ex)   
             {   
                 tbkAllarmi.Text = ex.Message;  
                if (ex.GetType().Name == "TaskCanceledException")   
                 {   
                     CloseDevice();   
                 }  
                else   
                 {   
                     tbkAllarmi.Text = "Task annullato";   
                 }   
             }  
            finally   
             {   
                 if (dataReaderObject != null)   
                 {   
                     dataReaderObject.DetachStream();   
                     dataReaderObject = null;   
                 }   
             }   
         }  

         private async Task ReadData(CancellationToken cancellationToken)   
         {   
             Task<UInt32> loadAsyncTask;  
            uint ReadBufferLength = 1024;  
            cancellationToken.ThrowIfCancellationRequested();  
            dataReaderObject.InputStreamOptions = InputStreamOptions.Partial;  
            loadAsyncTask = dataReaderObject.LoadAsync(ReadBufferLength).AsTask(cancellationToken);  
            UInt32 bytesRead = await loadAsyncTask;   
             if (bytesRead > 0)   
             {   
                 tbkStatusLed.Text = dataReaderObject.ReadString(bytesRead);                 
             }   
         }  

         private void CancelReadTask()   
         {   
             if (ReadCancellationTokenSource != null)   
             {   
                 if (!ReadCancellationTokenSource.IsCancellationRequested)   
                 {   
                     ReadCancellationTokenSource.Cancel();   
                 }   
             }   
         }  

         private void CloseDevice()   
         {   
             if (serialPort != null)   
             {   
                 serialPort.Dispose();   
             }   
             serialPort = null;  
            btnAccendiled.IsEnabled = false;   
             btnSpegniled.IsEnabled = false;   
             listOfDevices.Clear();   
         }   
     }   
}
}