SerialPort DataReceived Event Doesn't Fire

  • Thread starter Thread starter Otis Mukinfus
  • Start date Start date
O

Otis Mukinfus

I have been unable to get the DataReceived event of the SerialPort component to
fire.

Here is a sample of the code I found on the web that is said to work around a
bug in this control, but if the event doesn't fire, I don't see how it can do
anything.

private void DoUpdate(object s, EventArgs e)
{

Debug.WriteLine("Data Received");

}

private void orionSerialPort_DataReceived(object sender,
System.IO.Ports.SerialDataReceivedEventArgs e)
{
this.Invoke(new EventHandler(DoUpdate));
}

Anyway, that doesn't solve the problem.

Anyone have experience with the SerialPortComponent for .NET 2.0?
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
Might help if you provide some code that creates the orionSerialPort object,
opens the port and assignes the event handler... The existance of a method
that may or may not be uses really doesn't offer much to provide guidance
upon.
 
Might help if you provide some code that creates the orionSerialPort object,
opens the port and assignes the event handler... The existance of a method
that may or may not be uses really doesn't offer much to provide guidance
upon.

Here it is...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using N5GE.Radio;

namespace N5GE.TestWindow
{
public partial class Form1 : Form
{
private Orion _orion;
private bool On20 = false;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
_orion = new Orion("Com3", 57600, System.IO.Ports.Parity.None,
8, System.IO.Ports.StopBits.One);
orionSerialPort = _orion.SerialPort;
orionSerialPort.Open();
orionSerialPort.Handshake =
System.IO.Ports.Handshake.RequestToSendXOnXOff;
orionSerialPort.DtrEnable = true;

_orion.SetFrequency(VFO.A, 21.025000m, FrequencySettingType.Absolute);
_orion.SetFrequency(VFO.B, 21.025000m, FrequencySettingType.Absolute);
_orion.QueryVFO(VFO.A);

}

private void orionSerialPort_DataReceived(object sender,
System.IO.Ports.SerialDataReceivedEventArgs e)
{
this.Invoke(new EventHandler(DoUpdate));
}

private void orionSerialPort_ErrorReceived(object sender,
System.IO.Ports.SerialErrorReceivedEventArgs e)
{
Debug.WriteLine("Error Received");
}

private void ExecuteButton_Click(object sender, EventArgs e)
{
try
{
if (On20)
{
_orion.SetFrequency(VFO.A, 21.250000m, FrequencySettingType.Absolute);
_orion.SetFrequency(VFO.B, 21.250000m, FrequencySettingType.Absolute);
_orion.QueryVFO(VFO.A);
On20 = false;
}
else
{
_orion.SetFrequency(VFO.A, 14.250125m, FrequencySettingType.Absolute);
_orion.SetFrequency(VFO.B, 14.250125m, FrequencySettingType.Absolute);
_orion.QueryVFO(VFO.A);
On20 = true;
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (orionSerialPort.IsOpen)
{
orionSerialPort.Close();
}
}
private void DoUpdate(object s, EventArgs e)
{

Debug.WriteLine("Data Received");

}
}
}

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
Might help if you provide some code that creates the orionSerialPort object,
opens the port and assignes the event handler... The existance of a method
that may or may not be uses really doesn't offer much to provide guidance
upon.

Sorry Peter,

the event handler is assigned by VS 2005 generated code:

//
// orionSerialPort
//
this.orionSerialPort.DataReceived += new
System.IO.Ports.SerialDataReceivedEventHandler(this.orionSerialPort_DataReceived);
this.orionSerialPort.ErrorReceived += new
System.IO.Ports.SerialErrorReceivedEventHandler(this.orionSerialPort_ErrorReceived);

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
You don't show *where* "this.orionSerialPort.DataReceived +=" is, you only
mention that it is generated by VS 2005, so I'm assuming it's in
InitializeComponent. I'm guessing; but you've probably added a SerialPort
object to the form in the Designer and I'm assuming Orion.SerialPort is also
of type SerialPort.

If the above is all (or mostly) true, then what's happening is you're
replacing the orionSerialPort object after the InitializeComponent in your
Load handler. After that, this new SerialPort object has no data event
handlers assigned, hence you get no events.

InitializeComponent will always create a new SerialPort object and assign
the event handlers, if you've dropped it on the form. Meaning it will always
assign the event handlers to the wrong object. Really the only thing to do
is explicitly define the orionSerialPort object (i.e. not in the designer)
and explicitly hook up your event handlers (usually after InitializeComponent
in the c'tor; but, I don' think it matters with these objects. Then, you
won't be able to visually design the orionSerialPort.
 
Back
Top