VB如何连续读取串口数据
想实现的是要连续读取从机上的值。需要先发一个读的命令再去收取反馈回来的值(10BYTE)。从机从收到命令到发出回应需要至少6MS。用下面这段程序,我能连续读到数据,但过了...
想实现的是要连续读取从机上的值。需要先发一个读的命令再去收取反馈回来的值(10BYTE)。从机从收到命令到发出回应需要至少6MS。
用下面这段程序,我能连续读到数据,但过了几秒,就会停住不动,此时INBUFFERCOUNT里的值少于10BYTE,而且不变化。
跪求,急等
Private Sub Form_Load()
'MSComm1.CommPort = 2
MSComm1.PortOpen = True
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeBinary
MSComm1.RThreshold = 0
End Sub
Private Sub Timer1_Timer()
Dim inByte() As Byte
Dim z() As Byte
Dim n As Integer
Dim Send(9) As Byte
Dim tes As Byte
Dim strData As String
Send(0) = 2 'write
Send(1) = 1 'adress
Send(2) = &H85 'mode
Send(3) = 0 '3~8 data
Send(4) = 6
If comdirection.ListIndex = 0 Then
Send(5) = Text1.Text * 5 \ 256
Send(6) = (Text1.Text * 5) Mod 256
Else
Send(5) = (65535 - Text1.Text * 5) \ 256
Send(6) = (65536 - Text1.Text * 5) Mod 256
End If
Send(7) = &HB
Send(8) = &HB8
For i = 0 To 8
Send(9) = Send(9) Xor Send(i) '校验
Next i
MSComm1.Settings = "9600,e,8,1"
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
Do
DoEvents
'该循环用来检测命令是否全部发送完毕
Loop Until MSComm1.OutBufferCount = 0
MSComm1.Output = Send
Savetime = Timer '记下开始时的时间
While Timer > Savetime + 1 '循环等待
DoEvents '转让控制权,以便让操作系统处理其它的事件。
Wend
Do
DoEvents
Loop Until MSComm1.InBufferCount >= 10
inByte = MSComm1.Input
For n = 0 To UBound(inByte)
If Len(Hex(inByte(n))) = 1 Then
strData = strData & "0" & Hex(inByte(n))
Else
strData = strData & Hex(inByte(n))
End If
Next n
Text2.Text = Mid(strData, 7, 4)
Text3.Text = Mid(strData, 11, 4)
Text4.Text = Mid(strData, 15, 4)
MSComm1.InBufferCount = 0
End Sub 展开
用下面这段程序,我能连续读到数据,但过了几秒,就会停住不动,此时INBUFFERCOUNT里的值少于10BYTE,而且不变化。
跪求,急等
Private Sub Form_Load()
'MSComm1.CommPort = 2
MSComm1.PortOpen = True
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeBinary
MSComm1.RThreshold = 0
End Sub
Private Sub Timer1_Timer()
Dim inByte() As Byte
Dim z() As Byte
Dim n As Integer
Dim Send(9) As Byte
Dim tes As Byte
Dim strData As String
Send(0) = 2 'write
Send(1) = 1 'adress
Send(2) = &H85 'mode
Send(3) = 0 '3~8 data
Send(4) = 6
If comdirection.ListIndex = 0 Then
Send(5) = Text1.Text * 5 \ 256
Send(6) = (Text1.Text * 5) Mod 256
Else
Send(5) = (65535 - Text1.Text * 5) \ 256
Send(6) = (65536 - Text1.Text * 5) Mod 256
End If
Send(7) = &HB
Send(8) = &HB8
For i = 0 To 8
Send(9) = Send(9) Xor Send(i) '校验
Next i
MSComm1.Settings = "9600,e,8,1"
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
Do
DoEvents
'该循环用来检测命令是否全部发送完毕
Loop Until MSComm1.OutBufferCount = 0
MSComm1.Output = Send
Savetime = Timer '记下开始时的时间
While Timer > Savetime + 1 '循环等待
DoEvents '转让控制权,以便让操作系统处理其它的事件。
Wend
Do
DoEvents
Loop Until MSComm1.InBufferCount >= 10
inByte = MSComm1.Input
For n = 0 To UBound(inByte)
If Len(Hex(inByte(n))) = 1 Then
strData = strData & "0" & Hex(inByte(n))
Else
strData = strData & Hex(inByte(n))
End If
Next n
Text2.Text = Mid(strData, 7, 4)
Text3.Text = Mid(strData, 11, 4)
Text4.Text = Mid(strData, 15, 4)
MSComm1.InBufferCount = 0
End Sub 展开
推荐于2017-10-05 · 知道合伙人软件行家
关注
展开全部
VB6.0MsComm控件可以利用OnComm事件连续获取来自外设发送的信号。
基于:
1)MsComm控件的RThreshold 属性不为0和恰当的接收代码。
2)使用电脑主板物理COM口或PCI多串口卡上的COM口COM,这样的COM口是全双工的,发送与接收不会冲突。
3)高质量的接收代码和符合通信协议和数据帧规约的接收处理代码。
实例代码:
Private Sub Form_Load()
MSComm1.InputMode = comInputModeBinary 伍喊 '采用二进制传输
MSComm1.InBufferCount = 0 '清空接受缓虚蠢冲区
MSComm1.OutBufferCount = 0 差橘陪 '清空传输缓冲区
MSComm1.RThreshold = 1 '产生MSComm事件
MSComm1.InBufferSize = 1024
TxtSend = ""
TxtSend = ""
txtReceive = ""
Text2 = ""
End Sub
Private Sub MSComm1_OnComm() '接收数据
Dim strBuff As String
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
jieshou
lenInput = Len(strData)
Text2 = lenInput \ 2
'数据处理代码
End Select
End Sub
Public Function jieshou() '接收数据处理为16进制
Dim i As Integer
For i = 0 To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i))
Else
strData = strData & Hex(BytReceived(i))
End If
Next
txtReceive = strData
End Function
展开全部
通讯突然停住的有几个原因:
1 串口设置了接收长度产生中断,悔神简但长时间内接收缓冲区没达到产生中断的数据;
2 文本框字符串的总长度超过了64k,文本框不再更新;
3 串口参数配碧裤置不正确。
试试我下面的程序看还会不会停住吧,我的程序是不间断接收数据的,不会有停住的可能。
Private Sub Command1_Click()
Timer1.Enabled = True
Command1.BackColor = vbGreen
End Sub
Private Sub Command2_Click()
Text1.Text = ""
End Sub
Private Sub Form_Load()
'通讯口初始化:
With MSComm1
.Settings = "9600,n,8,2"
.CommPort = 3
.InputMode = comInputModeBinary
.InBufferCount = 0
.OutBufferCount = 0
.RThreshold = 0
.SThreshold = 0
.PortOpen = True
End With
Text1.Text = "瞎芹"
End Sub
Private Sub Text1_Change()
If Len(Text1.Text) > 10000 Then Text1.Text = ""
End Sub
Private Sub Timer1_Timer()
'采用轮循法采集数据
Dim inx() As Byte
Dim strTemp As String
Dim strTemp1 As String
Dim ReceivedLen As Integer
Timer1.Enabled = False '关闭定时器
If MSComm1.InBufferCount > 0 Then
ReceivedLen = MSComm1.InBufferCount
inx = MSComm1.Input
For i = 0 To UBound(inx)
strTemp1 = Hex(inx(i))
If Len(strTemp1) > 1 Then
strTemp = strTemp & strTemp1 & " "
Else
strTemp = strTemp & "0" & strTemp1 & " "
End If
Next i
Text1.Text = Text1.Text & Format(Second(Now), "00") & Right(Format(Str(Timer), "0.00"), 3) & " " & strTemp & vbCrLf
Text1.SelStart = Len(Text1.Text)
End If
Timer1.Enabled = True '打开定时器
Label1.Caption = Now()
End Sub
1 串口设置了接收长度产生中断,悔神简但长时间内接收缓冲区没达到产生中断的数据;
2 文本框字符串的总长度超过了64k,文本框不再更新;
3 串口参数配碧裤置不正确。
试试我下面的程序看还会不会停住吧,我的程序是不间断接收数据的,不会有停住的可能。
Private Sub Command1_Click()
Timer1.Enabled = True
Command1.BackColor = vbGreen
End Sub
Private Sub Command2_Click()
Text1.Text = ""
End Sub
Private Sub Form_Load()
'通讯口初始化:
With MSComm1
.Settings = "9600,n,8,2"
.CommPort = 3
.InputMode = comInputModeBinary
.InBufferCount = 0
.OutBufferCount = 0
.RThreshold = 0
.SThreshold = 0
.PortOpen = True
End With
Text1.Text = "瞎芹"
End Sub
Private Sub Text1_Change()
If Len(Text1.Text) > 10000 Then Text1.Text = ""
End Sub
Private Sub Timer1_Timer()
'采用轮循法采集数据
Dim inx() As Byte
Dim strTemp As String
Dim strTemp1 As String
Dim ReceivedLen As Integer
Timer1.Enabled = False '关闭定时器
If MSComm1.InBufferCount > 0 Then
ReceivedLen = MSComm1.InBufferCount
inx = MSComm1.Input
For i = 0 To UBound(inx)
strTemp1 = Hex(inx(i))
If Len(strTemp1) > 1 Then
strTemp = strTemp & strTemp1 & " "
Else
strTemp = strTemp & "0" & strTemp1 & " "
End If
Next i
Text1.Text = Text1.Text & Format(Second(Now), "00") & Right(Format(Str(Timer), "0.00"), 3) & " " & strTemp & vbCrLf
Text1.SelStart = Len(Text1.Text)
End If
Timer1.Enabled = True '打开定时器
Label1.Caption = Now()
End Sub
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
设置 MSComm1.RThreshold 为10
在 OnComm 事件中 处姿团简迹裤理!
当或薯 RThreshold 为10 时 就接收
详细 请看 MSDN 吧
在 OnComm 事件中 处姿团简迹裤理!
当或薯 RThreshold 为10 时 就接收
详细 请看 MSDN 吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询