该控件的ONCOMM事件有一个值1001对应的是收到断开信号,这是为什么?该如何处理该通信错误呢?

解决方案 »

  1.   

    if MSComm1.CommEvent=comEventBreak then
        ST = MSComm1.Input'不用处理它,就用这个input来清空接收缓冲区就行
    end if
      

  2.   

    这个错误是由什么引起的呢??我直接用的MSCOMM1。INBUFFERCOUNT=0 来清除的缓冲区,但是程序有短时间会死掉,过会有缓冲过来。继续运行。
      

  3.   

    Const Xon = &H11
          Const Xoff = &H13      Private Sub Form_Load()
             Form1.Caption = "App2"
             With MSComm1
                .CommPort = 2
                .Handshaking = 2 - comRTS
                .RThreshold = 1
                .RTSEnable = True
                .Settings = "9600,n,8,1"
                .SThreshold = 1
                .PortOpen = True
             End With
             Text1.Text = ""
          Label1.Caption = "No input yet"
          End Sub      Private Sub Form_Unload(Cancel As Integer)
             MSComm1.PortOpen = False
          End Sub      Private Sub MSComm1_OnComm()
             Dim InBuff As String         Select Case MSComm1.CommEvent
             ' Handle each event or error by placing
             ' code below each case statement.         ' This template is found in the Example
             ' section of the OnComm event help topic
             ' in VB help.         ' Errors
                Case comEventBreak   ' A Break was received.
                Case comEventCDTO    ' CD (RLSD) Timeout.
                Case comEventCTSTO   ' CTS Timeout.
                Case comEventDSRTO   ' DSR Timeout.
                Case comEventFrame   ' Framing Error
                Case comEventOverrun ' Data Lost.
                Case comEventRxOver  ' Receive buffer overflow.
                Case comEventRxParity   ' Parity Error.
                Case comEventTxFull  ' Transmit buffer full.
                Case comEventDCB  ' Unexpected error retrieving DCB]         ' Events
                Case comEvCD   ' Change in the CD line.
                Case comEvCTS  ' Change in the CTS line.
                Case comEvDSR  ' Change in the DSR line.
                Case comEvRing ' Change in the Ring Indicator.
                Case comEvReceive ' Received RThreshold # of chars.
                   Label1.Caption = "Input"
                   InBuff = MSComm1.Input
                   Call ParseChars(InBuff)
                Case comEvSend ' There are SThreshold number of
                               ' characters in the transmit
                               ' buffer.
                Case comEvEOF  ' An EOF character was found in
                               ' the input stream.
             End Select      End Sub      Sub HandleInput(InBuff As String)
             ' This is where you will process your input. This
             ' includes trapping characters, parsing strings,
             ' separating data fields, etc. For this case, you
             ' are simply going to display the data in the text
             ' box.         Text1.Text = Text1.Text & InBuff
          End Sub      Sub ParseChars(ByVal InString As String)
             Dim temp As String
             Dim x As Long
             Dim OutString as String         For x = 1 To Len(InString)
                temp = Mid$(InString, x, 1)
                If temp = Chr$(Xoff) Then
                   Label1.ForeColor = vbRed
                   Label1.Caption = "Xoff received"
                   temp = ""
                ElseIf temp = Chr$(Xon) Then
                   Label1.ForeColor = vbGreen
                   Label1.Caption = "Xon received"
                   temp = ""
                End If
                OutString = OutString & temp
                temp = ""
             Next x
             Call HandleInput(OutString)
          End Sub
      

  4.   

    楼上的谢谢,我就是问comEventBreak   错误的产生往往是什么原因,以及如果处理该错误,就是Case comEventBreak   ' A Break was received.后面怎么写??
    我用的是mscomm1.inbuffercount=0来清除了缓冲区,结果,程序在此处会有短时间的'死掉',稍后继续运行。
    如何处理比较妥当呢?
      

  5.   

    程序死掉跟你是否清除缓冲区没有关系,你应该在接受到停止码之后马上关闭串口,然后处理完数据再开。还有如果你接受的ASC字符,如果遇到VB不能识别的字符,VB会有短暂死机。不可避免。。