如何向IC卡中读写数据
请问下,如何向考勤卡中写数据和读数据???
那位高人能不能给点资料???
[解决办法]
购买IC卡控制器可得到关于此控制器的应用程序和开发工具包,该工具包包含一套对IC控制器开闭、上电下电、读写、校验等操作函数,编程是很简单的。
以下为部分参考文章
http://engine.cqvip.com/content/tn/91868x/2003/000/009/gc34_tn3_8238801.pdf
http://tech.yktchina.com/2006-12/20061229121144214.html
别人做过的一个24C01的卡读写程序,可以给你参考一下
模块中定义的函数
Declare Function IC_InitComm Lib "DCIC32.DLL " (ByVal Port%) As Long
Declare Function IC_ExitComm% Lib "DCIC32.DLL " (ByVal icdev As Long)
Declare Function IC_Down% Lib "DCIC32.DLL " (ByVal icdev As Long)
Declare Function IC_Pushout% Lib "DCIC32.DLL " (ByVal icdev As Long)
Declare Function IC_InitType% Lib "DCIC32.DLL " (ByVal icdev As Long, ByVal TypeNo%)
Declare Function IC_Status% Lib "DCIC32.DLL " (ByVal icdev As Long)
Declare Function IC_Erase% Lib "DCIC32.DLL " (ByVal icdev As Long, ByVal offset%, ByVal l%)
Declare Function IC_Read% Lib "DCIC32.DLL " (ByVal icdev As Long, ByVal offset%, ByVal l%, ByVal Databuffer$)
Declare Function IC_Read_Hex% Lib "DCIC32.DLL " (ByVal icdev As Long, ByVal offset%, ByVal l%, ByVal Databuffer$)
Declare Function IC_Read_Float% Lib "DCIC32.DLL " (ByVal icdev As Long, ByVal offset%, fdata As Single)
Declare Function IC_Read_Int% Lib "DCIC32.DLL " (ByVal icdev As Long, ByVal offset%, fdata As Long)
Declare Function IC_Write% Lib "DCIC32.DLL " (ByVal icdev As Long, ByVal offset%, ByVal length%, ByVal Databuffer$)
Declare Function IC_Write_Hex% Lib "DCIC32.DLL " (ByVal icdev As Long, ByVal offset%, ByVal length%, ByVal Databuffer$)
Declare Function IC_Write_Float% Lib "DCIC32.DLL " (ByVal icdev As Long, ByVal offset%, ByVal fdata As Single)
Declare Function IC_Write_Int% Lib "DCIC32.DLL " (ByVal icdev As Long, ByVal offset%, ByVal fdata As Long)
Declare Function IC_Write24% Lib "DCIC32.DLL " (ByVal icdev As Long, ByVal offset%, ByVal length%, ByVal Databuffer$)
Declare Function IC_Write24_Hex% Lib "DCIC32.DLL " (ByVal icdev As Long, ByVal offset%, ByVal length%, ByVal Databuffer$)
Declare Function IC_Write64% Lib "DCIC32.DLL " (ByVal icdev As Long, ByVal offset%, ByVal length%, ByVal Databuffer$)
Declare Function IC_Write64_Hex% Lib "DCIC32.DLL " (ByVal icdev As Long, ByVal offset%, ByVal length%, ByVal Databuffer$)
窗体中
Private InitCardOK As Boolean
Private icdev As Long
Private st%
Private Sub Command1_Click()
If icdev > 0 Then GoTo Out
icdev = IC_InitComm(0)
If icdev < 0 Then
icdev = IC_InitComm(1)
MsgBox " 初始化失败,请确认串口是否正常! "
Exit Sub
End If
InitCardOK = True
Command1.Enabled = False
HaveCard = True
MsgBox "初始化成功! "
Exit Sub
Out:
InitCardOK = False
End Sub
Private Sub Command2_Click()
If Valid Then
WriteToCard
End If
End Sub
Private Function WriteToCard() As Boolean
Dim KK() As String
Dim I, J As Integer
WriteToCard = False
Label4.Caption = "检测中...... "
Label4.Refresh
If Not CheckData(0, 9) Then Exit Function
Label4.Caption = "检测完毕! "
Exit Function
ERR:
End Function
Private Function CheckData(ByVal StartAdd As Integer, ByVal EndAdd As Integer) As Boolean
Dim I As Integer
Dim str As String * 30
Dim str1 As String * 30
Dim ReadStr As String * 30
Dim bln As Boolean
Dim ErrType As Integer
str = String((EndAdd - StartAdd + 1) * 2, "7 ")
str1 = String((EndAdd - StartAdd + 1) * 2, "F ")
ReadStr = String((EndAdd - StartAdd + 1) * 2, Chr(32))
CheckData = False
st = IC_Write24_Hex(icdev, StartAdd, EndAdd - StartAdd + 1, str$)
If st < 0 Then ErrType = 1: GoTo ERR
st = IC_Read_Hex(icdev, StartAdd, EndAdd - StartAdd + 1, ReadStr$)
If st < 0 Then ErrType = 2: GoTo ERR
If Mid$(ReadStr, 1, (EndAdd - StartAdd + 1) * 2) <> Mid$(str, 1, (EndAdd - StartAdd + 1) * 2) Then
ErrType = 3
GoTo ERR
End If
ReadStr = String((EndAdd - StartAdd + 1) * 2, Chr(32))
st = IC_Write24_Hex(icdev, StartAdd, EndAdd - StartAdd + 1, str1$)
If st < 0 Then ErrType = 4: GoTo ERR
st = IC_Read_Hex(icdev, StartAdd, EndAdd - StartAdd + 1, ReadStr$)
If st < 0 Then ErrType = 2: GoTo ERR
If Mid$(ReadStr, 1, (EndAdd - StartAdd + 1) * 2) <> Mid$(str1, 1, (EndAdd - StartAdd + 1) * 2) Then
ErrType = 5
GoTo ERR
End If
CheckData = True
StatusBar1.Panels(1).Text = "检测第 " & StartAdd & " 字节到第 " & EndAdd & " 字节成功,正在检测下一区域...... "
Exit Function
ERR:
MsgBox "检测时错误!! "
Select Case ErrType
Case 1
StatusBar1.Panels(1).Text = "写第 " & StartAdd & " 字节到第 " & EndAdd & " 字节时出错! "
Case 2
StatusBar1.Panels(1).Text = "回读第 " & StartAdd & " 字节到第 " & EndAdd & " 字节时出错! "
Case 3
StatusBar1.Panels(1).Text = "校验回读数据出错! " & "--第 " & StartAdd & " 字节到第 " & EndAdd & " 字节 "
Case 4
StatusBar1.Panels(1).Text = "清除第 " & StartAdd & " 字节到第 " & EndAdd & " 字节时出错! "
Case 5
StatusBar1.Panels(1).Text = "校验清除第 " & StartAdd & " 字节到第 " & EndAdd & " 字节时出错! "
Case Else
StatusBar1.Panels(1).Text = "未知错误! "
End Select
End Function
Private Function Valid() As Boolean
Dim ErrNum As Integer
Valid = False
If Not InitCardOK Then ErrNum = 10: GoTo ERR
st = IC_InitType(icdev, 64)
If st <> 0 Then ErrNum = 4: GoTo ERR
st = IC_Status(icdev)
If st < 0 Then ErrNum = 5: GoTo ERR
If st = 1 Then ErrNum = 6: GoTo ERR
Valid = True
Exit Function
ERR:
Select Case ErrNum
Case 4
MsgBox "初始化卡类型错误! "
Case 5
MsgBox "读写器连接错误! "
Case 6
MsgBox "读写器中无卡,请插入卡! "
HaveCard = False
Case 10
MsgBox "初始化失败! "
Case Else
MsgBox "未知错误! "
End Select
End Function
Private Sub Form_Unload(Cancel As Integer)
If InitCardOK Then
st = IC_ExitComm(icdev)
icdev = 0
If st < 0 Then
MsgBox "串口没有正确关闭,会影响下次读写卡, " & vbCrLf & vbCrLf & _
"请关闭系统以关闭串口! "
End If
End If
End Sub
'其实很简单的,呵呵,参考你的读卡器的例子就能完成了
[解决办法]
楼上的已经说了。。。
每张卡在出厂时分为16扇区,每个扇区有四块,每块有16个字节.在每个扇区的第四块数据是密钥和控制数据(前6个字节为A密钥后6个字节为B密钥中间4个字节为控制位)
一般读写某一块数据时一定要先用密钥去验证这个块所在的扇区密钥,成功后再进行读写操作。
[解决办法]
太复杂了!!
其实你的供应商应该有提供dll文件给你,和一般的调用api一样,只在先声明,后调用就OK了。
[解决办法]
贊同樓上的說法
如果是通過正常渠道買的IC卡鐘都是有相應的接口及說明文件的調用就行了
如果不是的話...
就用AccessPort來幫忙
------解决方案--------------------
cqq_chen(我是谁) ( ) 信誉:96 2007-08-04 13:25:11 得分: 0
太复杂了!!
其实你的供应商应该有提供dll文件给你,和一般的调用api一样,只在先声明,后调用就OK了。
//
米错
[解决办法]
我有个4442卡的存储简单说明不知有没有帮助
请看272967957的qq空间日志