十万火急!!怎样读取IC卡中的信息!!(高分奉送)

saimanok 2002-09-25 01:10:54
我现在正在作一个管理软件。

每位职工都有一张IC卡,上班时在自己的电脑上刷一下卡,计算机得识别他的身份。

请问在VB中如何实现呢???

哪有类似的原码???

谢了!!!
...全文
543 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
D_Q 2002-09-25
  • 打赏
  • 举报
回复
IC.BAS文件,封装了对IC卡读写器的操作

'Created by jungle 2000/02/25
'****************** IC卡读写模块 ************************
'本模块封装了IC卡的读写操作。使用时,应先调用InitIC, 然后可以
'反复进行READICCODE和WRITEICCODE操作。最后在退出
'界面之前必须调用RELEASEIC.
'*******************************************************

Const OLD_PWD = "8377EC" '卡出厂密码

Private Declare Function link_com Lib "ICbala32" _
(ByVal port As Long) As Long
Private Declare Function unlink_com Lib "ICbala32" _
(ByVal Code As Long) As Long
Private Declare Function power_on Lib "ICbala32" _
() As Long
Private Declare Function power_off Lib "ICbala32" _
() As Long

Private Declare Function inq_status Lib "ICbala32" _
(ByVal Status As String) As Long

Private Declare Function sel_card Lib "ICbala32" _
(ByVal CardType As Long) As Long
Private Declare Function sel_page Lib "ICbala32" _
(ByVal page As Long) As Long

Private Declare Function read_card Lib "ICbala32" _
(ByVal cmd As Long, ByVal addrh As Long, ByVal addrl As Long, _
ByVal length As Long, ByVal rece As String) As Long

Private Declare Function chk_secret Lib "ICbala32" _
(ByVal cmd As Long, ByVal secret As String) As Long

Private Declare Function write_card Lib "ICbala32" _
(ByVal cmd As Long, ByVal addrh As Long, ByVal addrl As Long, _
ByVal length As Long, ByVal send As String) As Long

Public lICHDc As Long

Public Enum ComConst
enCOM1 = 0
enCOM2 = 1
enCOM3 = 2
enCOM4 = 3
End Enum

Public Enum enICStateType
enInStatus = 0
enPowerStatus = 1
End Enum

Public Sub InitIC(pCom As ComConst)
lICHDc = link_com(pCom)
'Call SetICTypeNPage
End Sub

Public Sub ReleaseIC()
Dim lSuc As Long

lSuc = unlink_com(lICHDc)

If lSuc <> 0 Then
ERR.Raise 1001, , "端口资源释放错误,请检查!错误号:" + CStr(lSuc)
End If
End Sub

Public Sub PowerUpIC()
Dim lSuc As Long

lSuc = power_on()

If lSuc <> 0 Then
ERR.Raise 1001, , "IC卡加电错误,请检查!错误号:" + CStr(lSuc)
End If
End Sub

Public Sub PowerDownIC()
Dim lSuc As Long

lSuc = power_off()

If lSuc <> 0 Then
ERR.Raise 1001, , "IC卡断电错误,请检查!错误号:" + CStr(lSuc)
End If
End Sub

Public Sub SetICTypeNPage(Optional pCardType As Long = 13, _
Optional pPageNo As Long = 0)

Dim lSuc As Long
lSuc = sel_card(pCardType)
If lSuc <> 0 Then
ERR.Raise 1001, , "IC卡类型选择错误,请检查!错误号:" + CStr(lSuc)
End If
lSuc = sel_page(pPageNo)
If lSuc <> 0 Then
ERR.Raise 1001, , "IC卡页码选择错误,请检查!错误号:" + CStr(lSuc)
End If
End Sub

Public Function ReadStringFromIC(Optional pStartAddr As Long = 64, _
Optional pLength As Long = 6) As String
Dim tRece As String * 256
Dim cmd As Long
Dim addrh As Long
Dim lSuc As Long
Dim tStr As String

cmd = 18
addrh = 0
lSuc = read_card(cmd, addrh, pStartAddr, pLength, tRece)

If lSuc <> 0 Then
ERR.Raise 1001, , "IC卡数据读取错误,请检查!错误号:" + CStr(lSuc)
Else
tStr = Left(tRece, pLength)
End If
ReadStringFromIC = tStr
End Function

Public Sub CheckICPwd(Optional pICPwd As String = OLD_PWD)
Dim lSuc As Long
Dim cmd As Long
Dim tPwd As String * 6
cmd = 2

tPwd = pICPwd
lSuc = chk_secret(cmd, tPwd)

If lSuc <> 0 Then
Call PowerDownIC
ERR.Raise 1001, , "IC卡密码检测错误,请检查。如果连续发生三次错误,本IC卡将报废!错误号:" + CStr(lSuc)
End If
End Sub

Public Sub WriteStringToIC(pString As String, Optional pStartAddr As Long = 64)
Dim cmd As Long
Dim addrh As Long
Dim tSend As String * 256
Dim tLen As Long
Dim lSuc As Long

cmd = 18
addrh = 0

tLen = LenB(StrConv(pString, vbFromUnicode))
tSend = pString
lSuc = write_card(cmd, addrh, pStartAddr, tLen, tSend)

If lSuc <> 0 Then
Call PowerDownIC
ERR.Raise 1001, , "IC卡数据写入错误,请检查!错误号:" + CStr(lSuc)
End If
End Sub

Public Function QueryICState(pType As enICStateType) As Boolean
Dim tStr As String * 8
Dim lSuc As Long

lSuc = inq_status(tStr)

If lSuc <> 0 Then
ERR.Raise 1001, , "读写器状态查询错误,请检查!错误号:" + CStr(lSuc)
Else
Select Case pType
Case enInStatus:
If Mid(tStr, 6, 1) = "1" Then
QueryICState = True
Else
QueryICState = False
End If
Case enPowerStatus:
If Mid(tStr, 8, 1) = "1" Then
QueryICState = True
Else
QueryICState = False
End If
End Select
End If
End Function

Public Function ReadICCode() As String
Dim lSuc As Long
Dim tStr As String * 6

lSuc = inq_status(tStr)

If lSuc <> 0 Then
ERR.Raise 1001, , "读写器状态查询错误,请检查!错误号:" + CStr(lSuc)
End If

If Mid(tStr, 6, 1) <> "1" Then
ERR.Raise 1001, , "读写器中无卡,请插入IC卡!"
End If

If Mid(tStr, 8, 1) <> "1" Then
lSuc = power_on()
If lSuc <> 0 Then
ERR.Raise 1001, , "IC卡加电错误,请检查!错误号:" + CStr(lSuc)
End If
End If

Call SetICTypeNPage
tStr = ReadStringFromIC()

lSuc = power_off
If lSuc <> 0 Then
ERR.Raise 1001, , "IC卡断电错误,请检查!错误号:" + CStr(lSuc)
End If

ReadICCode = tStr
End Function

Public Sub WriteICCode(pString As String)
Dim lSuc As Long
Dim tStr As String * 8

lSuc = inq_status(tStr)

If lSuc <> 0 Then
ERR.Raise 1001, , "读写器状态查询错误,请检查!错误号:" + CStr(lSuc)
End If

If Mid(tStr, 6, 1) <> "1" Then
ERR.Raise 1001, , "读写器中无卡,请插入IC卡!"
End If

If Mid(tStr, 8, 1) <> "1" Then
lSuc = power_on()
If lSuc <> 0 Then
ERR.Raise 1001, , "IC卡加电错误,请检查!错误号:" + CStr(lSuc)
End If
End If

Call SetICTypeNPage
Call CheckICPwd
Call WriteStringToIC(pString)

lSuc = power_off
If lSuc <> 0 Then
ERR.Raise 1001, , "IC卡断电错误,请检查!错误号:" + CStr(lSuc)
End If
End Sub
D_Q 2002-09-25
  • 打赏
  • 举报
回复
Public Declare Function init_com Lib "sure32wc.dll" (ByVal port As Long) As Long
'初始化串行口: port=0 串行口1 , port=1 串行口2 返回值:0 成功,4 串行口错

Public Declare Function sele_card Lib "sure32wc.dll" (ByVal cardtype As Long) As Long
'选择卡的类型 cardtype =42 为 IC卡 返回值:0 成功

Public Declare Function power_on Lib "sure32wc.dll" () As Long
'给卡上电 返回值: 0成功 ,2无卡 ,4串行口错

'passwordpasswordpasswordpassword

Public Declare Function power_off Lib "sure32wc.dll" () As Long
'卡下电 自弹式读写机同时退卡 返回值:0成功,2无卡,4串行口错

Public Declare Function close_com Lib "sure32wc.dll" () As Long
'关闭串行口 返回值: 0 成功,4 串行口错

Public Declare Function rd_str Lib "sure32wc.dll" (ByVal apz As Long, ByVal address As Long, ByVal length As Long, ByVal buffers As String) As Long
Public Declare Function wr_str Lib "sure32wc.dll" (ByVal apz As Long, ByVal address As Long, ByVal length As Long, ByVal buffers As String) As Long
Public Declare Function rd_asc Lib "sure32wc.dll" (ByVal apz As Long, ByVal address As Long, ByVal length As Long, ByVal buffers As String) As Long
Public Declare Function wr_asc Lib "sure32wc.dll" (ByVal apz As Long, ByVal address As Long, ByVal length As Long, ByVal buffers As String) As Long
'读卡或写卡 :参数apz 为写卡的区域 0为公用区 1为应用数据区 address 为读取或写入的起始地址
'length 为读取或写入的字节数 buffers 为读写数据的缓冲区

Public Declare Function chk_sc Lib "sure32wc.dll" (ByVal i1 As Long, ByVal i2 As Long, ByVal STR As String) As Long
'核对密码 返回值:0 正确,11密码错
Public Declare Function wr_sc Lib "sure32wc.dll" (ByVal STR As String) As Long
'写入新的密码
Public Declare Function inquire Lib "sure32wc.dll" (ByVal STR As String) As Long
'读取读写器状态

7,786

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧