Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey everyone.

I've a question to ask.

I'm wondering that the DataReceived event is automatically raised whenever a data comes from serialPort? I've read but couldn't find info here:

http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived.aspx[^]

I've written a little program to read data every 3 seconds from serial port, but that doesn't work quiet well.

The code is below:

string buffer;

SerialPort sp = new SerialPort("COM1");

sp.BaudRate = 9600;
sp.Handshake = Handshake.None;

// Handler
sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
    buffer= sp.ReadExisting();
}

I don't want to use Timer, because the data doesn't always arrive in 3 second intervals. So, I can't get precise datas.

I know this's a simple question, but I'm stuck...
Any idea how to read with intervals?
My best regards...
Posted
Updated 5-Jun-18 18:05pm

1 solution

If you want to read at specific intervals, then use a Timer.
If you want to process data as it comes in, use DataRecieved.

But be aware that DataReceived is fired every time a byte arrives - so the data you read is likely to be a single character each time, and "buffer" will never hold an entire message if you overwrite it every time you handle the event.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900