By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,137 Members | 2,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,137 IT Pros & Developers. It's quick & easy.

Serial port buffer is having old data(data from the previous run)

P: 3
I have developed a GUI based application in C# for communicating with a 16 bit littleendian microcontroller device .the device has a serial port interface. appplication opens a COM port on which the device is connected with proper settings for the device.
2.It writes a command to the port.
3.datareceived event of the serial port reads the data in to a byte array.
3.i have closed the serialport using close() method
The problem that i m facing is,
1.Immediately after opening the port (before giving any request to the device)i am getting the data on serialport)this data is the data that i was receiving in the previous run of the application.
2For any command i am writing to the serial port (to make changes to the reply)There is a delay before getting the modified reply .During this delay i am receiving the old data(without modifications)
3.After switching off the device i am receiving the data for sometime

Is this is because of the uncleared buffer?
I have tried with Basestream.flush() before writing a command to the port.
May 26 '10 #1
Share this Question
Share on Google+
4 Replies

Expert 2.5K+
P: 3,525
May we see the code you are using to open and communicate with the COM port?
May 26 '10 #2

P: 3
the code snippet is as follows
Expand|Select|Wrap|Line Numbers
  1. //for opening the port on a button click
  3. if (serialPort3.IsOpen)
  4.      {
  5.          serialPort3.Close();    
  6.      }
  7.      else
  8.      {          
  9.              serialPort3.Open();
  10.      }
  13. private void serialPort3_DataReceived(object sender, SerialDataReceivedEventArgs e)
  14.  {
  15.      Control.CheckForIllegalCrossThreadCalls = false;    
  16.      int b = serialPort3.Read(bf, 0, 36);
  17.  }
  18. //for closing the port
  20.  if (serialPort3.IsOpen)
  21.      {
  22.          serialPort3.BaseStream.Dispose();
  23.          serialPort3.Close();
  24.      }
  27. //button click writing a packet containing my request to the port   
  28. {
  29.        byte[] packet = Gen.generatePacket(16, header, data, trial);
  30.        serialport3.write(packet,0,packet.length);
  31. }
May 26 '10 #3

Expert 2.5K+
P: 3,525
Since I have no idea if you are configuring all the properties of a serial port correctly, you might need this:

You seem to have an event handler for the DataReceived event. But it is only reading in to a single int. It needs to read into a buffer so it can store everything coming in, not just one number.
May 26 '10 #4

Expert 5K+
P: 7,872
The read call is reading into a buffer bf (and only reading 36 characters at max)
You need a little better logic there since the datarecieved event is probably being fired after every byte. If you are using a static buffer like that you would need to adjust your starting point index and data you allow to read.
That said, the serialport object already keeps the data buffered in the stream, so you don't need to read it off as fast as it comes in (unless you are going to overflow the serialport's buffer.)
May 26 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.