PB串口通信问题
以下是用VB通过串口采集卡钟考勤数据的程序段,在PB中要怎样书写呢,请大家帮我看看
outstr(0) = machineadress '整型数据
outstr(1) = 2
outstr(2) = 1
If Not FrmGather.MSComm.PortOpen Then FrmGather.MSComm.PortOpen = True
FrmGather.MSComm.InBufferCount = 0
FrmGather.MSComm.OutBufferCount = 0
FrmGather.MSComm.InputMode = comInputModeBinary
FrmGather.MSComm.Output = outstr
Curtime1 = Timer
Do
Curtime2 = Timer
If Curtime2 - Curtime1 <= 1 Then
DoEvents
Else
LabInfo.Caption = "系统忙,请重试,发送采集指令时超时出错!"
Labgather.Visible = False
Call ComError '端口关闭过程
Exit Sub
End If
Loop Until FrmGather.MSComm.InBufferCount >= 1
FrmGather.MSComm.InputLen = 1
Vin = FrmGather.MSComm.Input
If Vin(0) <> machineadress Then
Labgather.Visible = False
LabInfo.Caption = "采集数据总数时没有得到正确的考勤机地址信号!"
Call ComError '端口关闭过程
Exit Sub
End If
Curtime1 = Timer
Do Until FrmGather.MSComm.InBufferCount >= 3
Curtime2 = Timer
If Curtime2 - Curtime1 < 3 Then
DoEvents
Else
Labgather.Visible = False
LabInfo.Caption = "接收采集数据总数时等待超时!"
Call ComError '端口关闭过程
Exit Sub
End If
Loop
FrmGather.MSComm.InputLen = 3
Vin = FrmGather.MSComm.Input
Datanum = (Vin(0) - 114) * 65536 + Vin(1) * 256 + Vin(2)
Datanum = Datanum / 10
If Datanum = 0 Then
Labgather.Visible = False
LabInfo.Caption = "没有数据可以采集"
Call ComError '端口关闭过程
Exit Sub
End If
i = 1
LabRecord.Visible = True
Do Until i > Datanum '循环次数等于纪录数
LabRecord.Caption = "共" & Datanum & "条,第" & i & "条纪录"
Curtime1 = Timer
Do '接收地址信号
Curtime2 = Timer
If Curtime2 - Curtime1 <= 1 Then
DoEvents
Else
Labgather.Visible = False
LabInfo.Caption = "没有得到正确的考勤机地址信号!"
Call ComError '端口关闭过程
Exit Sub
End If
Loop Until FrmGather.MSComm.InBufferCount >= 1
FrmGather.MSComm.InputLen = 1
Vin = FrmGather.MSComm.Input
Curtime1 = Timer
Do Until FrmGather.MSComm.InBufferCount >= 9
Curtime2 = Timer
If Curtime2 - Curtime1 < 1 Then
DoEvents
Else
Labgather.Visible = False
LabInfo.Caption = "没有得到正确的考勤机地址信号!"
Call ComError '端口关闭过程
Exit Sub
End If
Loop
FrmGather.MSComm.InputLen = 9
Vin = FrmGather.MSComm.Input '成功采集到一条纪录
nUD = UBound(Ind)
ReDim Preserve Ind(0 To nUD + 1)
n = Fix(Vin(0) / 16) * 10 + Vin(0) Mod 16
If n >= 0 And n <= 9 Then
c = "0" + CStr(n)
Else
c = CStr(n)
End If
n = Fix(Vin(1) / 16) * 10 + Vin(1) Mod 16
If n >= 0 And n <= 9 Then
c = c + "0" + CStr(n)
Else
c = c + CStr(n)
End If
n = Fix(Vin(2) / 16) * 10 + Vin(2) Mod 16
If n >= 0 And n <= 9 Then
c = c + "0" + CStr(n)
Else
c = c + CStr(n)
End If
Ind(nUD + 1).id = c
y = Fix(Vin(3) / 16) * 10 + Vin(3) Mod 16
m = Fix(Vin(4) / 16) * 10 + Vin(4) Mod 16
D = Fix(Vin(5) / 16) * 10 + Vin(5) Mod 16
H = Fix(Vin(6) / 16) * 10 + Vin(6) Mod 16
Mi = Fix(Vin(7) / 16) * 10 + Vin(7) Mod 16
Smid = CStr(y) + "-" + CStr(m) + "-" + CStr(D)
Ind(nUD + 1).indate = CDate(Smid) '日期
Smid = CStr(H) + ":" + CStr(Mi) + ":" + "00"
Ind(nUD + 1).intime = CDate(Smid) '时间
Ind(nUD + 1).machine_id = machineadress '机器编号
i = i + 1
Loop 'loop do datanum 采集循环 循环纪录数目