void _tiSearchDevicePort_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { //0231 (地址 3030) 3030 (校验 3339) 03 //新检测串口设备 //0230 (地址 3030) 3031 (数据) (校验 3339) 03 byte[] by = PortAgreement.GetSearchHost(1); for (int i = 0; i < 10; i++) { foreach (string item in _m_ComPort.PortNames) { _m_Port.PortName = item; try { _m_Port.Open(); _m_Port.Write(by); Thread.Sleep(500); if (_m_Port.IsOpen) { _tiSearchDevicePort.Stop(); _tiSearchDevicePort.Dispose(); _tiSearchDevicePort = null; return; } _m_Port.Close(); } catch { } } } }
private void btn_OpenPort_Click(object sender, EventArgs e) { try { _mPort.PortName = cb_PortName.Text; _mPort.Open(); _mPort.IsOpen = true; StartEffect(); } catch (Exception ex) { MessageBox.Show(ex.Message, @"提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } }