用C# 模仿MVC模型 编写一个简单的串口调试助手,
private void InitializeCOMCombox() { //BaudRate baudRateCbx.Items.Add(4800); baudRateCbx.Items.Add(9600); baudRateCbx.Items.Add(19200); baudRateCbx.Items.Add(38400); baudRateCbx.Items.Add(57600); baudRateCbx.Items.Add(115200); baudRateCbx.Items.ToString(); //get 9600 print in text baudRateCbx.Text = baudRateCbx.Items[1].ToString(); //Data bits dataBitsCbx.Items.Add(7); dataBitsCbx.Items.Add(8); //get the 8bit item print it in the text dataBitsCbx.Text = dataBitsCbx.Items[1].ToString(); //Stop bits stopBitsCbx.Items.Add("One"); stopBitsCbx.Items.Add("OnePointFive"); stopBitsCbx.Items.Add("Two"); //get the One item print in the text stopBitsCbx.Text = stopBitsCbx.Items[0].ToString(); //Parity parityCbx.Items.Add("None"); parityCbx.Items.Add("Even"); parityCbx.Items.Add("Mark"); parityCbx.Items.Add("Odd"); parityCbx.Items.Add("Space"); //get the first item print in the text parityCbx.Text = parityCbx.Items[0].ToString(); //Handshaking handshakingcbx.Items.Add("None"); handshakingcbx.Items.Add("XOnXOff"); handshakingcbx.Items.Add("RequestToSend"); handshakingcbx.Items.Add("RequestToSendXOnXOff"); handshakingcbx.Text = handshakingcbx.Items[0].ToString(); //Com Ports string[] ArrayComPortsNames = SerialPort.GetPortNames(); if (ArrayComPortsNames.Length == 0) { statuslabel.Text = "No COM found !"; openCloseSpbtn.Enabled = false; } else { Array.Sort(ArrayComPortsNames); for (int i = 0; i < ArrayComPortsNames.Length; i ) { comListCbx.Items.Add(ArrayComPortsNames[i]); } comListCbx.Text = ArrayComPortsNames[0]; openCloseSpbtn.Enabled = true; } } /// <summary> /// update status bar /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void OpenComEvent(Object sender, SerialPortEventArgs e) { if (this.InvokeRequired) { Invoke(new Action<Object, SerialPortEventArgs>(OpenComEvent), sender, e); return; } if (e.isOpend) //Open successfully { statuslabel.Text = comListCbx.Text " Opend"; openCloseSpbtn.Text = "Close"; sendbtn.Enabled = true; autoSendcbx.Enabled = true; autoReplyCbx.Enabled = true; comListCbx.Enabled = false; baudRateCbx.Enabled = false; dataBitsCbx.Enabled = false; stopBitsCbx.Enabled = false; parityCbx.Enabled = false; handshakingcbx.Enabled = false; refreshbtn.Enabled = false; if (autoSendcbx.Checked) { autoSendtimer.Start(); sendtbx.ReadOnly = true; } } else //Open failed { statuslabel.Text = "Open failed !"; sendbtn.Enabled = false; autoSendcbx.Enabled = false; autoReplyCbx.Enabled = false; } }
评论