예제 #1
0
        /// <summary>
        /// Port Refresh.
        /// </summary>
        public void RefreshPort()
        {
            // Validate all settings
            if (!FullyDefined)
            {
                return;
            }

            lock (_lockPort)
            {
                if (_port != null && _port.IsOpen)
                {
                    //Cannot change any configuration during port open.
                    ClosePort();
                }

                _port = new SerialPort(CommPort.ToString());

                try
                {
                    //_port.PortName = CommPort.ToString();
                    //_port.DtrEnable = true;
                    _port.BaudRate  = BaudRateInt;
                    _port.Handshake = HandShake;
                    _port.DataBits  = (int)DataLength;
                    _port.StopBits  = StopBits;
                    _port.Parity    = Parity;
                    _port.ReceivedBytesThreshold = 1;
                    _port.WriteBufferSize        = 100;
                    _port.ReadBufferSize         = 100;
                    _port.NewLine      = _settings.RawNewLine;
                    _port.ReadTimeout  = (int)(double)ReadWriteTimeOut;
                    _port.WriteTimeout = (int)(double)ReadWriteTimeOut;
                    if (!OwnDataHandler)
                    {
                        _port.DataReceived += new SerialDataReceivedEventHandler(OnSerialDataReceived);
                    }
                    _port.Open();
                    U.LogInfo("{0} finished opening port {1} port status = {2}", this.Nickname, CommPort, PortStatus);
                }
                catch (Exception ex)
                {
                    ClosePort();
                    throw new ForceSimulateException(ex, "Could not open CommPort {0}", CommPort);
                }
            }
        }