【问题标题】:Serial port timeout串口超时
【发布时间】:2016-10-01 20:40:31
【问题描述】:

我正在使用带有串行端口通信的 Biomic V3,一切顺利,直到我发送 EOT(传输结束)字符。我正在使用后台工作人员来完成写入工作。没有发生串口超时,应用挂起。

通讯流程:

发送:入队字符

接收:确认字符

发送:标题

接收:确认字符

发送:患者

接收:确认字符

发送:仪器订单

接收:确认字符

发送:页脚

接收:确认字符

发送:传输结束字符(应用程序挂起)

谁能帮我解决这个问题,因为我没有想法! 提前谢谢你!

    #region Static Variables

    private static AutoResetEvent _SendWaitHandle = new AutoResetEvent(false);

    #endregion

    #region Member Variables

    private object _Lock = new object();

    private BackgroundWorker _BackgroundWorker;

    #endregion

    #region Public Methods

    public void Write()
    {
        Monitor.Enter(_Lock);
        try
        {
            // Reset Handle
            _SendWaitHandle.Reset();

            // Send Data Logic

            // Wait for ACK
            if (_SendWaitHandle.WaitOne(1000))
            {
                // Send EOT
            }
            else
            {
                // Timeout Occurred
                // Your Logic To Handle Timeout
            }
        }
        catch (Exception)
        {

            throw;
        }
        finally
        {
            Monitor.Exit(_Lock);
        }
    }

    #endregion

    #region Private Methods

    private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        // When ACK is received call SET
        _SendWaitHandle.Set();

    }


    private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        write();
    }

    #endregion
}

【问题讨论】:

    标签: serial-port timeout freeze


    【解决方案1】:

    我自己做的。向串行端口处理程序添加一个计时器以避免超时

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-16
      相关资源
      最近更新 更多