Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C#, Read serial Stream from Com1

Posted on 2011-03-04
1
1,167 Views
Last Modified: 2012-05-11
Hello
I have a device on my Com1 port, that sends me every 20ms data package with 10 Bytes, starting with a ":" .
To get this data, I open a serial Port.  
serialPort.Open();

Then I have a thread (1 sec cycle time) running that reads one byte after the other.
data = serialPort.ReadByte();

When I receive a ":" (start commando) I read nine Bytes:
serialPort.Read(buffer, 0, 9);

Now my Problem:
My thread reads the data only every second, but the Com1 device sends data every 20 ms.. When I disconect the Com1 device, I still read data on the Com1 port (has still data in its buffer). That means also, that I don't read the latest sent data from the Com1 device.
Can I empty or reset this Com1 buffer?
Or how can I read the latest recived data on the Com1 port?






I have VS2010 and .Net4.0

0
Comment
Question by:ICSAutomation
1 Comment
 
LVL 24

Accepted Solution

by:
alexey_gusev earned 200 total points
ID: 35042066
use DataReceived event to read the data (http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived.aspx)

DiscardInBuffer() clear incoming data buffer
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question