本示例使用设备:Android Linux RFID读写器NFC发卡器WEB可编程NDEF文本/智能海报/-淘宝网 (taobao.com)
函数声明
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
轻松读卡
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickDim 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 StringDim i As Integer'控制字指定,控制字的含义请查看本公司网站提供的动态库说明myctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY'指定区号myareano = ComboBox1.SelectedIndex '指定为第区 '批定密码模式authmode = ComboBox2.SelectedIndex '大于0表示用A密码认证,推荐用A密码认证'指定密码Tryhexstr = 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)CatchMessageBox.Show("卡片认证密码错误,请输入12位正确的16进制卡密码!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)TextBox2.Select()ReturnEnd Trystatus = piccreadex(myctrlword, mypiccserial(0), myareano, authmode, mypicckey(0), mypiccdata(0))If status = 0 Thenhexstr = ""For i = 0 To 47hexstr = hexstr + mypiccdata(i).ToString("X2") + " "NextTextBox3.Text = hexstrstatus = piccread((myareano + 1) * 4 - 1, mypiccdata(0))If status = 0 Thenhexstr = ""For i = 0 To 15hexstr = hexstr + mypiccdata(i).ToString("X2") + " "NextTextBox4.Text = hexstrEnd IfElseTextBox3.Text = ""TextBox4.Text = ""disperrinf(status)End IfEnd Sub
轻松写卡
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.ClickDim i As IntegerDim 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密码认证'指定密码Tryhexstr = 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)CatchMessageBox.Show("卡片认证密码错误,请输入12位正确的16进制卡密码!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)TextBox2.Select()ReturnEnd Try'指定写卡数据Tryhexstr = TextBox3.Text.Trim()For i = 0 To 47mypiccdata(i) = "&H" & hexstr.Substring(i * 3, 2)Next iCatchMessageBox.Show("写卡数据输入错误,请输入48个正确的16进制写卡数据!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)TextBox3.Select()ReturnEnd Trystatus = piccwriteex(myctrlword, mypiccserial(0), myareano, authmode, mypicckey(0), mypiccdata(0))If status = 0 Thenpcdbeep(50)MessageBox.Show("写卡成功!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information)Elsedisperrinf(status)End IfEnd Sub
修改卡片密码及控制位
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.ClickDim i As IntegerDim 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 Stringi = MessageBox.Show(" 您确定要修改卡片的密码吗,如确定修改请一定记住新密码,否则卡片将报废!", "示例提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)If i <> 6 ThenReturnEnd If'控制字指定,控制字的含义请查看本公司网站提供的动态库说明myctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY'指定区号myareano = ComboBox1.SelectedIndex '指定为第区 '批定密码模式authmode = ComboBox2.SelectedIndex '大于0表示用A密码认证,推荐用A密码认证'指定密码Tryhexstr = 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)CatchMessageBox.Show("卡片认证密码错误,请输入12位正确的16进制卡密码!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)TextBox2.Select()ReturnEnd Try'指定新密码,注意:指定新密码时一定要记住,否则有可能找不回密码,导致该卡报废。Tryhexstr = TextBox4.Text.Trim()For i = 0 To 15mypiccnewkey(i) = "&H" & hexstr.Substring(i * 3, 2)Next imypiccnewkey(16) = 3 '3是表示同时更改A、B、 密码权限访问字,为2表示密码权限访问字不更改,只改A、B密码,为0表示只改A密码CatchMessageBox.Show("新密码输入错误,请输入6字节A密码+4字节控制位+6字节B密码(16进制)!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)TextBox4.Select()ReturnEnd Trystatus = piccchangesinglekey(myctrlword, mypiccserial(0), myareano, authmode, mypiccoldkey(0), mypiccnewkey(0))If status = 0 Thenpcdbeep(50)MessageBox.Show("修改卡片密码成功!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Information)Elsedisperrinf(status)End IfEnd Sub
读写卡操作异常说明
Private Sub disperrinf(ByVal errcode As Byte)Select Case errcodeCase 1MessageBox.Show("0~2块数据都没读出来,可能刷卡太块。但卡序列号已被读出!", "示例程序", MessageBoxButtons.OK, MessageBoxIcon.Error)Case 2MessageBox.Show("第0块已被读出,但1~2块读取失败。卡序列号已被读出来!", "示例程序", MessageBoxButtons.OK, MessageBoxIcon.Error)Case 3MessageBox.Show("第0、1块已被读出,但2块读取失败。卡序列号已被读出来!", "示例程序", MessageBoxButtons.OK, MessageBoxIcon.Error)Case 8MessageBox.Show("未寻到卡,请将卡放在感应区!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)Case 9MessageBox.Show("读序列码错误!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)Case 10MessageBox.Show("选卡错误!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)Case 11MessageBox.Show("装载密码错误!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)Case 12MessageBox.Show("卡密码认证错误!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)Case 13MessageBox.Show("读卡错误!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)Case 14MessageBox.Show("写卡错误!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)Case 18MessageBox.Show("写UID错误!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)Case 21MessageBox.Show("没有动态库!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)Case 22MessageBox.Show("动态库或驱动程序异常!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)Case 23MessageBox.Show("驱动程序错误或尚未安装!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)Case 24MessageBox.Show("操作超时,一般是动态库没有反映!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)Case 25MessageBox.Show("发送字数不够!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)Case 26MessageBox.Show("发送的CRC错!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)Case 27MessageBox.Show("接收的字数不够!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)Case 28MessageBox.Show("接收的CRC错!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)Case ElseMessageBox.Show("返回码(对应的说明请看例子中的注释):" + errcode.ToString(), "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error)End SelectEnd Sub