473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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.

1.my 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
4 13323
tlhintoq
3,525 Expert 2GB
May we see the code you are using to open and communicate with the COM port?
May 26 '10 #2
@tlhintoq
the code snippet is as follows
Expand|Select|Wrap|Line Numbers
  1. //for opening the port on a button click
  2.  
  3. if (serialPort3.IsOpen)
  4.      {
  5.          serialPort3.Close();    
  6.      }
  7.      else
  8.      {          
  9.              serialPort3.Open();
  10.      }
  11.  
  12.  
  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
  19.  
  20.  if (serialPort3.IsOpen)
  21.      {
  22.          serialPort3.BaseStream.Dispose();
  23.          serialPort3.Close();
  24.      }
  25.  
  26.  
  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
tlhintoq
3,525 Expert 2GB
Since I have no idea if you are configuring all the properties of a serial port correctly, you might need this:
http://msdn.microsoft.com/en-us/libr...erialport.aspx

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.
http://stackoverflow.com/questions/4...rt-object-in-c
May 26 '10 #4
Plater
7,872 Expert 4TB
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

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

Similar topics

9
by: Mircea | last post by:
Hi everybody, I have a big problem. I am trying to use serial communication in C# 2. The problem is that the event DataReceived is never fired. I have tried on two computers and it does not...
2
by: Padu | last post by:
Hi, I know that the System.IO.Ports.Serial port event for receiving data (DataReceived) happens on a secondary thread, and that if there will be any interaction of that data with the UI, it...
4
by: Adamant | last post by:
Hi All, I'm new to programming for PPC's and .NET, coming from a VB6 / Delphi background. I've been looking around as apparantly there is an easy way to perform serial port comunication in...
2
by: evle | last post by:
haw to read data from an Infrared Infrared Remote Control
0
by: RG | last post by:
I am trying to read from my serial port a 24 bit binary number. I was able to read this number as a HEX but I was getting errors as at times using the vBCrLf indicator. I also can read it as an...
2
by: Lou | last post by:
I have a class that creates an instance of the seril Port. Every thing works fine except whenever I receive data I cannot display the recieved data. I get no errors but the recived data seems to...
2
by: alok | last post by:
Hi I have opened a serial port and then blocked on a select system call . but select returns even if no data is comming from other end. So when I read the buffer after select returns, I only...
2
by: colin | last post by:
Hi, Im having a tiresome amount of trouble with using a bluetooth serial link. The receiving end is a bluetooth-rs232 module conected to my embeded system. The PC has a little usb bluetooth...
0
by: ghjk | last post by:
I want to read sms from GSM modem using C# in serial communication. I wrote the code. But i want to do it automatically. I put my code here and please tell me how can i do it automatically. ...
2
by: mmrasheed | last post by:
Hi, I am newbie in python. I am working on Telit GM862 GPS/GPRS module which has python interpreter built in. But it seems this problem is pretty much related to general python structure. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.