예제 #1
0
        /// <summary>
        /// 打开PLC
        /// </summary>
        private void OpenPlc()
        {
            if (string.IsNullOrEmpty(comboPlcList.Text))
            {
                throw new Exception("PLC端口不能为空");
            }
            if (null == _plcSerialPort)
            {
                _plcSerialPort = new PLCSerialPort(comboPlcList.Text, null);
            }
            else
            {
                var portName = _plcSerialPort.GetPortName();
                if (!portName.Equals(comboPlcList.Text))
                {
                    //如果端口不通的话,就先关闭原来的port
                    _plcSerialPort.Close();
                    _plcSerialPort = new PLCSerialPort(comboPlcList.Text, null);
                }
            }

            var flag = _plcSerialPort.Open();

            if (!flag)
            {
                throw new Exception("打开PLC失败");
            }
        }