A serial RFID reader can be read using DataReader with the help of SerialPort that is being used to find the connected port in the system.
private SerialDevice _serialPort;
private DataReader _dataReader;
private ObservableCollection<DeviceInformation> _deviceList;
private CancellationTokenSource _readCancellationTokenSource;
Then we need to configure the serial settings
try
{
string devSel = SerialDevice.GetDeviceSelector();
var dis = await DeviceInformation.FindAllAsync(devSel);
if (dis.Count > 0)
{
DeviceInformation entry = (DeviceInformation)dis[0];
this._serialPort = await SerialDevice.FromIdAsync(entry.Id);
this._serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
this._serialPort.BaudRate = 9600;
this._serialPort.DataBits = 8;
this._serialPort.StopBits = SerialStopBitCount.One;
this._serialPort.Parity = SerialParity.None;
this._serialPort.Handshake = SerialHandshake.None;
this._readCancellationTokenSource = new CancellationTokenSource();
}
} catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
And from that serial port InputStream, we can read the data from the serial device.
this._dataReader = new DataReader(this._serialPort.InputStream);
Task<UInt32> loadAsyncTask;
uint ReadBufferLength = 1024;
cancellationToken.ThrowIfCancellationRequested();
// create task and wait for the data on the input stream
loadAsyncTask = this._dataReader.LoadAsync(ReadBufferLength).AsTask(cancellationToken);
UInt32 bytesRead = await loadAsyncTask;
// this line will return the data from the serial RFID device
this._dataReader.ReadString(bytesRead);
Reference link: https://github.com/phanithken/RFIDReader