Click here to Skip to main content
15,570,202 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using C# Framework 4.0 for Serial Port communication.
I have defined ReceivedBytesThreshold as 51204; Initially the port is opened for low baud rate as 921600 for some purpose. After doing that Serial port is closed and disposed and reopened for some other task using higher Baud rate 1228800.

When number of bytes received is 51204, the DataReceivedHandler is fired.
private static void DataReceivedHandler(
                    object sender,
                    SerialDataReceivedEventArgs e)
    SerialPort sp = (SerialPort)sender;
    Console.WriteLine (sp.BytesToRead());

The problem is "BytesToRead" function is no more showing full bytes. It shows values like 51174, 51123 etc. less than 51204.

I have changed ReadBufferSize as well, but there is no effect on account of that.
When I used to open Serial port only once at Baud rate 1228800, the application works fine...but opening serial port for baud rate 921600 is necessary to carry out some task initially.
What can be done to receive full bytes?
Updated 14-Sep-15 23:16pm
Leo Chapiro 15-Sep-15 4:23am    
So you just need to increase the ReceivedBytesThreshold ?
Wahaj Khan 15-Sep-15 6:18am    
Sorry...If we increase the ReceivedBytesThreshold value above 51204, the Data Received event will never fire as no more than 51204 bytes will be received
[no name] 15-Sep-15 4:53am    
See[^], remarks:

"The DataReceived event is also raised if an Eof character is received, regardless of the number of bytes in the internal input buffer and the value of the ReceivedBytesThreshold property."

maybe it helps.
Philippe Mori 15-Sep-15 12:37pm    
There might be some timeout too... Does the received data is the beginning of the block with missing data at the end or there are hole in the data.

Using hardware or software to inspect data send and received can help a lot to ensure that you get the desired data.

1 solution

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