Solved

C#, Read serial Stream from Com1

Posted on 2011-03-04
1
1,164 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

832 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