/// <summary> /// Wait until any response is received from the arduino /// Use the function e.g. after a reset to receive the boot message /// No command must be sent /// </summary> /// <param name="maxMilliseconds"></param> /// <returns></returns> public async Task<string> WaitUntilResonseAsync(int maxMilliseconds = int.MaxValue) { string message = null; var checkresponse = new ArduinoSerialCommunication.CommandEventHandler((obj, e) => { message = e.Info; }); try { ReplyReceived += checkresponse; var sw = Stopwatch.StartNew(); while (_continue && message == null && sw.ElapsedMilliseconds < maxMilliseconds) { if (_autoEvent.WaitOne(10) == false) await Task.Delay(1); } } finally { ReplyReceived -= checkresponse; } return message; }
/// <summary> /// Send a command to the arduino and wait until a (OK) reply /// Queue must be empty /// </summary> /// <param name="line">command line</param> /// <returns>ok result from arduino or empty(if error)</returns> public async Task<string> SendCommandAndReadOKReplyAsync(string line, int waitForMilliseconds=int.MaxValue) { string message = null; if (await WaitUntilNoPendingCommandsAsync(waitForMilliseconds)) { var checkresponse = new ArduinoSerialCommunication.CommandEventHandler((obj, e) => { message = e.Info; }); ReplyOK += checkresponse; QueueCommand(line); await WaitUntilNoPendingCommandsAsync(waitForMilliseconds); ReplyOK -= checkresponse; } return message; }