private void SerialForm_Load(object sender, EventArgs e) { #if DEBUG var f = new FileForm() { LogFile = Directory.GetCurrentDirectory() + "\\log\\test.ht2" }; f.ShowDialog(); this.Close(); return; #endif //SerialPort(String, Int32, Parity, Int32, StopBits) 使用指定的端口名、波特率、奇偶校验位、数据位和停止位初始化 SerialPort 类的新实例。 try { var ss = ConfigurationManager.AppSettings["SerialSetting"]; var temp = ss.Split('/'); _port = new SerialPort(temp[0], int.Parse(temp[1]), (Parity)Enum.Parse(typeof(Parity), temp[2]), int.Parse(temp[3]), (StopBits)Enum.Parse(typeof(StopBits), temp[4])); _port.Open(); _port.DataReceived += (s1, e1) => { var indata = _port.ReadLine(); _log.WriteLine(_log._direc + _logfile, indata); MyAction(indata); }; } catch (Exception exception) { MessageBox.Show(exception.Message); Application.Exit(); } }
/// <summary> /// 获取命令结合 用于创建btn,toolstrip /// </summary> /// <returns></returns> private List <Cmds> GetCmds() { var cmds = new List <Cmds>(); var cmd = new Cmds { Cmd = "开始接收", Click = (s, e) => { _this.Start(); } }; cmds.Add(cmd); var cmd2 = new Cmds { Cmd = "停止接收", Click = (s, e) => { _this.Stop(); } }; cmds.Add(cmd2); var cmd3 = new Cmds { Cmd = "重启应用", Click = (s, e) => { #if DEBUG MessageBox.Show(_valusetTable.Rows.Count.ToString()); #endif Application.Restart(); } }; cmds.Add(cmd3); var cmd4 = new Cmds { Cmd = "打开", Click = (s, e) => { var of = new OpenFileDialog { InitialDirectory = _log._direc, Filter = "日志文件(*.ht2)|*.ht2" }; if (of.ShowDialog() != DialogResult.OK) { return; } var f = new FileForm { LogFile = of.FileName }; f.ShowDialog(); } }; cmds.Add(cmd4); return(cmds); }