private void btn_connect_Click(object sender, EventArgs e) { try { if (serialPort1.IsOpen) { serialPort1.Close(); label_state_serial.Text = "串口状态:已断开"; btn_connect.Text = "连接串口"; comboBox_serialNumber.Enabled = true; comboBox_baud.Enabled = true; } else { serialPort1.PortName = comboBox_serialNumber.SelectedItem.ToString(); serialPort1.Open(); label_state_serial.Text = "串口状态:已连接"; btn_connect.Text = "断开串口"; gsm = new GSM(serialPort1, interfaceUpdataHandle); gsmSerialProcess = gsm.GsmSerialHandle; comboBox_serialNumber.Enabled = false; comboBox_baud.Enabled = false; } }catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
public GSM(SerialPort com, Form1.HandleInterfaceUpdataDelegate ui) { if (com == null || !com.IsOpen) { throw new Exception("GSM初始化出错:串口实例不可用"); } else { serialPort = com; //创建委托对象 gsmSerialHandle = new HandleSerialRecvDelegate(serialRecv); //得到从主线程获得的更新UI的委托对象 updateMainUI = ui; timer_message_out = new System.Timers.Timer(10000); timer_message_out.AutoReset = false; timer_message_out.Elapsed += timer_message_out_tick; } }