예제 #1
0
        public static bool ConfigPort(System.IO.Ports.SerialPort _serialPort, SerialPortConfig _serialPortConfig = default(SerialPortConfig))
        {
            Console.WriteLine(" Port: " + _serialPort.PortName);
            if (_serialPortConfig == null)
            {
                _serialPortConfig = new SerialPortConfig();
            }

            bool portOpened = false;

            try
            {
                _serialPort.DataBits        = _serialPortConfig.DataBits;
                _serialPort.StopBits        = _serialPortConfig.StopBits;
                _serialPort.Parity          = _serialPortConfig.Parity;
                _serialPort.Handshake       = _serialPortConfig.Handshake;
                _serialPort.WriteBufferSize = _serialPortConfig.WriteBufferSize;
                _serialPort.ReadBufferSize  = _serialPortConfig.ReadBufferSize;
                _serialPort.Encoding        = System.Text.Encoding.GetEncoding(28591);

                //Causes an error if it is being used
                openPort(_serialPort);
                _serialPort.DiscardInBuffer();
                _serialPort.DiscardOutBuffer();

                portOpened = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(System.Reflection.MethodInfo.GetCurrentMethod().Name + ". Error: " + ex.Message);
            }

            if (portOpened)
            {
                try
                {
                    //Causes error in Mono for non-standard baud rate
                    _serialPort.BaudRate = _serialPortConfig.BaudRate;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(System.Reflection.MethodInfo.GetCurrentMethod().Name + ". Error: " + ex.Message);
                    ForceSetBaudRate(_serialPort.PortName, _serialPortConfig.BaudRate);
                }
            }

            return(portOpened);
        }
예제 #2
0
 public void ConfigPort(SerialPortConfig _serialPortConfig = default(SerialPortConfig))
 {
     ConfigPort(m_serialPort, _serialPortConfig);
 }