Esempio n. 1
0
        /// <summary>
        /// Connect to the serial port.
        /// This will set the serial port based off the serial options.
        /// It will then try to open the serial port.  If it cannot be opened,
        /// it will get an exception and return false.  The error will be logged.
        /// </summary>
        /// <returns>TRUE = Connection could be made.  / FALSE = Conneciton could not be made.</returns>
        public bool Connect()
        {
            // Make sure serial port is set
            if (!string.IsNullOrEmpty(_serialOptions.Port))
            {
                _serialPort                = new SerialPort(_serialOptions.Port);
                _serialPort.BaudRate       = _serialOptions.BaudRate; // Set baud rate
                _serialPort.DataBits       = _serialOptions.DataBits; // Set Data bits
                _serialPort.Parity         = _serialOptions.Parity;   // Set Parity
                _serialPort.StopBits       = _serialOptions.StopBits; // Set stop bits
                _serialPort.Handshake      = Handshake.None;          // No handshake (Flow Control None)
                _serialPort.ReadBufferSize = 16 * 65536;
                ReceiveBufferString        = "";                      // Clear the buffer

                //Set the read/write timeouts
                _serialPort.ReadTimeout  = 50;
                _serialPort.WriteTimeout = 500;

                // Check if Open() has already been called on this serial port object
                if (!_serialPort.IsOpen)
                {
                    // Open the serial port and remove old data
                    try
                    {
                        _serialPort.Open();

                        // If the connection is opened, start the thread
                        if (_serialPort.IsOpen)
                        {
                            if (!_readThread.IsAlive)
                            {
                                _readThread          = new Thread(new ParameterizedThreadStart(ReadThreadMethod));
                                _readThread.Priority = ThreadPriority.Normal;
                                _readThread.Name     = "Serial Connection";
                                _readThread.Start();
                            }
                        }

                        // Discard any old data in buffer
                        _serialPort.DiscardInBuffer();
                        Debug.WriteLine("SerialConnection.Connect(): " + _serialOptions.ToString());
                    }
                    catch (System.UnauthorizedAccessException ex)
                    {
                        // Port is already in use
                        log.Warn("COMM Port already in use: " + _serialOptions.Port, ex);
                        Debug.WriteLine("COMM Port already in use: " + _serialOptions.Port);
                        Disconnect();
                        return(false);
                    }
                    catch (System.ArgumentOutOfRangeException ex_range)
                    {
                        // Not sure what is causing this exception yet
                        log.Error("Error COMM Port: " + _serialOptions.Port, ex_range);
                        Debug.WriteLine("Error COMM Port: " + _serialOptions.Port);
                        Disconnect();
                        return(false);
                    }
                    catch (System.IO.IOException io_ex)
                    {
                        // The Serial port does not exist
                        log.Debug(string.Format("Error COM Port: {0} does not exist.", _serialOptions.Port), io_ex);
                        Debug.WriteLine(string.Format("Error COM Port: {0} does not exist.", _serialOptions.Port));
                        Disconnect();
                        return(false);
                    }
                    catch (Exception ex)
                    {
                        log.Error("Error Opening in COMM Port: " + _serialOptions.Port, ex);
                        Debug.WriteLine("Error Opening in COMM Port: " + _serialOptions.Port);
                        Disconnect();
                        return(false);
                    }
                }

                // If the serial port is already open, return true
                return(true);
            }

            // Serial port could not be opened
            // Either a bad string or a string not given for the port
            Disconnect();
            return(false);
        }
Esempio n. 2
0
        /// <summary>
        /// Display the object to a string.
        /// </summary>
        /// <returns>String of the object.</returns>
        public override string ToString()
        {
            string result = SerialOptions.ToString();

            return(result);
        }