明华 RF-35 读写器 vb.net 源代码

需要有mwrf_bj.dll

Imports System.Runtime.InteropServices

Module mwrf_dll

    <DllImport("mwrf_bj.dll")>
    Function rf_beep(ByVal icdev As Integer, ByVal Msec As UInteger) As Byte

    End Function

    <DllImport("mwrf_bj.dll")>
    Function Open_Device(ByVal port As Integer, ByVal baud As UInteger) As Integer

    End Function

    <DllImport("mwrf_bj.dll")>
    Function Close_Device(ByVal icdev As Integer) As Byte

    End Function

    <DllImport("mwrf_bj.dll")>
    Function rf_authentication(ByVal icdev As Integer, ByVal Mode As Byte, ByVal SecNr As Byte) As Byte

    End Function

    <DllImport("mwrf_bj.dll")>
    Function rf_load_key(ByVal icdev As Integer, ByVal Mode As Byte, ByVal SecNr As Byte, ByVal NKey As IntPtr) As Byte

    End Function

    <DllImport("mwrf_bj.dll")>
    Function rf_load_key_hex(ByVal icdev As Integer, ByVal Mode As Byte, ByVal SecNr As Byte, ByRef NKey As String) As Byte

    End Function

    <DllImport("mwrf_bj.dll")>
    Function rf_halt(ByVal icdev As Integer) As Byte

    End Function

    '<DllImport("mwrf_bj.dll")>
    'Function rf_request(ByVal icdev As Integer, ByVal Mode As Byte, ByRef Type As UInteger) As Byte

    'End Function

    '<DllImport("mwrf_bj.dll")>
    'Function rf_anticoll(ByVal icdev As Integer, ByVal Bcnt As Byte, ByRef Snr As ULong) As Byte

    'End Function

    '<DllImport("mwrf_bj.dll")>
    'Function rf_select(ByVal icdev As Integer, ByRef Snr As UInteger, ByRef size As Byte) As Byte

    'End Function

    '<DllImport("mwrf_bj.dll")>
    'Function rf_HL_authentication(ByVal icdev As Integer, ByVal reqmode As Byte, ByVal snr As ULong, ByVal authmode As Byte, ByVal secnr As Byte) As Byte

    'End Function

    '<DllImport("mwrf_bj.dll")>
    'Function rf_HL_read(ByVal icdev As Integer, ByVal Mode As Byte, ByVal Adr As Byte, ByVal snr As ULong, ByVal Data As IntPtr, ByRef NSnr As ULong) As Byte

    'End Function

    <DllImport("mwrf_bj.dll")>
    Function rf_read(ByVal icdev As Integer, ByVal Adr As Byte, ByVal Data As IntPtr) As Byte

    End Function

    <DllImport("mwrf_bj.dll")>
    Function rf_write(ByVal icdev As Integer, ByVal Adr As Byte, ByVal Data As IntPtr) As Byte

    End Function

    <DllImport("mwrf_bj.dll")>
    Function rf_card(ByVal icdev As Integer, ByVal Mode As Byte, ByRef Snr As ULong) As Byte

    End Function

End Module

Class mwrf
    Implements IDisposable

    Dim icdev As Integer
    Dim snr As ULong

    Sub New(ByVal port As Integer, Optional ByVal baud As Integer = 115200)
        icdev = Open_Device(port, baud)
        If icdev < 0 Then
            Throw New Exception("Open_Device Error")
        End If
    End Sub

    Sub Beep(ByVal ms As Integer)
        rf_beep(icdev, ms / 10)
    End Sub

    Sub CheckErr(ByVal no As Integer)
        If no = 0 Then
            Exit Sub
        End If
        Dim errs = Split(My.Resources.Err, vbCrLf)
        Dim des = ""
        For i = 0 To errs.Length - 1
            If no.ToString = errs(i) Then
                des = errs(i + 1)
                Exit For
            End If
        Next
        Throw New Exception("#" & no.ToString & " " & des)
    End Sub

    Sub Close()
        CheckErr(Close_Device(icdev))
    End Sub

    Sub Halt()
        CheckErr(rf_halt(icdev))
    End Sub

    Sub InitCard(ByVal Adr As Integer, ByVal key() As Byte)
        Dim k As IntPtr = Marshal.UnsafeAddrOfPinnedArrayElement(key, 0)
        CheckErr(rf_load_key(icdev, 0, CInt(Adr / 4), k))

        CheckErr(rf_card(icdev, 1, snr))

        CheckErr(rf_authentication(icdev, 0, CInt(Adr / 4)))
    End Sub

    Function Read(ByVal Adr As Integer, ByVal key() As Byte) As Byte()
        InitCard(Adr, key)

        Dim data(15) As Byte
        Dim d = Marshal.AllocHGlobal(16)
        Dim ret = rf_read(icdev, Adr, d)
        CheckErr(ret)
        Marshal.Copy(d, data, 0, 16)
        Return data
    End Function

    Sub Write(ByVal Adr As Integer, ByVal data() As Byte, ByVal key() As Byte)
        InitCard(Adr, key)
        Dim ret = rf_write(icdev, Adr, Marshal.UnsafeAddrOfPinnedArrayElement(data, 0))
        CheckErr(ret)

    End Sub

    Sub Dispose() Implements IDisposable.Dispose
        Close()
    End Sub

End Class

测试代码:
Using dev As New mwrf(3)
Do While True
Try
dev.Write(8, {&H22, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H22}, {&HFF, &HFF, &HFF, &HFF, &HFF, &HFF})
dev.Beep(10)
Dim x = dev.Read(0, {&HFF, &HFF, &HFF, &HFF, &HFF, &HFF})
dev.Beep(10)
dev.Halt()
Catch ex As Exception
‘ MsgBox(ex.ToString, MsgBoxStyle.Critical, “”)
dev.Beep(1000)
End Try
Loop
End Using

发表评论

注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

:wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :?: :-| :-x :-o :-P :-D :-? :) :( :!: 8-O 8)

本文链接:https://twd2.me/archives/1949QrCode