7,762
社区成员
发帖
与我相关
我的任务
分享
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Form_Load()
Timer1.Interval = 500
End Sub
Private Sub Timer1_Timer()
Dim bolReadSuccess As Boolean '读卡成功
Dim lngP As Long
Dim strReadMemo As String
MSComm1.InputLen = 0
MSComm1.InBufferCount = 0
MSComm1.Output = "读卡命令"
Sleep 20 '等待20ms,通常情况下,如果正常读卡,MScomm控件的读入缓冲区就会有数据(不为空)
If MSComm1.InBufferCount = 0 Then Exit Sub
'当接收到了数据时执行下面的语句
lngP = GetTickCount '记录通信等待起始时间
bolReadSuccess = False
Do
DoEvents
If MSComm1.InBufferCount >= 10 Then
bolReadSuccess = True '此处判断是否读卡成功,你可根据你的实际情况编写
End If
Loop Until bolReadSuccess Or GetTickCount - lngP > 80
strReadMemo = MSComm1.Input '读入内容
End Sub