private void btnConnectSerialPort_Click(object sender, EventArgs e) { switch (btnConnectSerialPort.Text) { case "連接": { XmlConfig.XmlConfig.Write(cbxSerialPort.SelectedItem.ToString(), "serialport", "port"); int lRet = optController.InitSerialPort(cbxSerialPort.SelectedItem.ToString()); if (0 != lRet) { MessageBox.Show("串口初始化失敗!", "Error"); return; } cbxSerialPort.Enabled = false; btnConnectSerialPort.Text = "斷開"; break; } case "斷開": { int lRet = optController.ReleaseSerialPort(); if (0 != lRet) { MessageBox.Show("釋放端口失敗!", "Error"); return; } btnConnectSerialPort.Text = "連接"; break; } default: { break; } } }
/// <summary> /// 连接光源 0:串口连接,1:IP地址连接,2:SN连接 /// </summary> public bool Connect(TimeSpan timeout) { int ret = 0; switch (this.Mode) { case 0: ret = opt.InitSerialPort(this.Config); break; case 1: ret = opt.CreateEtheConnectionByIP(this.Config); break; case 2: ret = opt.CreateEtheConnectionBySN(this.Config); break; default: ret = opt.CreateEtheConnectionByIP(this.Config); break; } return(ret == 0); }