/// <summary> /// 判断是否已经打开 /// </summary> /// <param name="printerName"></param> /// <returns></returns> private bool OpenPmj(out string printerName) { if (string.IsNullOrEmpty(comboBoxPmj.Text)) { throw new Exception("喷码机端口不能为空"); } //首先关闭原来的串口 if (null != _pmjSerialPort) { var portName = _pmjSerialPort.GetPortName(); //比较端口的名称,如果端口名称已经改变,就需要重新建立连接 if (!portName.Equals(comboBoxPmj.Text)) { _pmjSerialPort.Close(); _pmjSerialPort = new PmjSerialPort(comboBoxPmj.Text, new PmjDataRecv()); } } //如果不存在的话,也需要重新建立 if (null == _pmjSerialPort) { _pmjSerialPort = new PmjSerialPort(comboBoxPmj.Text, new PmjDataRecv()); } return(_pmjSerialPort.HasPrinter(out printerName)); }
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { try { //关闭串口 _pmjSerialPort?.Close(); // _plcSerialPort?.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }