private void btnOpen_Click(object sender, EventArgs e) { //Open SerialPort if (btnOpen.Text == "Open") { string portName = cmbSerialList.Items.Count > 0 ? cmbSerialList.SelectedItem.ToString() : ""; //MessageBox.Show(portName); if (portName != null && portName != "") { Serial = new SerialOperUnit(); int result = Serial.init(portName, 115200, 8, "None", "1"); if (result > 0) { MessageBox.Show("Open SerialPort(" + portName + ") Error!"); } else { btnOpen.Text = "Close"; if (handler != null) { Serial.setSerialDataReceivedEventHandler(handler); } else { Serial.setSerialDataReceivedEventHandler(DataReceivedHandler); } } } else { MessageBox.Show("Please select any portName int the first!"); } } //Close SerialPort else if (btnOpen.Text == "Close") { if (Serial != null) { Serial.close(); Serial = null; } btnOpen.Text = "Open"; } }
private void btnOpen_Click(object sender, EventArgs e) { if (btnOpen.Text == "打开") { string portName = cmbSerialList.Items.Count > 0 ? cmbSerialList.SelectedItem.ToString() : ""; //MessageBox.Show(portName); if (portName != null && portName != "") { Serial = new SerialOperUnit(); int result = Serial.init(portName, 115200, 8, "None", "1"); if (result > 0) { MessageBox.Show("Open SerialPort(" + portName + ") Error!"); } else { btnOpen.Text = "关闭"; //Serial.setSerialDataReceivedEventHandler(DataReceivedHandler); Serial.delegateSerialRead += new SerialOperUnit.DelegateSerialRead(dataRxHandler); Serial.read(true); /* * if (handler != null) * { * Serial.setSerialDataReceivedEventHandler(handler); * } * else * { * Serial.setSerialDataReceivedEventHandler(DataReceivedHandler); * }*/ } } } else if (btnOpen.Text == "关闭") { if (Serial != null) { Serial.close(); Serial = null; } btnOpen.Text = "打开"; } }