473,765 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

3 New Member
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.flus h() before writing a command to the port.
May 26 '10 #1
4 13409
tlhintoq
3,525 Recognized Expert Specialist
May we see the code you are using to open and communicate with the COM port?
May 26 '10 #2
mayaanu
3 New Member
@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 Recognized Expert Specialist
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 Recognized Expert Expert
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
14268
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 work at all. I have tried also in C++ .NET 2005 and also does not work. I did not find any useful information on the net. Do you have a small working C# program about how to setup and use the COM1
2
3468
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 should be done through invoke. Right now my application receives serial data and a class hierarchy is responsible for handling it. If I run the application, everything seems to work fine, but if I put a breakpoint on it and try to follow, the...
4
5498
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 VB.net (2005) using the .Net Compact Framework 2.0. I've seen a lot of third-party components about but am reluctant to
2
3792
by: evle | last post by:
haw to read data from an Infrared Infrared Remote Control
0
2007
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 incoming string looking for the vbCrLf indicator but it too gave me errors as the length of the stream was not always 24 bits for some reason. I have to slow the output down to reduce the amount of garbled data being sent to the serial port.
2
6184
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 just go no where. I can see the recived data in my serial receive function but when I either raise an event with it or try to display it in a text box nothing happens. I do use beginInvoke on the text box. If I trace the code through it all...
2
2182
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 found 0s nad this continues . So the problem is select returns even if there is no data in serial port buffer. void main() {
2
5431
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 dongle, ive tried a another dongle with widicom software as I was having problems with the IVT bluesoleil software. that seems to have got rid of some other problems,
0
4566
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. public partial class SMS : Form { //create an Serial Port object SerialPort sp = new SerialPort(); public SMS() {
2
10401
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 need a promt/terminal when the device is connected to PC. If user enters a command by serial port and press "Enter" then the data is read by the device and work on the command. This is similar to readline() function. Unfortunately there is no...
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10163
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9957
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8832
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.