Solved

C#, Read serial Stream from Com1

Posted on 2011-03-04
1
1,185 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
[X]
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
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

728 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