none
new SerialPort() caused exception RRS feed

  • Question


  • BaseStream = '_serialPort.BaseStream' threw an exception of type 'System.InvalidOperationException'

    Hi, I am working on serial port access using SerialPort() class. After the new statement, there is a InvalidOperationException occurred all the time. I am not sure about how it happened. We are using WEC 2013 update 5 on a NPX ARM processor.

    Thanks,

    Dick

    Here is my code -

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
     
    using System.IO.Ports;
    using System.Threading;
     
     
     
     
    namespace UartTEST
    {
        class Program
        {
     
            static bool _continue;
            static SerialPort _serialPort;
     
     
            static void Main(string[] args)
            {
                Console.WriteLine("Welcome to C# on Windows Embedded Systems");
     
                string name;
                string message;
                StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
                Thread readThread = new Thread(Read);
     
                // Create a new SerialPort object with default settings.
                _serialPort = new SerialPort();
     
                // Allow the user to set the appropriate properties.
                _serialPort.PortName = "COM9";                    
                _serialPort.BaudRate = 115200;
                _serialPort.Parity = Parity.None;
                _serialPort.DataBits = 8;
                _serialPort.StopBits = StopBits.One;
                _serialPort.Handshake = Handshake.None;
     
                // Set the read/write timeouts
                _serialPort.ReadTimeout = 500;
                _serialPort.WriteTimeout = 500;
     
                _serialPort.Open();
                _continue = true;
                readThread.Start();
     
                Console.Write("Name: ");
                name = Console.ReadLine();
     
                Console.WriteLine("Type QUIT to exit");
     
                while (_continue)
                {
                    message = Console.ReadLine();
     
                    if (stringComparer.Equals("quit", message))
                    {
                        _continue = false;
                    }
                    else
                    {
                        _serialPort.WriteLine(
                            String.Format("<{0}>: {1}", name, message));
                    }
                }
     
                readThread.Join();
                _serialPort.Close();
     
            }
     
            public static void Read()
            {
                while (_continue)
                {
                    try
                    {
                        string message = _serialPort.ReadLine();
                        Console.WriteLine(message);
                    }
                    catch (TimeoutException) { }
                }
            }
     
            public static string SetPortName(string defaultPortName)
            {
                string portName;
     
                Console.WriteLine("Available Ports:");
                foreach (string s in SerialPort.GetPortNames())
                {
                    Console.WriteLine("   {0}", s);
                }
     
                Console.Write("COM port({0}): ", defaultPortName);
                portName = Console.ReadLine();
     
                if (portName == "")
                {
                    portName = defaultPortName;
                }
                return portName;
            }
     
            public static int SetPortBaudRate(int defaultPortBaudRate)
            {
                string baudRate;
     
                Console.Write("Baud Rate({0}): ", defaultPortBaudRate);
                baudRate = Console.ReadLine();
     
                if (baudRate == "")
                {
                    baudRate = defaultPortBaudRate.ToString();
                }
     
                return int.Parse(baudRate);
            }
     
        }
    }
    


    milkfish

    Wednesday, April 22, 2015 7:30 PM

All replies

  • When you state "After the new statement", what do you mean by "After"?   Does that mean after the new statement, but before the next statement; does it mean just sometime after, but I don't know when; does it mean I know when, but I didn't state it?

    Bruce Eitman (eMVP) Senior Engineer Bruce.Eitman AT Eurotech DOT com My BLOG http://geekswithblogs.net/bruceeitman Eurotech Inc. www.Eurotech.com

    Thursday, April 23, 2015 1:20 PM
    Moderator
  • Sorry for the confusion. I should say step over the new statement code, the I check the local variable value.

    Thanks.


    milkfish

    Thursday, April 23, 2015 1:34 PM
  • And we are using Freescale ARM processor.

    milkfish

    Thursday, April 23, 2015 1:36 PM