Search code examples
c#serial-portxbee

code not reading from serial port


Good day,

I have tried everything to read a few strings from my xbee module on csharp. but my code keeps telling me the serial port is not open when it reaches the event handler. any help would be appreciated greatly. thanks string display = myserial.ReadLine();

Operation Exception

using System;
using System.Management;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
namespace ConsoleApplication2
{
    class Program
    {
        public static  SerialPort myserial = new SerialPort();
        public string display;
        static void Main(string[] args)
        {
            string[] ports = SerialPort.GetPortNames();
            foreach (string p in ports)
            {
                Console.WriteLine(p);
            }
            SerialPort myserial = new SerialPort();

            myserial.BaudRate = 9600;
            myserial.Parity = Parity.None;
            myserial.StopBits = StopBits.One;
            myserial.DataBits = 8;
            myserial.Handshake = Handshake.None;
            myserial.RtsEnable = true;
            myserial.DtrEnable = true;
            myserial.ReadTimeout = 100000;
            myserial.PortName = "COM3";
            myserial.ReadTimeout = 10000;
            myserial.DataReceived += new SerialDataReceivedEventHandler(DataRecievedHandler);
            myserial.Open();
            if (myserial != null)
            {
                if (myserial.IsOpen)
                {
                    Console.WriteLine("connected");
                }
            }
            Console.ReadLine();
        }
   static void DataRecievedHandler(object sender, SerialDataReceivedEventArgs e)
        {
           string display = myserial.ReadLine();
        }

    }
} 

Solution

  • Your problem is that you have an ambiguity in your code. 2 Variables with the same name.

    The class variable that you declare outside the main:

    class Program
    {
        public static  SerialPort myserial = new SerialPort();
    

    and the variable inside the main method:

    static void Main(string[] args)           
    {
        SerialPort myserial = new SerialPort();
    

    Inside the method the compiler will take the local variable myserial. You open it and register the event:

    myserial.DataReceived += new SerialDataReceivedEventHandler(DataRecievedHandler);
    

    So far everything is fine. But outside the Main method this SerialPort myserial does not exist. That means when you try to access myserial inside the DataRecievedHandler method the compiler "thinks" that you mean the first variable on class level! But this SerialPort has never been opened! Therefore it gives you the error.

    You can solve it by using the sender object inside the event. Since the open SerialPort fires this event:

    static void DataRecievedHandler(object sender, SerialDataReceivedEventArgs e)
    {  
        SerialPort port = sender as SerialPort;
    
        if(port != null)
        {    
            string display = port.ReadLine();
        }
    }
    

    Note: This variable display exist only inside the DataRecievedHandler method. You cannot use it in the main. Because you declare it again. This is a local variable which is not the same as you have declared on class level! remove the string and the class level variable will be used:

    make it:

    display = port.ReadLine();
    

    2.

    You can also solve it by simply removing the declaration of the SerialPort myserial variable inside the Main method. Probably would be simpler ;) Just remove this line inside the Main method:

    SerialPort myserial = new SerialPort();