public async void Open() { _isOpened = _serialPort.Open(); if (_isOpened == false) { await Task.Delay(2000); _isOpened = _serialPort.Open(); } }
/// <summary> /// 打开串口 /// </summary> public bool Open() { if (null == _port) { _port = new GodSerialPort(this._portName, 115200, Parity.Odd, 8, StopBits.One, Handshake.None); _port.UseDataReceived(true, (sp, bytes) => { StringBuilder sb = new StringBuilder(); foreach (var b in bytes) { sb.Append($"{b:X2} "); } Console.WriteLine($"recv data:{sb.ToString()}"); //如果接受到数据,就提交给接口处理 _dataRecv = bytes; _dataRecvPort?.DealData(bytes); }); } if (_port.IsOpen) { return(true); } //打开串口,并进行监听 return(_port.Open()); }
/// <summary> /// 打开串口 /// </summary> public bool Open() { if (null == _port) { _port = new GodSerialPort(this._portName, this._rate, Parity.Even, 7, StopBits.One, Handshake.None); _port.UseDataReceived(true, (sp, bytes) => { StringBuilder sb = new StringBuilder(); foreach (var b in bytes) { sb.Append($"{b:X2} "); } Console.WriteLine($"recv data from plc:{sb.ToString()}"); //如果接受到数据,就提交给接口处理 if (_dataRecv == null) { _dataRecv = bytes; } else { _dataRecv = _dataRecv.Concat(bytes).ToArray(); Console.Write($"拼接后数据:"); CommandFactory.PrintBytes(_dataRecv); } _dataRecvPort?.DealData(bytes); }); } if (_port.IsOpen) { return(true); } //打开串口,并进行监听 return(_port.Open()); }
public bool Open() { try { IsOpen = port.Open(); _logger.Info($"扫描枪连接{(IsOpen ? "成功" : "失败")}."); return(IsOpen); } catch (Exception ex) { _logger.Error("扫描器连接异常", ex); IsOpen = false; return(false); } }
public bool Open() { try { IsOpen = port.Open(); Log.Information("扫描枪连接{IsOpen}.", IsOpen ? "成功" : "失败"); return(IsOpen); } catch (Exception ex) { Log.Error(ex, "扫描器连接异常"); IsOpen = false; return(false); } }
private void btnSetting_Click(object sender, RoutedEventArgs e) { _serialPort = new GodSerialPort(txtCOM.Text, Convert.ToInt32(txtRate.Text), Parity.None, 8, StopBits.One) { DataFormat = SerialPortDataFormat.Hex, RtsEnable = true, DtrEnable = true }; _serialPort.Open(); if (!_serialPort.IsOpen) { Console.WriteLine("串口打开失败"); } else { int interval = Convert.ToInt32(txtReceiveInterval.Text); Task.Factory.StartNew(() => { while (true) { var data = _serialPort?.ReadString(); this.Dispatcher.Invoke(() => { if (!string.IsNullOrEmpty(data)) { if (data.StartsWith("00 C7") || data.StartsWith("00 C8")) { Console.WriteLine("收到注册包或心跳包" + data); } else { content.Text = data + "\r\n"; } } }); Thread.Sleep(5000); } }); } }
static void Main(string[] args) { Console.Write("input serialport name:"); string read = Console.ReadLine(); if (string.IsNullOrWhiteSpace(read)) { Exit(); } GodSerialPort gsp = new GodSerialPort(x => { x.PortName = read; }); gsp.UseDataReceived(true, (sp, bytes) => { if (bytes != null && bytes.Length > 0) { string buffer = string.Join(" ", bytes); Console.WriteLine("receive data:" + buffer); } }); bool flag = gsp.Open(); if (!flag) { Exit(); } Console.WriteLine("serialport opend"); Console.WriteLine("press any thing as data to send,press key 'q' to quit."); string data = null; while (data == null || data.ToLower() != "q") { if (!string.IsNullOrEmpty(data)) { Console.WriteLine("send data:" + data); gsp.WriteAsciiString(data); } data = Console.ReadLine(); } }
private void Initialize() { var portName = settings.COMPort; try { portName = GodSerialPort.GetPortNames().Single <string>(); } catch { } myPort = new GodSerialPort(portName, baudRate: 9600, parity: 0); myPort.UseDataReceived(true, OnDataReceived); myPort.OnPinChange(action: (port, e) => { Trace.TraceEvent(TraceEventType.Error, 3, "pinchanged:" + e.ToString()); }); myPort.OnError(action: (port, e) => { Trace.TraceEvent(TraceEventType.Error, 1, "error:" + e.ToString()); }); myPort.Open(); }
// ReSharper disable once UnusedParameter.Local static void Main(string[] args) { Console.Write("input serialport number(only 0-9):"); string read = Console.ReadLine(); bool flag = uint.TryParse(read, out uint num); if (!flag) { Exit(); } GodSerialPort gsp = new GodSerialPort("COM" + num, 9600); gsp.UseDataReceived((bytes) => { if (bytes != null && bytes.Length > 0) { string buffer = string.Join(" ", bytes); Console.WriteLine("receive data:" + buffer); } }); flag = gsp.Open(); if (!flag) { Exit(); } Console.WriteLine("serialport opend"); Console.WriteLine("press any thing as data to send,press key 'q' to quit."); string data = null; while (data == null || data.ToLower() != "q") { if (!string.IsNullOrEmpty(data)) { Console.WriteLine("send data:" + data); gsp.WriteAsciiString(data); } data = Console.ReadLine(); } }
public static bool IsConnected(GodSerialPort serial, SettingPort settingPort) { serial = new GodSerialPort(settingPort.portNumber, settingPort.baudRate, settingPort.parity, settingPort.dataBits, settingPort.stopBits); return(serial.Open()); }