本示例使用设备:Android Linux RFID读写器NFC发卡器WEB可编程NDEF文本/智能海报/-淘宝网 (taobao.com)
函数声明
vbnet
Module Module1
'读卡函数声明
Public Declare Function piccreadex Lib "OUR_MIFARE.dll" (ByVal ctrlword As Byte, ByRef serial As Byte, ByVal area As Byte, ByVal keyA1B0 As Byte, ByRef picckey As Byte, ByRef piccdata0_2 As Byte) As Byte
'写卡函数声明
Public Declare Function piccwriteex Lib "OUR_MIFARE.dll" (ByVal ctrlword As Byte, ByRef serial As Byte, ByVal area As Byte, ByVal keyA1B0 As Byte, ByRef picckey As Byte, ByRef piccdata0_2 As Byte) As Byte
'修改单区函数声明
Public Declare Function piccchangesinglekey Lib "OUR_MIFARE.dll" (ByVal ctrlword As Byte, ByRef serial As Byte, ByVal area As Byte, ByVal keyA1B0 As Byte, ByRef piccoldkey As Byte, ByRef piccnewkey As Byte) As Byte
'让设备发出声响函数声明
Public Declare Function pcdbeep Lib "OUR_MIFARE.dll" (ByVal xms As Long) As Byte
'读取设备编号函数声明
Public Declare Function pcdgetdevicenumber Lib "OUR_MIFARE.dll" (ByRef devicenumber As Byte) As Byte
'寻卡并返回该卡的序列号
Public Declare Function piccrequest Lib "OUR_MIFARE.dll" (ByRef serial As Byte) As Byte
'密码认证方式1,用外部密码认证,必须指定外部密码。本函数必须在piccrequest或piccrequestex函数执行之后运行,并且要紧接着调用,中途不能调用其他函数。
Public Declare Function piccauthkey1 Lib "OUR_MIFARE.dll" (ByRef serial As Byte, ByVal area As Byte, ByVal keyA1B0 As Byte, ByRef picckey As Byte) As Byte
'读出一块的数据,也就是16个字节。必须在执行piccrequest或 Piccrequestex函数,接着执行piccauthkey1或 piccauthkey2函数,然后执行piccread才能成功读出一块的数据。
Public Declare Function piccread Lib "OUR_MIFARE.dll" (ByVal block As Byte, ByRef blockdata As Byte) As Byte
'写一块的数据,也就是16个字节。必须在执行piccrequest或 Piccrequestex函数,接着执行piccauthkey1或 piccauthkey2函数,然后执行piccread才能成功读出一块的数据。
Public Declare Function piccwrite Lib "OUR_MIFARE.dll" (ByVal block As Byte, ByRef blockdata As Byte) As Byte
'修改卡A/B密码及控制字
Public Declare Function piccchangesinglekeyex Lib "OUR_MIFARE.dll" (ByVal ctrlword As Byte, ByRef serial As Byte, ByVal area As Byte, ByVal keyA1B0 As Byte, ByRef piccoldkey As Byte, ByRef piccnewkey As Byte) As Byte
'控制字定义,控制字指定,控制字的含义请查看本公司网站提供的动态库说明
Public Const BLOCK0_EN = &H1 '操作第0块
Public Const BLOCK1_EN = &H2 '操作第1块
Public Const BLOCK2_EN = &H4 '操作第2块
Public Const NEEDSERIAL = &H8 '仅对指定序列号的卡号操作
Public Const EXTERNKEY = &H10 '需要在参数中指定认证密码
Public Const NEEDHALT = &H20 '操作后休眠该卡,必须拿开卡再放回感应区才能重新感应到卡
End Module
轻松读卡
vbnet
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim status As Byte '存放返回值
Dim myareano As Byte '区号
Dim authmode As Byte '密码类型,用A密码或B密码
Dim myctrlword As Byte '控制字
Dim mypiccserial(3) As Byte '卡序列号,从0~3四个字节
Dim mypicckey(5) As Byte '密码,从0~5六个字节
Dim mypiccdata(47) As Byte '卡数据缓冲,从0~47四十八个字节
Dim hexstr As String
Dim i As Integer
'控制字指定,控制字的含义请查看本公司网站提供的动态库说明
myctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY
'指定区号
myareano = ComboBox1.SelectedIndex '指定为第区 '批定密码模式
authmode = ComboBox2.SelectedIndex '大于0表示用A密码认证,推荐用A密码认证
'指定密码
Try
hexstr = TextBox2.Text.Trim()
mypicckey(0) = "&H" & hexstr.Substring(0, 2)
mypicckey(1) = "&H" & hexstr.Substring(2, 2)
mypicckey(2) = "&H" & hexstr.Substring(4, 2)
mypicckey(3) = "&H" & hexstr.Substring(6, 2)
mypicckey(4) = "&H" & hexstr.Substring(8, 2)
mypicckey(5) = "&H" & hexstr.Substring(10, 2)
Catch
MessageBox.Show("卡片认证密码错误,请输入12位正确的16进制卡密码!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox2.Select()
Return
End Try
status = piccreadex(myctrlword, mypiccserial(0), myareano, authmode, mypicckey(0), mypiccdata(0))
If status = 0 Then
hexstr = ""
For i = 0 To 47
hexstr = hexstr + mypiccdata(i).ToString("X2") + " "
Next
TextBox3.Text = hexstr
status = piccread((myareano + 1) * 4 - 1, mypiccdata(0))
If status = 0 Then
hexstr = ""
For i = 0 To 15
hexstr = hexstr + mypiccdata(i).ToString("X2") + " "
Next
TextBox4.Text = hexstr
End If
Else
TextBox3.Text = ""
TextBox4.Text = ""
disperrinf(status)
End If
End Sub
轻松写卡
vbnet
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim i As Integer
Dim status As Byte '存放返回值
Dim myareano As Byte '区号
Dim authmode As Byte '密码类型,用A密码或B密码
Dim myctrlword As Byte '控制字
Dim mypicckey(5) As Byte '密码
Dim mypiccserial(3) As Byte '卡序列号
Dim mypiccdata(47) As Byte '卡数据缓冲
Dim hexstr As String
'控制字指定,控制字的含义请查看本公司网站提供的动态库说明
myctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY
'指定区号
myareano = ComboBox1.SelectedIndex '指定为第区 '批定密码模式
authmode = ComboBox2.SelectedIndex '大于0表示用A密码认证,推荐用A密码认证
'指定密码
Try
hexstr = TextBox2.Text.Trim()
mypicckey(0) = "&H" & hexstr.Substring(0, 2)
mypicckey(1) = "&H" & hexstr.Substring(2, 2)
mypicckey(2) = "&H" & hexstr.Substring(4, 2)
mypicckey(3) = "&H" & hexstr.Substring(6, 2)
mypicckey(4) = "&H" & hexstr.Substring(8, 2)
mypicckey(5) = "&H" & hexstr.Substring(10, 2)
Catch
MessageBox.Show("卡片认证密码错误,请输入12位正确的16进制卡密码!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox2.Select()
Return
End Try
'指定写卡数据
Try
hexstr = TextBox3.Text.Trim()
For i = 0 To 47
mypiccdata(i) = "&H" & hexstr.Substring(i * 3, 2)
Next i
Catch
MessageBox.Show("写卡数据输入错误,请输入48个正确的16进制写卡数据!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox3.Select()
Return
End Try
status = piccwriteex(myctrlword, mypiccserial(0), myareano, authmode, mypicckey(0), mypiccdata(0))
If status = 0 Then
pcdbeep(50)
MessageBox.Show("写卡成功!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
disperrinf(status)
End If
End Sub
修改卡片密码及控制位
vbnet
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim i As Integer
Dim status As Byte '存放返回值
Dim myareano As Byte '区号
Dim authmode As Byte '密码类型,用A密码或B密码
Dim myctrlword As Byte '控制字
Dim mypiccserial(3) As Byte '卡序列号
Dim mypiccoldkey(5) As Byte '旧密码
Dim mypiccnewkey(16) As Byte '新密码
Dim hexstr As String
i = MessageBox.Show(" 您确定要修改卡片的密码吗,如确定修改请一定记住新密码,否则卡片将报废!", "示例提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If i <> 6 Then
Return
End If
'控制字指定,控制字的含义请查看本公司网站提供的动态库说明
myctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY
'指定区号
myareano = ComboBox1.SelectedIndex '指定为第区 '批定密码模式
authmode = ComboBox2.SelectedIndex '大于0表示用A密码认证,推荐用A密码认证
'指定密码
Try
hexstr = TextBox2.Text.Trim()
mypiccoldkey(0) = "&H" & hexstr.Substring(0, 2)
mypiccoldkey(1) = "&H" & hexstr.Substring(2, 2)
mypiccoldkey(2) = "&H" & hexstr.Substring(4, 2)
mypiccoldkey(3) = "&H" & hexstr.Substring(6, 2)
mypiccoldkey(4) = "&H" & hexstr.Substring(8, 2)
mypiccoldkey(5) = "&H" & hexstr.Substring(10, 2)
Catch
MessageBox.Show("卡片认证密码错误,请输入12位正确的16进制卡密码!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox2.Select()
Return
End Try
'指定新密码,注意:指定新密码时一定要记住,否则有可能找不回密码,导致该卡报废。
Try
hexstr = TextBox4.Text.Trim()
For i = 0 To 15
mypiccnewkey(i) = "&H" & hexstr.Substring(i * 3, 2)
Next i
mypiccnewkey(16) = 3 '3是表示同时更改A、B、 密码权限访问字,为2表示密码权限访问字不更改,只改A、B密码,为0表示只改A密码
Catch
MessageBox.Show("新密码输入错误,请输入6字节A密码+4字节控制位+6字节B密码(16进制)!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox4.Select()
Return
End Try
status = piccchangesinglekey(myctrlword, mypiccserial(0), myareano, authmode, mypiccoldkey(0), mypiccnewkey(0))
If status = 0 Then
pcdbeep(50)
MessageBox.Show("修改卡片密码成功!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
disperrinf(status)
End If
End Sub
读写卡操作异常说明
vbnet
Private Sub disperrinf(ByVal errcode As Byte)
Select Case errcode
Case 1
MessageBox.Show("0~2块数据都没读出来,可能刷卡太块。但卡序列号已被读出!", "示例程序", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 2
MessageBox.Show("第0块已被读出,但1~2块读取失败。卡序列号已被读出来!", "示例程序", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 3
MessageBox.Show("第0、1块已被读出,但2块读取失败。卡序列号已被读出来!", "示例程序", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 8
MessageBox.Show("未寻到卡,请将卡放在感应区!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 9
MessageBox.Show("读序列码错误!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 10
MessageBox.Show("选卡错误!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 11
MessageBox.Show("装载密码错误!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 12
MessageBox.Show("卡密码认证错误!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 13
MessageBox.Show("读卡错误!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 14
MessageBox.Show("写卡错误!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 18
MessageBox.Show("写UID错误!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 21
MessageBox.Show("没有动态库!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 22
MessageBox.Show("动态库或驱动程序异常!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 23
MessageBox.Show("驱动程序错误或尚未安装!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 24
MessageBox.Show("操作超时,一般是动态库没有反映!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 25
MessageBox.Show("发送字数不够!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 26
MessageBox.Show("发送的CRC错!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 27
MessageBox.Show("接收的字数不够!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 28
MessageBox.Show("接收的CRC错!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case Else
MessageBox.Show("返回码(对应的说明请看例子中的注释):" + errcode.ToString(), "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Select
End Sub