Inheritance: IDisposable
        /*
        protected override void Dispose(bool disposing)
        {
            try
            {
                try
                {
                    Type mytype = typeof(System.IO.Ports.SerialPort);
                    FieldInfo field = mytype.GetField("internalSerialStream", BindingFlags.Instance | BindingFlags.NonPublic);

                    if (field != null)
                    {
                        Stream stream = (Stream)field.GetValue(this);

                        if (stream != null)
                        {
                            try
                            {
                                stream.Dispose();
                            }
                            catch (Exception ex) { Console.WriteLine("1 " + ex.ToString()); }
                            stream = null;
                        }
                    }
                }
                catch (Exception ex) { Console.WriteLine("2 " + ex.ToString()); }

                base.Dispose(disposing);
            }
            catch (Exception ex) { Console.WriteLine("3 " + ex.ToString()); }
        }
        */
        public new void Open()
        {
            // 500ms write timeout - win32 api default
            this.WriteTimeout = 500;

            if (base.IsOpen)
                return;

            try
            {
                // this causes element not found with bluetooth devices.
                if (BaudRate > 115200)
                {
                    Console.WriteLine("Doing SerialPortFixer");
                    SerialPortFixer.Execute(this.PortName);
                    Console.WriteLine("Done SerialPortFixer");
                }
            }
            catch (Exception ex) { Console.WriteLine(ex.ToString()); }

            if (PortName.StartsWith("/"))
                if (!File.Exists(PortName))
                    throw new Exception("No such device");

            try
            {
                base.Open();
            }
            catch {
                try { base.Close(); }
                catch { }
                throw;
            }
        }
Esempio n. 2
0
        /*
         * protected override void Dispose(bool disposing)
         * {
         *  try
         *  {
         *      try
         *      {
         *          Type mytype = typeof(System.IO.Ports.SerialPort);
         *          FieldInfo field = mytype.GetField("internalSerialStream", BindingFlags.Instance | BindingFlags.NonPublic);
         *
         *          if (field != null)
         *          {
         *              Stream stream = (Stream)field.GetValue(this);
         *
         *              if (stream != null)
         *              {
         *                  try
         *                  {
         *                      stream.Dispose();
         *                  }
         *                  catch (Exception ex) { Console.WriteLine("1 " + ex.ToString()); }
         *                  stream = null;
         *              }
         *          }
         *      }
         *      catch (Exception ex) { Console.WriteLine("2 " + ex.ToString()); }
         *
         *      base.Dispose(disposing);
         *  }
         *  catch (Exception ex) { Console.WriteLine("3 " + ex.ToString()); }
         * }
         */
        public new void Open()
        {
            // 500ms write timeout - win32 api default
            this.WriteTimeout = 500;

            if (base.IsOpen)
            {
                return;
            }

            try
            {
                Console.WriteLine("Doing SerialPortFixer");
                SerialPortFixer.Execute(this.PortName);
                Console.WriteLine("Done SerialPortFixer");
            }
            catch (Exception ex) { Console.WriteLine(ex.ToString()); }

            if (PortName.StartsWith("/"))
            {
                if (!File.Exists(PortName))
                {
                    throw new Exception("No such device");
                }
            }

            try
            {
                base.Open();
            }
            catch {
                try { base.Close(); }
                catch { }
                throw;
            }
        }