Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / July 2006

Tip: Looking for answers? Try searching our database.

SerialPort DataReceived Event Doesn't Fire

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Otis Mukinfus - 24 Jul 2006 00:31 GMT
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
Peter Ritchie [MVP] - 24 Jul 2006 15:45 GMT
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.

Signature

http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#

> I have been unable to get the DataReceived event of the SerialPort component to
> fire.
[quoted text clipped - 24 lines]
> http://www.arltex.com
> http://www.tomchilders.com
Otis Mukinfus - 25 Jul 2006 11:49 GMT
>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
Otis Mukinfus - 25 Jul 2006 11:51 GMT
>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
Peter Ritchie [C# MVP] - 25 Jul 2006 14:35 GMT
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.
Signature

http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#

> >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
[quoted text clipped - 18 lines]
> http://www.arltex.com
> http://www.tomchilders.com

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.