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

需要有mwrf_bj.dll

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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