/// <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失败"); } }
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { try { //关闭串口 _pmjSerialPort?.Close(); // _plcSerialPort?.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }