/// <summary>从串口列表选择串口,支持自动选择关键字</summary> /// <param name="keyWord">串口名称或者描述符的关键字</param> /// <returns></returns> public static SerialTransport Choose(String keyWord = null) { var ns = GetNames(); if (ns.Count == 0) { Console.WriteLine("没有可用串口!"); return(null); } var name = ""; var des = ""; Console.WriteLine("可用串口:"); Console.ForegroundColor = ConsoleColor.Green; foreach (var item in ns) { if (item.Value == "Serial0") { continue; } if (keyWord != null && (item.Key.EqualIgnoreCase(keyWord) || item.Value.Contains(keyWord))) { name = item.Key; des = item.Value; } //Console.WriteLine(item); Console.WriteLine("{0,5}({1})", item.Key, item.Value); } // 没有自动选择,则默认最后一个 if (name.IsNullOrEmpty()) { var item = ns.Last(); name = item.Key; des = item.Value; } while (true) { Console.ResetColor(); Console.Write("请输入串口名称(默认 "); Console.ForegroundColor = ConsoleColor.Red; Console.Write("{0}", name); Console.ResetColor(); Console.Write("):"); var str = Console.ReadLine(); if (str.IsNullOrEmpty()) { break; } // 只有输入有效串口名称才行 if (ns.ContainsKey(str)) { name = str; des = ns[str]; break; } } Console.WriteLine(); Console.Write("正在打开串口 "); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("{0}({1})", name, des); Console.ResetColor(); var sp = new SerialTransport { PortName = name }; return(sp); }
/// <summary>连接串口</summary> public void Connect() { var name = cbName.SelectedItem + ""; if (String.IsNullOrEmpty(name)) { "请选择串口".SpeechTip(); MessageBox.Show("请选择串口!", Text); cbName.Focus(); return; } var p = name.IndexOf("("); if (p > 0) name = name.Substring(0, p); SaveInfo(); var cfg = SerialPortConfig.Current; var pt = Port; if (pt != null) { // 如果上次没有关闭,则关闭 try { pt.Close(); } catch { pt = null; } } if (pt == null) pt = new SerialTransport(); pt.PortName = name; pt.BaudRate = cfg.BaudRate; pt.Parity = cfg.Parity; pt.DataBits = cfg.DataBits; pt.StopBits = cfg.StopBits; //pt.Open(); pt.Disconnected += Port_Disconnected; pt.Received += OnReceived; pt.ReceiveAsync(); //pt.EnsureCreate(); Port = pt; var sp = pt.Serial; // 这几个需要打开端口以后才能设置 try { if (sp.DtrEnable != miDTR.Checked) sp.DtrEnable = miDTR.Checked; if (sp.RtsEnable != miRTS.Checked) sp.RtsEnable = miRTS.Checked; if (sp.BreakState != miBreak.Checked) sp.BreakState = miBreak.Checked; } catch { } Enabled = false; }
/// <summary>从串口列表选择串口,支持自动选择关键字</summary> /// <param name="keyWord">串口名称或者描述符的关键字</param> /// <returns></returns> public static SerialTransport Choose(String keyWord = null) { var ns = GetNames(); if (ns.Count == 0) { Console.WriteLine("没有可用串口!"); return null; } var name = ""; var des = ""; Console.WriteLine("可用串口:"); Console.ForegroundColor = ConsoleColor.Green; foreach (var item in ns) { if (item.Value == "Serial0") continue; if (keyWord != null && (item.Key.EqualIgnoreCase(keyWord) || item.Value.Contains(keyWord))) { name = item.Key; des = item.Value; } //Console.WriteLine(item); Console.WriteLine("{0,5}({1})", item.Key, item.Value); } // 没有自动选择,则默认最后一个 if (name.IsNullOrEmpty()) { var item = ns.Last(); name = item.Key; des = item.Value; } while (true) { Console.ResetColor(); Console.Write("请输入串口名称(默认 "); Console.ForegroundColor = ConsoleColor.Red; Console.Write("{0}", name); Console.ResetColor(); Console.Write("):"); var str = Console.ReadLine(); if (str.IsNullOrEmpty()) break; // 只有输入有效串口名称才行 if (ns.ContainsKey(str)) { name = str; des = ns[str]; break; } } Console.WriteLine(); Console.Write("正在打开串口 "); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("{0}({1})", name, des); Console.ResetColor(); var sp = new SerialTransport(); sp.PortName = name; return sp; }