I came across this article with a neat trick, I thought I would share with you:
https://www.codemag.com/article/0611061/Fun-with-RFIDThe RFID reader send 12-byte datagram with a format that looks like :
<LF>123456789A<CR>
Since serial devices send their datagram in more than one blocks, his neat trick was
using a TextBox control (with the Multiline property set to True), then append all data until
the last line is an empty string. then data( TagID) is in the second to the last line.
To check for this condition, he use the Text changed event.
Public Class Form1
Private WithEvents serialPort As New IO.Ports.SerialPort
Private tagID As String = String.Empty
Private timeRecorded As DateTime = Now
Const COM As String = "COM3"
Const FILE_NAME As String = "C:\Attendance.csv"
Const INTERVAL As Integer = 3
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Interval = INTERVAL * 1000
If serialPort.IsOpen Then
serialPort.Close()
End If
Try
With serialPort
.PortName = COM
.BaudRate = 2400
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
.Handshake = IO.Ports.Handshake.None
End With
serialPort.Open()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serialPort.DataReceived
txtTagID.BeginInvoke(New myDelegate(AddressOf updateTextBox), New Object() {})
End Sub
Public Delegate Sub myDelegate()
Public Sub updateTextBox()
' for receiving plain ASCII text
With txtTagID
.AppendText(serialPort.ReadExisting)
.ScrollToCaret()
End With
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
End Sub
Private Sub txtTagID_TextChanged(sender As Object, e As EventArgs) Handles txtTagID.TextChanged
If txtTagID.Lines(txtTagID.Lines.Length - 1) = String.Empty Then
Dim temptagID As String = txtTagID.Lines(txtTagID.Lines.Length - 2)
Dim tp As TimeSpan = Now.Subtract(timeRecorded)
Dim timeInterval As Double = tp.Ticks / TimeSpan.TicksPerSecond
If (temptagID = tagID) And timeInterval < INTERVAL Then
Exit Sub
End If
tagID = temptagID
timeRecorded = Now
End If
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
serialPort.Close()
End Sub
End Class