/// <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()); }
private void OnDataReceived(GodSerialPort arg1, byte[] arg2) { var received = Encoding.ASCII.GetString(arg2, 0, arg2.Length).Trim(); var remote = Service.NinjectRegistry.Get <TeamsRemote>(); switch (received) { case "mic": Trace.TraceInformation("Toggle mute status..."); remote.ToggleMute(); break; case "hand": Trace.TraceInformation("Toggle raise hand..."); remote.ToggleRaiseHand(); break; case "cam": Trace.TraceInformation("Toggle camera..."); remote.ToggleCamera(); break; default: Trace.TraceInformation("Received: " + received); break; } }
private void btnTesKoneksi_Click(object sender, EventArgs e) { const int MAX_LENGTH = 20; var appName = "OpenRetail Server"; var version = string.Format("v{0}", MainProgram.currentVersion); var displayLine1 = string.Format("{0}{1}", StringHelper.CenterAlignment(appName.Length, MAX_LENGTH), appName); var displayLine2 = string.Format("{0}{1}", StringHelper.CenterAlignment(version.Length, MAX_LENGTH), version); System.Diagnostics.Debug.Print("displayLine1: {0}", displayLine1); System.Diagnostics.Debug.Print("displayLine2: {0}", displayLine2); if (!Utils.IsRunningUnderIDE()) { GodSerialPort serialPort = null; if (!GodSerialPortHelper.IsConnected(serialPort, _settingPort)) { MsgHelper.MsgWarning("Koneksi ke customer display, silahkan coba port yang lain."); return; } GodSerialPortHelper.SendStringToCustomerDisplay(displayLine1, displayLine2, serialPort); } }
/// <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()); }
/// <summary> /// Sets the cursor to line 1 column 1. /// </summary> private static void SetCursorToLine(this GodSerialPort serial, Line lineNumber) { var command = new byte[6] { 0x1B, 0x5B, (byte)lineNumber, 0x3B, 0x31, 0x48 }; serial.Write(command, 0, 6); }
/// <summary> /// 关闭串口 /// </summary> public void Close() { if (null != _port) { _port.Close(); _port = null; } }
private const int MAX_LENGTH = 20; // maksimal karakter customer display private void DisplayItemProduct(ItemJualProduk itemJual) { var produk = itemJual.Produk; var jumlah = itemJual.jumlah - itemJual.jumlah_retur; var hargaJual = itemJual.harga_setelah_diskon; if (produk != null) { if (!(hargaJual > 0)) { double diskon = itemJual.diskon; double diskonRupiah = 0; if (!(diskon > 0)) { if (_customer != null) { diskon = _customer.diskon; } if (!(diskon > 0)) { var diskonProduk = GetDiskonJualFix(produk, jumlah, produk.diskon); diskon = diskonProduk > 0 ? diskonProduk : produk.Golongan.diskon; } } hargaJual = GetHargaJualFix(produk, jumlah, produk.harga_jual); diskonRupiah = diskon / 100 * hargaJual; hargaJual -= diskonRupiah; } } var subTotal = StringHelper.RightAlignment(Convert.ToString(jumlah * hargaJual), MAX_LENGTH - (jumlah.ToString().Length + hargaJual.ToString().Length + 1)); var displayLine1 = StringHelper.FixedLength(produk.nama_produk, MAX_LENGTH); var displayLine2 = string.Format("{0}x{1}{2}", jumlah, hargaJual, subTotal); System.Diagnostics.Debug.Print("displayLine1: {0}", displayLine1); System.Diagnostics.Debug.Print("displayLine2: {0}", displayLine2); if (!Utils.IsRunningUnderIDE() && _settingCustomerDisplay.is_active_customer_display) { GodSerialPort serialPort = null; if (!GodSerialPortHelper.IsConnected(serialPort, _settingPort)) { return; } GodSerialPortHelper.SendStringToCustomerDisplay(displayLine1, displayLine2, serialPort); } }
public ScanController(SerialConfig config, Logging logger) { _logger = logger; Config = config; port = new GodSerialPort(c => { c.PortName = Config.PortName; c.BaudRate = Config.BuadRate; c.DataBits = Config.DataBits; c.StopBits = StopBits.One; c.Parity = Parity.None; }); port.OnData = OnDataRead; }
public ScanController(SerialConfig config) { Config = config; port = new GodSerialPort(c => { c.PortName = Config.PortName; c.BaudRate = Config.BuadRate; c.DataBits = Config.DataBits; c.StopBits = StopBits.One; c.Parity = Parity.None; }) { OnData = OnDataRead }; }
/// <summary> /// Clears the entire display and sets the cursor to line 1 column 1. /// </summary> private static void ClearScreen(this GodSerialPort serial) { var command = new byte[4] { 0x1B, 0x5B, 0x32, 0x4A }; try { serial.Write(command, 0, 4); //SetCursorToLine(serial, Line.One); } catch { } }
private void OnDataRead(GodSerialPort port, byte[] data) { try { string code = Encoding.ASCII.GetString(data); if (code.Length > 10) { OnScanCoded?.Invoke(code); } } catch (Exception ex) { Log.Error(ex, "扫描枪接收数据时异常。"); } }
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); } }); } }
private void OnDataRead(GodSerialPort port, byte[] data) { string code = Encoding.ASCII.GetString(data); if (code.Length > 10) { if (codeRegex.IsMatch(code)) { OnScanCoded?.Invoke(code, EnumCodeType.PartCode); } else { OnScanCoded?.Invoke(code, EnumCodeType.EngineCode); } _logger.Info($"接收到扫描枪数据:{code}"); } }
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(); } }
private void DisplayKembalian(string kembalian) { var displayLine1 = "Kembalian"; var displayLine2 = StringHelper.RightAlignment(kembalian, MAX_LENGTH); System.Diagnostics.Debug.Print("displayLine1: {0}", displayLine1); System.Diagnostics.Debug.Print("displayLine2: {0}", displayLine2); if (!Utils.IsRunningUnderIDE() && _settingCustomerDisplay.is_active_customer_display) { GodSerialPort serialPort = null; if (!GodSerialPortHelper.IsConnected(serialPort, _settingPort)) { return; } GodSerialPortHelper.SendStringToCustomerDisplay(displayLine1, displayLine2, serialPort); } }
private void DisplayKalimatPembuka() { var displayLine1 = string.Format("{0}{1}", StringHelper.CenterAlignment(_settingCustomerDisplay.opening_sentence_line1.Length, MAX_LENGTH), _settingCustomerDisplay.opening_sentence_line1); var displayLine2 = string.Format("{0}{1}", StringHelper.CenterAlignment(_settingCustomerDisplay.opening_sentence_line2.Length, MAX_LENGTH), _settingCustomerDisplay.opening_sentence_line2); System.Diagnostics.Debug.Print("displayLine1: {0}", displayLine1); System.Diagnostics.Debug.Print("displayLine2: {0}", displayLine2); if (!Utils.IsRunningUnderIDE() && _settingCustomerDisplay.is_active_customer_display) { GodSerialPort serialPort = null; if (!GodSerialPortHelper.IsConnected(serialPort, _settingPort)) { return; } GodSerialPortHelper.SendStringToCustomerDisplay(displayLine1, displayLine2, serialPort); } }
private void receiveWindow_Loaded(object sender, RoutedEventArgs e) { try { _serialPort = new GodSerialPort("COM8", 9600, Parity.None, 8, StopBits.One) { DataFormat = SerialPortDataFormat.Hex, RtsEnable = true, DtrEnable = true }; Open(); Task.Factory.StartNew(() => { while (true) { var data = _serialPort.ReadString(); this.Dispatcher.Invoke(() => { if (!string.IsNullOrEmpty(data)) { this.rtxReceiveData.AppendText(data + "\n"); } }); Thread.Sleep(3000); } }); } catch (Exception ex) { Console.WriteLine("串口数据接收错误:" + ex.ToString()); } }
public static void SendStringToCustomerDisplay(string displayLine1, string displayLine2, GodSerialPort serialPort) { serialPort.ClearScreen(); serialPort.SetCursorToLine(Line.One); serialPort.WriteAsciiString(displayLine1); serialPort.SetCursorToLine(Line.Two); serialPort.WriteAsciiString(displayLine2); serialPort.Close(); }
public static bool IsConnected(GodSerialPort serial, SettingPort settingPort) { serial = new GodSerialPort(settingPort.portNumber, settingPort.baudRate, settingPort.parity, settingPort.dataBits, settingPort.stopBits); return(serial.Open()); }