关于如何与这些设备交互的问题非常多,有用的答案也很少。我编写了一个应用程序,该应用程序将该设备用于读取和写入目的。我将简要强调与此串行设备交互所需的方法。
问问题
1266 次
1 回答
3
将此视为普通串行设备并单独发送所有命令。我编写了一个使用此设备读取/写入徽章的应用程序。创建一个串行端口和一个 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
请记住,一旦刷卡(成功与否),设备就会触发 datareceived 事件并产生结果。它要么发送一个反映成功的字符序列,要么发送一个失败的字符序列。互联网上针对此设备的程序员手册非常有用。
请注意,我发布的内容不是您可以复制/粘贴到您的代码中并突然拥有工作设备的内容。尽管您可以使用其中的一部分(例如 write 函数)来执行此操作,但您需要根据您的情况自定义过程。我只是想向您展示使用该设备的交易顺序。
于 2015-09-17T11:45:03.373 回答