簡體   English   中英

如何在中使用MSR206 MagStripe R / W

[英]How to Use MSR206 MagStripe R/W in

關於如何與這些設備接口存在很多問題,很少有有用的答案。 我編寫了一個使用該設備進行讀寫的應用程序。 我將簡要介紹與該串行設備交互所需的方法。


將其視為普通的串行設備,然后分別發送所有命令。 我編寫了一個使用此設備讀取/寫入徽章的應用程序。 創建一個串行端口和一個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事件並產生結果。 它會發送一個反映成功的char序列,或者發送失敗的char序列。 互聯網上有關該設備的程序員手冊非常有用。

請意識到,我發布的不是您可以復制/粘貼到您的代碼中的東西,並且突然有了可以正常使用的設備。 盡管您可以使用部分代碼來做到這一點(例如,寫功能),但是您將需要根據自己的情況自定義過程。 我只是想向您展示使用該設備的交易順序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM