locked
System.IO.Ports.SerialDataReceivedEventHandler Not Working For Me RRS feed

  • Question

  • I can't figure out why this event is not being raised in my application. This is not my actual app but it is a simplified application that i created to try and figure out why my application is not working and even the simplified app doesnt work.  I am attempting to trigger the event with a device called the NetCallerID which is a caller ID that sends its data to the serial port of my PC.  I have tested the hardware in HyperTerminal and it works fine.  Am I doing something wrong or is there a BUG with this feature? The events method is never triggering.




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

    namespace ConsoleApplication1
    {
        class Program
        {
            SerialPort port = new SerialPort("COM3", 4800, Parity.None, 8, StopBits.One);

            public Program()
            {
            
                port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived);

                port.Open();

               
            }
           

            static void Main(string[] args)
            {   
                string one = Console.ReadLine();
            }

            public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                Console.WriteLine(port.ReadExisting());
            }
        }
    }


     

    Sunday, July 10, 2005 1:48 AM

Answers

  • The program you posted will not work because the Program constructor is never called, therefore you never subscribe to the event.  Try this...



    static void Main(string[] args)
    {
       Program theProgram = new Program();
       string one = Console.ReadLine();
    }

     
    Tuesday, July 12, 2005 6:52 PM

All replies

  • the code looks fine. Are you sure you don't need to send any inputs to the device before you can start to revice information from it? Do you just connect with Hyper terminal and pass no argument?

    Regards,
    Saurabh Nandu
    www.MasterCSharp.com
    www.AksTech.com
    Monday, July 11, 2005 7:38 AM
  • Yes, I am sure that I do not have to send any commands to the ID first.  When using HyperTerminal I just open the connection and make the phone call. 

    I'm not at my code right now so I can't test this for my self but, is the port.open() losing focus(Out of Scope) and is the connection being terminated.  I noticed in this applicaition I am opening the port in the constructor.  In my real application I was opening the Port in a public method.  Do i need to call port.Open() in a place where it will always be in scope?  I am instatiating my SerialPort object in the scope of the class so the object stays live until the program terminates this would make me think that scope is not my problem. 

    Is there any place else i may be able to ask this question and get more help?
    Monday, July 11, 2005 11:25 AM
  • Someone told me that the ReadLine() function would work without the eventhandler.  Can anybody give me a simple example of how that would work.  How would I know when to check for information.  For example if I populated a string with the information from the readline()  How would I tell my ProccessData function that I wrote that the information in the string has changed and that it needs to trigger the ProccessData function to extract the individual components of the data such as First Name, Last Name, Ect. 

    Tuesday, July 12, 2005 4:38 PM
  • The program you posted will not work because the Program constructor is never called, therefore you never subscribe to the event.  Try this...



    static void Main(string[] args)
    {
       Program theProgram = new Program();
       string one = Console.ReadLine();
    }

     
    Tuesday, July 12, 2005 6:52 PM