串行通信期间的流量控制

时间:2014-01-18 16:39:09

标签: .net vb.net serial-port flow-control

我设计了一个应用程序,通过USB->串行适配器(Windows 7 64位,VS2013,目标.Net Framework 3.5 SP1)与我的开发盒上的外部设备进行通信。我还有一个带有Windows XP SP3和.Net Framework 3.5 SP1的本机串口的测试盒设置。外部设备以19200,N,8,1进行通信,并支持硬件流控制。我知道流量控制有效,因为当我在设备上启动报告时,将流量控制设置为无,并手动创建一个复选框以打开和关闭DTR设备将在DTR关闭时暂停。

我无法通过我的Windows桌面应用程序自动完成硬件流控制(Handshake.RequestToSend)。如果我将握手设置为无,则通信“通常”成功。有时在具有本机串行端口的较慢测试盒上,我相信当从设备请求大量数据时接收缓冲区溢出,即使我已经设置了一个非常大的缓冲区并使DataReceived例程尽可能高效。我在驱动程序中尝试了各种流量控制设置,没有任何效果。如果我在我的应用程序中将握手设置为RequestToSend,则通信似乎停止并且设备永远不会响应我的初始状态请求。我尝试过各种True / False与DtrEnable和RtsEnable的组合无济于事。我在互联网上搜索了好几个小时,还没有看到任何迄今为止有帮助的东西。这是我的一些代码:

  Private sbReceive_Extract As New StringBuilder 'Receive buffer

  Private WithEvents comPort As SerialPort

  Private bReady as Boolean = False 'data ready flag

  Private bExtract as Boolean = False 'type of data request

  Public Sub Status_Request()

    sbReceive_Extract.Length = 0 'clear buffer

    If Open_Port() = True

      comPort.DiscardInBuffer()
      comPort.DiscardOutBuffer()

      'Send status request to external serial device
      comPort.Write(Chr(27) & "?" & Chr(13))

      bExtract = False 'Just get status header this time    

      'Loop until device responds to request
      bReady = False
      Do Until bReady = True
        Application.DoEvents()
        'update an onscreen status label as buffer fills
        lblStatus.Text = "Buffer read: " & sbReceive_Extract.Length.ToString
      Loop

    End If

  End Sub


  Public Function Open_Port() As Boolean

      'return code for port open or closed
    Dim bReturnResult As Boolean = False

    If comPort Is Nothing Then
      comPort = New SerialPort
    End If

    Try
      With comPort
        .PortName = "COM1"       'comm port 1
        .BaudRate = 19200  'transfer speed
        .DataBits = 8       '8 bits
        .StopBits = StopBits.One
        .Parity = Parity.None
        .ReadBufferSize = 32768 '32768=size of buffer in bytes
        .ReadTimeout = SerialPort.InfiniteTimeout
        .Handshake = Handshake.RequestToSend 'works when set to none
        .DtrEnable = True 'tried both true/false
        .RtsEnable = True 'tried both true/false
        .Encoding = Encoding.Default
        .NewLine = Chr(13) '13=carriage return
        .ReceivedBytesThreshold = 1
      End With

      If comPort.IsOpen = False Then
        comPort.Open()
      End If

      If comPort.IsOpen = True Then
        bReturnResult = True
      Else
        bReturnResult = False
      End If

    Catch Ex As Exception
      bReturnResult = False
    End Try

    Return bReturnResult

  End Function

Private Sub comVoteExt_DataReceived(ByVal sender As SerialPort,     ByVal eArgs As SerialDataReceivedEventArgs)     处理comVoteExt.DataReceived

Dim sData As Integer = 0 'integer to hold serial data

Try
  Do
    sData = sender.ReadChar 'take one char off the stack
    sbReceive_Extract.Append(Chr(sData))
    If bExtract = True Then
      'Check for data
      If iLength = 0 Then
          'hex 04 is end of data on tally stream
        If Chr(sData) = Chr("&H04") Then
          iLength = sbReceive_Extract.Length
        End If
      Else
           '6 bytes reserved for checksum and carriage return
        If sbReceive_Extract.Length >= iLength + 6 Then
            'Meets checksum length. Ready for next stage.
          bReady = True
        End If
      End If
    Else
        '20 bytes of header information 
      If sbReceive_Extract.Length > 20 Then
          'Data is ready for next stage. Minimum header length met.
        bReady = True
      End If
    End If
  Loop Until sender.BytesToRead = 0
Catch exc As Exception
End Try

End Sub

看起来有什么事情可能导致我的握手问题吗?有没有办法在端口打开后“唤醒”设备,以便开始通信?在端口打开后我尝试将DTREnable设置为true,这似乎并不重要。我很难过。

0 个答案:

没有答案