public Form1() { InitializeComponent(); //串口列表 comboBoxPort.Items.AddRange(SerialPort.GetPortNames()); //波特率列表 comboBoxBaudrate.BeginUpdate(); foreach (int br in allBaudrates) { comboBoxBaudrate.Items.Add(br.ToString()); } comboBoxBaudrate.EndUpdate(); //数据位列表 comboBoxDataBits.BeginUpdate(); foreach (int db in allDataBits) { comboBoxDataBits.Items.Add(db.ToString()); } comboBoxDataBits.EndUpdate(); //停止位列表 comboBoxStopBits.BeginUpdate(); foreach (float sb in allStopBits) { comboBoxStopBits.Items.Add(sb.ToString()); } comboBoxStopBits.EndUpdate(); //TODO: 读取保存的配置 try { using (FileStream stream = File.OpenRead(strCfgFileName)) { BinaryFormatter deserializer = new BinaryFormatter(); //二进制序列化器 config = deserializer.Deserialize(stream) as PortCfg; //反序列化 int i = 0; foreach (string name in SerialPort.GetPortNames()) { if (name == config.portName) { break; } i++; } comboBoxPort.SelectedIndex = Clamp <int>(i, 0, comboBoxPort.Items.Count - 1); comboBoxBaudrate.SelectedIndex = Clamp <int>(config.baudrateIdx, 0, comboBoxBaudrate.Items.Count - 1); comboBoxDataBits.SelectedIndex = Clamp <int>(config.dataBitsIdx, 0, comboBoxDataBits.Items.Count - 1); comboBoxStopBits.SelectedIndex = Clamp <int>(config.stopBitsIdx, 0, comboBoxStopBits.Items.Count - 1); stream.Close(); } } catch { //配置文件不存在,使用默认设置 if (comboBoxPort.Items.Count > 0) { comboBoxPort.SelectedIndex = 0; } comboBoxBaudrate.SelectedIndex = 2; comboBoxDataBits.SelectedIndex = 3; comboBoxStopBits.SelectedIndex = 0; } Type type = textBoxReceive.GetType(); MethodInfo setStyle = type.GetMethod("SetStyle", BindingFlags.Instance | BindingFlags.NonPublic); MethodInfo updateStyle = type.GetMethod("UpdateStyles", BindingFlags.Instance | BindingFlags.NonPublic); try { object[] param = new object[2]; param[0] = ControlStyles.OptimizedDoubleBuffer; param[1] = true; setStyle.Invoke(textBoxReceive, param); updateStyle.Invoke(textBoxReceive, null); } catch (Exception e) { Console.WriteLine(e.ToString()); } }
//尝试打开端口 bool TryOpenPort() { serialPort = new SerialPort(); //设置端口 serialPort.PortName = comboBoxPort.Text; //设置波特率 serialPort.BaudRate = allBaudrates[comboBoxBaudrate.SelectedIndex]; //设置校验 serialPort.Parity = Parity.None; //设置数据位数 serialPort.DataBits = allDataBits[comboBoxDataBits.SelectedIndex]; //设置停止位 if (comboBoxStopBits.Text == "1") { serialPort.StopBits = StopBits.One; } else if (comboBoxStopBits.Text == "1.5") { serialPort.StopBits = StopBits.OnePointFive; } else if (comboBoxStopBits.Text == "2") { serialPort.StopBits = StopBits.Two; } //接收数据回调 serialPort.DataReceived += this.OnReceivedData; //回调阈值设置为1 serialPort.ReceivedBytesThreshold = 1; try { serialPort.Open(); } catch { serialPort = null; return(false); } //TODO: 保存配置 if (config == null) { config = new PortCfg(); } config.portName = serialPort.PortName; config.baudrateIdx = comboBoxBaudrate.SelectedIndex; config.dataBitsIdx = comboBoxDataBits.SelectedIndex; config.stopBitsIdx = comboBoxStopBits.SelectedIndex; using (FileStream stream = File.Create(strCfgFileName)) { BinaryFormatter serializer = new BinaryFormatter(); //二进制格式序列化器 serializer.Serialize(stream, config); //序列化对象到文件中 stream.Close(); } return(true); }