Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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 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 :

C#
  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 :

C#
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

C#
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
       }

   }
Posted
Updated 20-Sep-15 5:37am
v2
Comments
Ralf Meier 20-Sep-15 11:42am    
Your Solution will block your application when "waiting" is required.
I won't do it like this.

I understood the following :
- You send data to the SerialPort.
- The Device which is connected to the SerialPort doesn't answer imidiatly - so you have to wait a while ...
- the answer is complete if 113 bytes are received ...?

Why don't you store the data on each DataReceived in a seperate String (append it on each Receive) and ask if the necessary number of bytes are in the string ?

1 solution

I have found that performing the reads on a separate thread works quite well.
I do that whether I'm communicating over serial, socket, or stream.


Here's the Reader method for my SerialCommunicator:

C#
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 ;
}


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

C#
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 ) ;



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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900