protected override async Task ConnectSlave() { if (config.SlaveConnection.Substring(0, 3) == "COM" || config.SlaveConnection.Substring(0, 8) == "/dev/tty") { try { Console.WriteLine($"Opening...{config.SlaveConnection}"); m_serialPort = SerialDeviceFactory.CreateSerialDevice(config.SlaveConnection, (int)config.BaudRate, config.Parity, (int)config.DataBits, config.StopBits); m_serialPort.Open(); //m_serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived); await Task.Delay(2000); //Wait target to be ready to write the modbus package } catch (Exception e) { Console.WriteLine("Connect Slave failed"); Console.WriteLine(e.Message); m_serialPort = null; } } }
protected override async Task ConnectSlaveAsync() { try { if (this.config.SlaveConnection.Substring(0, 3) == "COM" || this.config.SlaveConnection.Substring(0, 8) == "/dev/tty") { Console.WriteLine($"Opening...{this.config.SlaveConnection}"); this.serialPort = SerialDeviceFactory.CreateSerialDevice(this.config.SlaveConnection, (int)this.config.BaudRate.Value, this.config.Parity.Value, this.config.DataBits.Value, this.config.StopBits.Value); this.serialPort.Open(); //serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived); await Task.Delay(2000).ConfigureAwait(false); //Wait target to be ready to write the modbus package } } catch (Exception e) { Console.WriteLine("Connect Slave failed"); Console.WriteLine(e.Message); this.serialPort = null; } }