Solved

C#, Read serial Stream from Com1

Posted on 2011-03-04
1
1,155 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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now