如果已经要接收的字符数,可以很方便地用comEvReceive来接收,问题是,如果我的数据长度是未知的,要如何接收?
比如,我发一“AAA“指令过去,返回为3字符“xxx”;接着我发一“BBB”过去,返回为"yyy…yDD",其中DD是数据结束标志,该段程序如何写?
另外,我使用comEventBreak来提示“设备断电”,以前可以正常使用,但最近时而灵时而不灵的,怎么回事呢?该用什么代码来判断呢?我不想再发查询指令过去,因为担心会打扰我的其他指令。Private Sub cmdCommEvent_Click()
MSComm1.RThreshold = 3
Me.MSComm1.Output = “AAA”
End SubPrivate Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEventBreak
MsgBox "设备断开连接!"
End
Case comEvReceive
txtT.Text = Me.MSComm1.Input
End Select
End Sub
比如,我发一“AAA“指令过去,返回为3字符“xxx”;接着我发一“BBB”过去,返回为"yyy…yDD",其中DD是数据结束标志,该段程序如何写?
另外,我使用comEventBreak来提示“设备断电”,以前可以正常使用,但最近时而灵时而不灵的,怎么回事呢?该用什么代码来判断呢?我不想再发查询指令过去,因为担心会打扰我的其他指令。Private Sub cmdCommEvent_Click()
MSComm1.RThreshold = 3
Me.MSComm1.Output = “AAA”
End SubPrivate Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEventBreak
MsgBox "设备断开连接!"
End
Case comEvReceive
txtT.Text = Me.MSComm1.Input
End Select
End Sub
解决方案 »
- 找出文件夹内文件最新的更新时间(包括里面的子文件夹哈)
- 为什么我在vb和aceess连接时不能使用update,其他都是好的
- 如何注册install shield devoloper 8?????????????????????????
- vb生成EXE文件时出错!急,在先等待
- 请问如何捕捉鼠标和键盘?
- 从ACCESS数据库中读取数据并用MSChart绘制曲线
- 要输入大于1M的文本内容用什么控件来装?
- 如何从一个表中随机读取记录?
- vb中支持gif动画
- S0S!通过电脑向手机发送短消息,如何向手机发送中文消息呢?
- 请求帮忙:使用VB向Excel中插入数据时出现的奇怪问题
- 如何使用MSComm检测串口设备是否处于断电状态?
Dim ESC As String: ESC = Chr(27)
txtT.Text = vbnullstring '<-
MSComm1.RThreshold = 3
Me.MSComm1.Output = “AAA”
End SubPrivate Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEventBreak
MsgBox "设备断开连接!"
End
Case comEvReceive
txtT.Text = txtT.Text & Me.MSComm1.Input '<-
End Select
End Sub
Public Property Get ESC() As String
ESC = Chr$(27)
End Property
或者自己做一个 tlb,里面定义常量。
txtT.Text = vbnullstring '<-发送前向文本框清除,两次发送的回复不会混合
MSComm1.RThreshold = 1 '<-回复长度未知,就逐个字符接受
Me.MSComm1.Output = “AAA”
End SubPrivate Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEventBreak
MsgBox "设备断开连接!"
End
Case comEvReceive
txtT.Text = txtT.Text & Me.MSComm1.Input '<-本次回复的所有字符拼接起来
End Select
End Sub
即:
Click事件中:
发送AAA
等待xxx
获得xxx后,发送BBB,
等待yyy……yDD
Option Explicit
Dim strData As StringPrivate Sub Command1_Click()
MSComm1.Output = "AAA"
End SubPrivate Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
End SubPrivate Sub MSComm1_OnComm()
Dim sj As String
Select Case MSComm1.CommEvent
Case 2
sj = MSComm1.Input
strData = strData & sj
If Mid(strData, 1, 3) = "xxx" Then
MSComm1.Output = "BBB"
strData = ""
ElseIf Right(strData, 2) = "DD" Then
Text1 = strData
strData = ""
End If
End Select
End Sub
发送AAA
等待xxx
获得xxx后,发送BBB,
等待yyy……yDD
发送CCC
等待
发送DDD
……
Private m_Receive As StringPrivate Function SendAndReceive(ByVal cmd As String) As Boolean
Dim bAllReceive As Boolean
m_DeviceError = False
m_Receive = vbNullString
MSComm1.Output cmd
While Not bAllReceive
DoEvents
If m_DeviceError Then Exit Function
Select Case cmd
Case "AAA"
If Len(m_Receive) = 3 Then bAllReceive = True
Case "BBB"
If Right$(m_Receive, 2) = "DD" Then bAllReceive = True
case ... '<-根据不同的发送命令和规则判断接收是否完成
End Select
Wend
txtT.Text = txtT.Text & m_Receive & vbCrLf
SendAndReceive = True
End FunctionPrivate Sub cmdCommEvent_Click()
txtT.Text = vbNullString
MSComm1.RThreshold = 1
If Not SendAndReceive("AAA") Then Exit Sub
If Not SendAndReceive("BBB") Then Exit Sub
...
End SubPrivate Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEventBreak
m_DeviceError = True
MsgBox "设备断开连接!"
End
Case comEvReceive
m_Receive = m_Receive & Me.MSComm1.Input
End Select
End Sub
算了,结贴吧,以后想想有什么好的办法。