在 AxMSComm1.CommEvent中取得数据的,但是在Timer1_Tick事件中无法触发,是怎么回事呢 Select Case AxMSComm1.CommEvent Case 2 str = AxMSComm1.Input
将接收代码放Sub过程,用Timer控件定时触发 Private Sub Timer1_Timer() Call jieshou End SubPublic 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 StringPrivate 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 SubPrivate 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 SubPrivate Sub Timer1_Timer() strData = "" End Sub
Select Case AxMSComm1.CommEvent
Case 2
str = AxMSComm1.Input
Private Sub Timer1_Timer()
Call jieshou
End SubPublic 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
我调了一下程序,在strBuff = MSComm1.Input时,抛出异常,请问是
怎么回事呢?
是我的问题
我给出的代码用串口调试精灵测试无你说的出错现象。
假如单片机发送的数据是有间隔时间,接收可直接在MSComm1_OnComm事件中实现,没必要在接收侧由Timer控件触发OnComm事件:
Option Explicit
Dim BytReceived() As Byte
Dim strData As StringPrivate 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 SubPrivate 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 SubPrivate Sub Timer1_Timer()
strData = ""
End Sub
----------
或者是用CommEvent取数据.
或者是用TIMER事件取数据.
这两种方式是并列的,还从没有看到混在一起用的,这是自找麻烦.