MSComm控件接受数据,想设定每100ms,触发OnComm事件
请问该如何处理,用mscomm能否实现,谢谢大家

解决方案 »

  1.   

    用timer控件,100ms的精度还是可以
      

  2.   

    但是OnComm事件,是一直在执行的,能不能写一下程序
      

  3.   

    在 AxMSComm1.CommEvent中取得数据的,但是在Timer1_Tick事件中无法触发,是怎么回事呢
          Select Case AxMSComm1.CommEvent
                Case 2
                    str = AxMSComm1.Input
      

  4.   

    将接收代码放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
      

  5.   

    zdingyun() ,你好
    我调了一下程序,在strBuff = MSComm1.Input时,抛出异常,请问是
    怎么回事呢?
      

  6.   

    zdingyun() ,你好
    是我的问题
      

  7.   

    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
      

  8.   

    最好一个疑问,我现在因为是读GPS的数据,从GPS侧每100ms是一组数据,请问像这样的情况,如果单单用OnComm事件,该如何作处理,明天结分
      

  9.   

    在 AxMSComm1.CommEvent中取得数据的,但是在Timer1_Tick事件中无法触发
    ----------
      或者是用CommEvent取数据.
      或者是用TIMER事件取数据.
      这两种方式是并列的,还从没有看到混在一起用的,这是自找麻烦.