private void cmbBarType_SelectedIndexChanged(object sender, EventArgs e) { EBarType barType = (EBarType)Enum.Parse(typeof(EBarType), cmbBarType.Text); if (comMon == null) { comMon = new CBarCOM(barType, 0, cmbBarType.Text); if (comMon.comMode == EComMode.SerialPort) { cmbCOM.Enabled = true; txtSerIP.Enabled = false; labSetting.Text = CLanguage.Lan("串口波特率:"); if (cmbBarType.Text == "SR700") { txtBand.Text = "115200,E,8,1"; } else { txtBand.Text = "115200,N,8,1"; } } else { cmbCOM.Enabled = false; txtSerIP.Enabled = true; labSetting.Text = CLanguage.Lan("TCP端口:"); if (cmbBarType.Text == "SR710_TCP") { txtSerIP.Text = "192.168.1.125"; txtBand.Text = "10001"; } else { txtBand.Text = "10000"; } } comMon = null; } }
private void btnOpen_Click(object sender, EventArgs e) { try { btnOpen.Enabled = false; if (cmbCOM.Text == "" && txtSerIP.Text == string.Empty) { labStatus.Text = CLanguage.Lan("请输入串口编号"); labStatus.ForeColor = Color.Red; return; } string er = string.Empty; if (comMon == null) { if (!Enum.IsDefined(typeof(EBarType), cmbBarType.Text)) { labStatus.Text = CLanguage.Lan("找不到类型") + "【" + cmbBarType.Text + "】"; labStatus.ForeColor = Color.Red; return; } EBarType barType = (EBarType)Enum.Parse(typeof(EBarType), cmbBarType.Text); comMon = new CBarCOM(barType, 0, cmbBarType.Text); string comName = string.Empty; string comSetting = string.Empty; if (comMon.comMode == EComMode.SerialPort) { comName = cmbCOM.Text; comSetting = txtBand.Text; } else { comName = txtSerIP.Text; comSetting = txtBand.Text; } if (!comMon.Open(comName, out er, comSetting)) { labStatus.Text = er; labStatus.ForeColor = Color.Red; comMon = null; return; } btnOpen.Text = CLanguage.Lan("关闭"); labStatus.Text = CLanguage.Lan("成功打开串口"); labStatus.ForeColor = Color.Blue; cmbCOM.Enabled = false; cmbBarType.Enabled = false; txtSerIP.Enabled = false; txtBand.Enabled = false; } else { comMon.Close(); comMon = null; btnOpen.Text = CLanguage.Lan("打开"); labStatus.Text = CLanguage.Lan("关闭串口"); labStatus.ForeColor = Color.Blue; cmbCOM.Enabled = true; cmbBarType.Enabled = true; txtSerIP.Enabled = true; txtBand.Enabled = true; } } catch (Exception) { } finally { btnOpen.Enabled = true; } }