本文介绍了如何在串口c#等待60秒回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码在串口发送和接收数据,发送数据没问题,但是当接收响应数据超过10秒时,会出现错误,因为字符串sRecv = serialPort1.ReadExisting( );数据为null,所以如果前10秒无法接收数据,如何等待接收数据。



i use this code to sent and receive the data in serial port, to sent the data no problem, but while receive respon the data more then 10 second, there will be an error because while string "sRecv = serialPort1.ReadExisting();" the data null, so how to waiting receive the data if first 10 second cannot receive the data.

serialPort1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
  public static string serialBuffer = "";
    static string expectedEcho = null;
    static object expectedEchoLock = new object();
    static ManualResetEvent expectedEchoReceived = new ManualResetEvent(false);

 private void SenContent()
{
SendCommand(sContent); //sent content to serial port
}









i使用此代码在串口发送和接收数据,发送数据没问题,但是虽然接收响应数据超过10秒,但会出现错误,因为字符串sRecv = serialPort1.ReadExisting();数据为null,所以如果前10秒钟无法接收数据,如何等待接收数据。



这只是变量:







i use this code to sent and receive the data in serial port, to sent the data no problem, but while receive respon the data more then 10 second, there will be an error because while string "sRecv = serialPort1.ReadExisting();" the data null, so how to waiting receive the data if first 10 second cannot receive the data.

this just variable :

  serialPort1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
  public static string serialBuffer = "";
    static string expectedEcho = null;
    static object expectedEchoLock = new object();
    static ManualResetEvent expectedEchoReceived = new ManualResetEvent(false);

 private void SenContent()
{
SendCommand(sContent); //sent content to serial port
}





和以下函数发送内容:





and this below function to sent Content :

public bool SendCommand(string item)
    {
        lock (expectedEchoLock)
        {
            expectedEchoReceived.Reset();
            expectedEcho = item;
        }
        serialPort1.Write(item);
        Trace.Trace2(string.Format("sSend : {0}", item));
        return expectedEchoReceived.WaitOne(10000);
    }





然后这个收到数据回复





and then this to receive data reply

public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
   {
        if (serialPort1.BytesToRead == 113) //i need only byte 113 so i use '==3'
       {
           sRecv = serialPort1.ReadExisting();
           serialPort1.Write(((char)6).ToString()); //sent ACK
       }

   }

推荐答案

private void
Reader
(
    object Stream
)
{
    byte[] buffer = new byte [ this.port.ReadBufferSize ] ;

    PIEBALD.Types.StreamReader stream = new StreamReader
        ( (System.IO.Stream) Stream , buffer.Length ) ;

    while ( !this.abort )
    {
        int len = stream.Read ( buffer , 0 , buffer.Length ) ;

        if ( len > 0 )
        {
            this.RaiseDataReceived ( this.Encoding.GetString ( buffer , 0 , len ) ) ;
        }
        else
        {
            System.Threading.Thread.Sleep ( 100 ) ;
        }
    }

    return ;
}





我打开端口后,我会在Reader线程中产生:





After I open the port I spawn off the Reader thread:

this.stream = this.port.BaseStream ;

this.reader = new System.Threading.Thread(this.Reader)
{
    Priority = System.Threading.ThreadPriority.BelowNormal
,
    IsBackground = true
} ;

this.reader.Start ( this.stream ) ;







我无法相信我仍然还没有写完我的SerialCommunicator文章......:mad:




I can't believe I still haven't finished writing my SerialCommunicator article... :mad:


这篇关于如何在串口c#等待60秒回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 16:50