首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

关于MSComm控件设定间隔时间的有关问题,请诸位前辈指点一下!()

2012-01-24 
关于MSComm控件设定间隔时间的问题,请诸位前辈指点一下!(在线等)MSComm控件接受数据,想设定每100ms,触发On

关于MSComm控件设定间隔时间的问题,请诸位前辈指点一下!(在线等)
MSComm控件接受数据,想设定每100ms,触发OnComm事件
请问该如何处理,用mscomm能否实现,谢谢大家


[解决办法]
将接收代码放Sub过程,用Timer控件定时触发
Private Sub Timer1_Timer()
Call jieshou
End Sub

Public Sub jieshou()
Dim BytReceived() As Byte
Dim strBuff As String
Dim strData As String
Dim i As Integer
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
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
Text3 = strData
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 1 'COM端口
MSComm1.Settings = "9600,n,8,1 "
MSComm1.InputMode = comInputModeBinary '采用二进制传输
MSComm1.InBufferCount = 0 '清空接受缓冲区
MSComm1.OutBufferCount = 0 '清空传输缓冲区
MSComm1.RThreshold = 1 '产生MSComm事件
MSComm1.InBufferSize = 1024
MSComm1.PortOpen = True '打开端口
Text3 = " "
Timer1.Interval = 100
End Sub

[解决办法]
dengjun1982(dengj) :
我给出的代码用串口调试精灵测试无你说的出错现象。
假如单片机发送的数据是有间隔时间,接收可直接在MSComm1_OnComm事件中实现,没必要在接收侧由Timer控件触发OnComm事件:
Option Explicit
Dim BytReceived() As Byte
Dim strData As String

Private Sub MSComm1_OnComm()
On Error Resume Next
Text3 = " "
Dim strBuff As String
Dim i As Integer

Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
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
Text3 = Text3 + strData
End Select
End Sub

Private Sub Form_Load()
MSComm1.CommPort = 1 'COM端口
MSComm1.Settings = "9600,n,8,1 "
MSComm1.InputMode = comInputModeBinary '采用二进制传输
MSComm1.InBufferCount = 0 '清空接受缓冲区
MSComm1.OutBufferCount = 0 '清空传输缓冲区
MSComm1.RThreshold = 1 '产生MSComm事件
MSComm1.InBufferSize = 1024
MSComm1.PortOpen = True '打开端口
Text3 = " "
Timer1.Interval = 200
End Sub

Private Sub Timer1_Timer()
strData = " "
End Sub

热点排行