Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C#, Read serial Stream from Com1

Posted on 2011-03-04
1
Medium Priority
?
1,216 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 800 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month11 days, 2 hours left to enroll

885 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