计划采取comEvReceive 事件触发后再读取input的方式
想向大家请教一下: comEvReceive 事件触发前要有 mscomm1.input这条函数执行吗?
我现在的一个任务是PC与一台普通的串口设备进行数据传送,采用的是软件握手的方式。大致过程如下
PC 先向下位机发送PR1 <CR>指令
下位机回复上位机 06 13 10
PC收到这三个字符后,发送chr(05)
下位机将有效测量数据返回至PC
已经用龚建伟老师的调试助手调通了,并用labwindows已经完成了数据采集程序,为方便使用DDE技术 现在是想用VB完成数据采集程序
考虑到下位机的回复速度可能会比较慢,出现comin=mscomm1.input 执行后comin为一个空的字符串的情况,计划采取comEvReceive 事件触发后再读取input的方式。设置好rthrehold=1后 始终不发生comEvReceive 事件
初学VB 不知道应该怎样解决,请各位帮忙,谢谢
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 5985
ClientLeft = 60
ClientTop = 450
ClientWidth = 9075
LinkTopic = "Form1"
ScaleHeight = 5985
ScaleWidth = 9075
StartUpPosition = 3 '窗口缺省
Begin VB.TextBox Text2
Height = 495
Left = 3720
TabIndex = 7
Text = "Text2"
Top = 960
Width = 1215
End
Begin VB.Timer Timer10
Enabled = 0 'False
Left = 3000
Top = 3240
End
Begin VB.TextBox Text1
Height = 375
Index = 5
Left = 1440
TabIndex = 6
Text = "Text1"
Top = 3360
Width = 1215
End
Begin VB.TextBox Text1
Height = 375
Index = 4
Left = 1440
TabIndex = 5
Text = "Text1"
Top = 2880
Width = 1215
End
Begin VB.TextBox Text1
Height = 375
Index = 3
Left = 1440
TabIndex = 4
Text = "Text1"
Top = 2400
Width = 1215
End
Begin VB.TextBox Text1
Height = 375
Index = 2
Left = 1440
TabIndex = 3
Text = "Text1"
Top = 1920
Width = 1215
End
Begin VB.TextBox Text1
Height = 375
Index = 1
Left = 1440
TabIndex = 2
Text = "Text1"
Top = 1440
Width = 1215
End
Begin VB.Timer Timer2
Enabled = 0 'False
Interval = 2000
Left = 3000
Top = 2640
End
Begin MSCommLib.MSComm MSComm8
Left = 2880
Top = 1680
_ExtentX = 1005
_ExtentY = 1005
_Version = 393216
DTREnable = 0 'False
RThreshold = 1
SThreshold = 1
End
Begin VB.TextBox Text1
Height = 375
Index = 0
Left = 1440
TabIndex = 1
Text = "Text1"
Top = 960
Width = 1215
End
Begin Command1
Caption = "打开串口"
Height = 495
Left = 1320
TabIndex = 0
Top = 5040
Width = 1215
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim k As Integer
Dim step As Integer
Dim comflag As Integer 'whether there is a input in comport
Dim j As Integer 'j stands for the channel
Private Sub Command1_Click()
Dim i As Integer
i = 0
If Command1.Caption = "打开串口" Then
Command1.Caption = "关闭串口"
Timer2.Enabled = True
Call Timer2_Timer
Else: Command1.Caption = "打开串口"
Timer2.Enabled = False
Do
Text1(i).Text = ""
i = i + 1
Loop While (i < 6)
End If
End Sub
Private Sub Form_Load()
Form1.Show
MSComm8.PortOpen = True
step = 1
comflag = 1
j = 0
End Sub
Private Sub MSComm8_OnComm()
Select Case
Case comEvReceive '收到 RThreshold 个字符
comflag = 1
Case comEvSend
Text2.Text = "sent data"
End Select
End Sub
Private Sub Timer10_Timer()
k = k + 1
End Sub
Private Sub Timer2_Timer()
Dim comin As String
Dim len1 As Integer
Dim comout As String
Dim x As String
Dim i As Integer
'Dim j As Integer
comout = "PR"
comin = ""
len1 = Len(comin)
i = 0
'j = 0
k = 0
Do
If MSComm8.PortOpen = False Then
MSComm8.PortOpen = True
End If
Select Case step
Case 1
MSComm8.Output = comout + Chr(49 + j) + Chr(13)
step = 2
Case 2
If comflag = 1 Then
comin = MSComm8.Input
If ((Left(comin, 1) = Chr(6)) And (Right(Left(comin, 2), 1) = Chr(13)) And (Right(Left(comin, 3), 1) = Chr(10))) Then
MSComm8.Output = Chr(5)
step = 4
Else:
step = 2
GoTo ErrorHandler
End If
Else: GoTo ErrorHandler
End If
Case 4
If comflag = 1 Then
comin = MSComm8.Input
x = Left(comin, 1)
step = 1
Select Case x
Case 0 'ok
Text1(j).Text = Format(Right(comin, 10) * 100#, "0.00E+00")
Case 1
Text1(j).Text = "UnderRan"
Case 2
Text1(j).Text = "OverRang"
Case 3
Text1(j).Text = "SensorEr"
Case 5
Text1(j).Text = "NoSensor"
Case Else
GoTo ErrorHandler
End Select
j = j + 1
Else: GoTo ErrorHandler
End If
End Select
Loop While (j < 6)
ErrorHandler:
'MSComm8.PortOpen = False
End Sub