Imports System.Collections.Generic Imports System.Windows.Forms Imports System.Text Imports System.Runtime.InteropServices ''' <summary> ''' DUKa 的摘要说明 ''' </summary> Public Class DUKa ' ' TODO: 在此处添加构造函数逻辑 ' Public Sub New() End Sub #Region "对USB接口的使用(PHILIPH卡)" <DllImport("dcrf32.dll")> _ Public Shared Function dc_init(ByVal port As Int32, ByVal baud As Long) As Integer End Function '初试化 <DllImport("dcrf32.dll")> _ Public Shared Function dc_exit(ByVal icdev As Integer) As Short End Function <DllImport("dcrf32.dll")> _ Public Shared Function dc_reset(ByVal icdev As Integer, ByVal sec As UInteger) As Short End Function <DllImport("dcrf32.dll")> _ Public Shared Function dc_request(ByVal icdev As Integer, ByVal _Mode As Char, ByRef TagType As UInteger) As Short End Function <DllImport("dcrf32.dll")> _ Public Shared Function dc_card(ByVal icdev As Integer, ByVal _Mode As Char, ByRef Snr As ULong) As Short End Function <DllImport("dcrf32.dll")> _ Public Shared Function dc_halt(ByVal icdev As Integer) As Short End Function <DllImport("dcrf32.dll")> _ Public Shared Function dc_anticoll(ByVal icdev As Integer, ByVal _Bcnt As Char, ByRef IcCardNo As ULong) As Short End Function <DllImport("dcrf32.dll")> _ Public Shared Function dc_beep(ByVal icdev As Integer, ByVal _Msec As UInteger) As Short End Function <DllImport("dcrf32.dll")> _ Public Shared Function dc_authentication(ByVal icdev As Integer, ByVal _Mode As Integer, ByVal _SecNr As Integer) As Short End Function <DllImport("dcrf32.dll")> _ Public Shared Function dc_load_key(ByVal icdev As Integer, ByVal mode As Integer, ByVal secnr As Integer, <[In]()> ByVal nkey As Byte()) As Short End Function '密码装载到读写模块中 <DllImport("dcrf32.dll")> _ Public Shared Function dc_load_key_hex(ByVal icdev As Integer, ByVal mode As Integer, ByVal secnr As Integer, ByVal nkey As String) As Short End Function '密码装载到读写模块中 <DllImport("dcrf32.dll")> _ Public Shared Function dc_write(ByVal icdev As Integer, ByVal adr As Integer, <[In]()> ByVal sdata As Byte()) As Short End Function '向卡中写入数据 <DllImport("dcrf32.dll")> _ Public Shared Function dc_write(ByVal icdev As Integer, ByVal adr As Integer, <[In]()> ByVal sdata As String) As Short End Function '向卡中写入数据 <DllImport("dcrf32.dll")> _ Public Shared Function dc_write_hex(ByVal icdev As Integer, ByVal adr As Integer, <[In]()> ByVal sdata As String) As Short End Function '向卡中写入数据(转换为16进制) <DllImport("dcrf32.dll")> _ Public Shared Function dc_read(ByVal icdev As Integer, ByVal adr As Integer, <Out()> ByVal sdata As Byte()) As Short End Function <DllImport("dcrf32.dll")> _ Public Shared Function dc_read(ByVal icdev As Integer, ByVal adr As Integer, <MarshalAs(UnmanagedType.LPStr)> ByVal sdata As StringBuilder) As Short End Function '从卡中读数据 <DllImport("dcrf32.dll")> _ Public Shared Function dc_read_hex(ByVal icdev As Integer, ByVal adr As Integer, <MarshalAs(UnmanagedType.LPStr)> ByVal sdata As StringBuilder) As Short End Function '从卡中读数据(转换为16进制) <DllImport("dcrf32.dll")> _ Public Shared Function a_hex(ByVal oldValue As String, ByRef newValue As String, ByVal len As Int16) As Integer End Function '普通字符转换成十六进制字符 <DllImport("dcrf32.dll")> _ Public Shared Sub hex_a(ByRef oldValue As String, ByRef newValue As String, ByVal len As Integer) End Sub '十六进制字符转换成普通字符 #End Region Public Shared Function WriteIn(ByVal str As String) As Boolean Dim _icdev As Int32 = -1 Try If _icdev < 0 Then _icdev = dc_init(100, 115200) End If Dim st As Integer '1.初始化 If _icdev > 0 Then Else dc_exit(_icdev) Return False End If '蜂鸣 st = dc_beep(_icdev, 10) If st = 0 Then Else dc_exit(_icdev) Return False End If Dim icCardNo As ULong = 0 Dim tt As Char = ChrW(0) Dim ss As UInteger = 0 st = dc_reset(_icdev, ss) st = dc_card(_icdev, tt, icCardNo) If icCardNo <> 0 Then Else dc_exit(_icdev) Return False End If Dim hexkey As Byte() = New Byte(5) {&HFF, &HFF, &HFF, &HFF, &HFF, &HFF}
st = dc_load_key(_icdev, 0, 1, hexkey) '核对密码 Dim sector As Integer = 1 st = dc_authentication(_icdev, 0, sector) If st = 0 Then Else dc_exit(_icdev) Return False End If Dim address As Integer = 1 Dim data32 As Byte() data32 = New Byte(15) {&H6A, &HC2, &H92, &HFA, &HA1, &H31, _ &H5B, &H4D, &H6A, &HC2, &H92, &HFA, _ &HA1, &H31, &H5B, &H4D} '= "12345678901234561234567890123456"; Dim data32_hex As String = "".PadLeft(32, " "c) address = sector * 4 + 2 '写卡操作 data32_hex = str st = dc_write(_icdev, address, data32_hex) If st = 0 Then dc_exit(_icdev) Else dc_exit(_icdev) Return False End If dc_exit(_icdev) Return True Catch dc_exit(_icdev) Return False End Try End Function
|