首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用MSR206 MagStripe R/W

如何使用MSR206 MagStripe R/W
EN

Stack Overflow用户
提问于 2015-09-17 11:45:03
回答 1查看 1.3K关注 0票数 1

有很多关于如何与这些设备接口的问题,很少有有用的答案。我已经编写了一个应用程序,它将这个设备用于读写目的。我将简要地强调与这个串行设备交互所需的方法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-17 11:45:03

这是一个正常的串行设备,并单独发送所有命令。我已经编写了一个应用程序,它使用这个设备读写徽章。创建一个串口和一个DataReceived事件处理程序以及一个ReceivedText方法来处理正在读取的数据。

代码语言:javascript
复制
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

然后,您需要向设备发送read命令,使其进入读模式(在读取模式下,当您刷卡时将触发DataReceived事件)。

代码语言:javascript
复制
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

此时,当您刷卡时,数据接收事件将触发并将字符串数据传递给您的方法来处理数据。在我的例子中,我将接收到的文本附加到一个隐藏的文本框中(类似于这样,因为接收到的文本事件很可能在每次滑动时多次触发,每个触发器上都会接收到一些数据。当然,您希望将这些数据组合到最终结果中。

代码语言:javascript
复制
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

最后,写到磁条也是一样的.

代码语言:javascript
复制
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

请记住,一旦刷卡(成功与否)设备触发数据接收事件的结果。它要么发送反映成功的字符序列,要么发送失败的字符序列。这个设备在互联网上的程序员手册是非常有用的。

请注意,我已经发布的东西,您将无法复制/粘贴到您的代码,并突然有一个工作的设备。虽然您可能可以使用它的一部分(例如写函数)来完成这个任务,但是您需要根据您的情况定制这些过程。我只想向您展示使用该设备的事务处理顺序。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32629536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档