如何使用 MSR206 MagStripe R/W
How to Use MSR206 MagStripe R/W in
有很多关于如何与这些设备交互的问题,但很少有有用的答案。我编写了一个应用程序,将此设备用于读写目的。我将简要强调与此串行设备交互所需的方法。
将其视为普通串行设备并单独向其发送所有命令。我已经使用此设备编写了 reads/writes 徽章的应用程序。创建串行端口和 DataReceived 事件处理程序以及 ReceivedText 方法来处理正在读取的数据。
Private Sub SerialPort1_DataReceived(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs)
System.Threading.Thread.Sleep(700)'delay to try to wait till all datareceived
ReceivedText(SerialPort1.ReadExisting)
End Sub
然后您需要向设备发送读取命令,以便它进入读取模式(当处于读取模式时,它会在您刷卡时触发 DataReceived 事件)。
Public Sub SendReadCommad()
Dim bytes() As Byte = {&H1B, &H72} 'Hex command to put device in read mode
SerialPort1.Write(bytes, 0, 2) 'Sends command to device.
End Sub
此时,当您刷卡时,datareceived 事件将触发并将字符串数据传递给您的方法以处理数据。在我的例子中,我将接收到的文本附加到一个隐藏的文本框中(它像这样附加是因为接收到的文本事件很可能在一次滑动时触发多次,每次触发都会接收到数据片段。当然你会想将这些数据组合成最终结果。
Public Sub ReceivedText(ByVal [text] As String)
If Not tbxHiddenInput.Dispatcher.CheckAccess() Then
tbxHiddenInput.Dispatcher.BeginInvoke(New SetTextCallBack(AddressOf ReceivedText), ([text]))
Else
Dim charArray As Char() = [text].ToCharArray
Dim commandArray As String() = Nothing
Dim i As Int16 = 0
Dim hexVal As String = ""
For Each c As Char In charArray
hexVal = Convert.ToString(Convert.ToInt32(c), 16)
tbxHiddenInput.AppendText(hexVal)
Next
'this section below is used to evaluate the Chars read to determine what type of data
'the device sent. This is need because the device sends status information with
'the result of things like a badge Write attempt.
If charArray(0) = Chr(27) Then
Select Case charArray(1)
Case Chr(115)
ReadTrackData(charArray) 'Method where I actually parse the track data out in the way I wan't for my app. This will be custom for you.
Case Chr(48)
MessageBox.Show("Badge write status: Success!")
Case Else
If isWriteCommand = True Then
MessageBox.Show("Badge write status: Failure!")
SendResetCommad()
myMagWindow.WriteToMagStripe(myMagWindow.tbxTrack1.Text, myMagWindow.tbxTrack2.Text, myMagWindow.tbxTrack3.Text)
myMagWindow.PictureBox3.Visibility = Windows.Visibility.Visible
Else
MessageBox.Show("Badge read status: Failure!")
SendResetCommad() 'Indentical to SendReadCommand, except the Hex data sent. This puts device in normal mode.
SendReadCommad()
End If
End Select
End If
End If
End Sub
最后,写磁条也是一样的...
Public Sub WriteToMagStripe(ByVal track1 As String, track2 As String, track3 As String)
isWriteCommand = True
Dim commandHeader() As Byte = {&H1B, &H77, &H1B, &H73, &H1B, &H1} 'Series of hex chars that tell the reader to enter write mode.
Dim bytes(4096) As Byte
Dim b As Int16 = 0
'Build the byte array beginning with the write header
For Each c As Byte In commandHeader
bytes(b) = c
b += 1
Next
'Append track data to the byte array
For Each c As Char In track1
bytes(b) = Convert.ToInt16(c)
b += 1
Next
'at end of track1 data append track seperator char sequence
bytes(b) = &H1B
b += 1
bytes(b) = &H2
b += 1
For Each c As Char In track2
bytes(b) = Convert.ToInt16(c)
b += 1
Next
'at end of track2 data append track seperator char sequence
bytes(b) = &H1B
b += 1
bytes(b) = &H3
b += 1
For Each c As Char In track3
bytes(b) = Convert.ToInt16(c)
b += 1
Next
'at end of track1 data append data end char sequence
bytes(b) = &H3F
b += 1
bytes(b) = &H1C
ReDim Preserve bytes(b)
SerialPort1.Write(bytes, 0, bytes.Length)
End Sub
请记住,一旦刷卡(成功与否),设备就会触发数据接收事件并产生结果。它要么发送一个反映成功的字符序列,要么发送一个失败的字符序列。
互联网上针对此设备的程序员手册非常有用。
请注意,我发布的内容并不是您可以 copy/paste 进入您的代码并突然拥有一个工作设备的东西。虽然您可以使用它的一部分(例如写函数)来完成此操作,但您需要根据您的情况自定义过程。我只是想向您展示使用该设备的交易顺序。
有很多关于如何与这些设备交互的问题,但很少有有用的答案。我编写了一个应用程序,将此设备用于读写目的。我将简要强调与此串行设备交互所需的方法。
将其视为普通串行设备并单独向其发送所有命令。我已经使用此设备编写了 reads/writes 徽章的应用程序。创建串行端口和 DataReceived 事件处理程序以及 ReceivedText 方法来处理正在读取的数据。
Private Sub SerialPort1_DataReceived(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs)
System.Threading.Thread.Sleep(700)'delay to try to wait till all datareceived
ReceivedText(SerialPort1.ReadExisting)
End Sub
然后您需要向设备发送读取命令,以便它进入读取模式(当处于读取模式时,它会在您刷卡时触发 DataReceived 事件)。
Public Sub SendReadCommad()
Dim bytes() As Byte = {&H1B, &H72} 'Hex command to put device in read mode
SerialPort1.Write(bytes, 0, 2) 'Sends command to device.
End Sub
此时,当您刷卡时,datareceived 事件将触发并将字符串数据传递给您的方法以处理数据。在我的例子中,我将接收到的文本附加到一个隐藏的文本框中(它像这样附加是因为接收到的文本事件很可能在一次滑动时触发多次,每次触发都会接收到数据片段。当然你会想将这些数据组合成最终结果。
Public Sub ReceivedText(ByVal [text] As String)
If Not tbxHiddenInput.Dispatcher.CheckAccess() Then
tbxHiddenInput.Dispatcher.BeginInvoke(New SetTextCallBack(AddressOf ReceivedText), ([text]))
Else
Dim charArray As Char() = [text].ToCharArray
Dim commandArray As String() = Nothing
Dim i As Int16 = 0
Dim hexVal As String = ""
For Each c As Char In charArray
hexVal = Convert.ToString(Convert.ToInt32(c), 16)
tbxHiddenInput.AppendText(hexVal)
Next
'this section below is used to evaluate the Chars read to determine what type of data
'the device sent. This is need because the device sends status information with
'the result of things like a badge Write attempt.
If charArray(0) = Chr(27) Then
Select Case charArray(1)
Case Chr(115)
ReadTrackData(charArray) 'Method where I actually parse the track data out in the way I wan't for my app. This will be custom for you.
Case Chr(48)
MessageBox.Show("Badge write status: Success!")
Case Else
If isWriteCommand = True Then
MessageBox.Show("Badge write status: Failure!")
SendResetCommad()
myMagWindow.WriteToMagStripe(myMagWindow.tbxTrack1.Text, myMagWindow.tbxTrack2.Text, myMagWindow.tbxTrack3.Text)
myMagWindow.PictureBox3.Visibility = Windows.Visibility.Visible
Else
MessageBox.Show("Badge read status: Failure!")
SendResetCommad() 'Indentical to SendReadCommand, except the Hex data sent. This puts device in normal mode.
SendReadCommad()
End If
End Select
End If
End If
End Sub
最后,写磁条也是一样的...
Public Sub WriteToMagStripe(ByVal track1 As String, track2 As String, track3 As String)
isWriteCommand = True
Dim commandHeader() As Byte = {&H1B, &H77, &H1B, &H73, &H1B, &H1} 'Series of hex chars that tell the reader to enter write mode.
Dim bytes(4096) As Byte
Dim b As Int16 = 0
'Build the byte array beginning with the write header
For Each c As Byte In commandHeader
bytes(b) = c
b += 1
Next
'Append track data to the byte array
For Each c As Char In track1
bytes(b) = Convert.ToInt16(c)
b += 1
Next
'at end of track1 data append track seperator char sequence
bytes(b) = &H1B
b += 1
bytes(b) = &H2
b += 1
For Each c As Char In track2
bytes(b) = Convert.ToInt16(c)
b += 1
Next
'at end of track2 data append track seperator char sequence
bytes(b) = &H1B
b += 1
bytes(b) = &H3
b += 1
For Each c As Char In track3
bytes(b) = Convert.ToInt16(c)
b += 1
Next
'at end of track1 data append data end char sequence
bytes(b) = &H3F
b += 1
bytes(b) = &H1C
ReDim Preserve bytes(b)
SerialPort1.Write(bytes, 0, bytes.Length)
End Sub
请记住,一旦刷卡(成功与否),设备就会触发数据接收事件并产生结果。它要么发送一个反映成功的字符序列,要么发送一个失败的字符序列。 互联网上针对此设备的程序员手册非常有用。
请注意,我发布的内容并不是您可以 copy/paste 进入您的代码并突然拥有一个工作设备的东西。虽然您可以使用它的一部分(例如写函数)来完成此操作,但您需要根据您的情况自定义过程。我只是想向您展示使用该设备的交易顺序。