Net 2 Serialport OnDataReceived

L

Lars Siden

Hi,

I've really struggled with this. Using Hyperterminal everything works fine.
But if I write my own program I can't get "OnDataReceived" to be fired.

MS samples from the SDK gives me the same result. I can read data by waiting
and whiling - but that's just bad. I want to use the Event!

I get the same problem in Winforms and in a Console app.

I Use Vista 64 bit ( but as I said, works fine in Hyperterminal ).

I've tried VS 2005 and VS 2008B2.

Here is the code for my simplified console app: <<-- Please HELP :)

-------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;

namespace conSer
{
class Program
{
static public SerialPort sp;

static void Main(string[] args)
{
sp = new SerialPort("COM3", 115200, Parity.None, 8, StopBits.One);
sp.DtrEnable = true;

sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);

sp.Open();

Console.WriteLine("Port open\r\n");

bool going = true;

while (going)
{
string cmd = Console.ReadLine();

if (cmd.Equals("info"))
{
Console.WriteLine("Read: " + sp.BytesToRead.ToString() + " Write: " +
sp.BytesToWrite.ToString() + "\r\n");
}

if (cmd.Equals("bye"))
going = false;
else
sp.Write(cmd);
}

if (sp.IsOpen)
sp.Close();
sp.Dispose();

}

static void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Console.WriteLine("Reading");
Console.WriteLine( sp.ReadExisting() );
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top