Example #1
0
        bool ResetControl()
        {
            EventWaitHandle ewh = new EventWaitHandle(false, EventResetMode.AutoReset);

            ArduinoSerial.BytesTransferedHandler h = (port, bytes_) =>
            {
                if (bytes_.Contains(ArduinoSerial.Reply.Hello))
                {
                    ewh.Set();
                }
            };
            try
            {
                Arduino.BytesReceived += h;
                for (int i = 0; i < 3; i++)
                {
                    Arduino.Write(ArduinoSerial.Command.Ready, ArduinoSerial.Command.Hello);
                }
                return(ewh.WaitOne(50));
            }
            finally
            {
                Arduino.BytesReceived -= h;
            }
        }
Example #2
0
        bool SendSync(Func <byte, bool> predicate, int timeout, params byte[] bytes)
        {
            EventWaitHandle ewh = new EventWaitHandle(false, EventResetMode.AutoReset);

            ArduinoSerial.BytesTransferedHandler h = (port, bytes_) =>
            {
                foreach (var b in bytes_)
                {
                    if (predicate(b))
                    {
                        ewh.Set();
                        break;
                    }
                }
            };
            try
            {
                Arduino.BytesReceived += h;
                Arduino.Write(bytes);
                if (!ewh.WaitOne(timeout))
                {
                    return(false);
                }
                return(true);
            }
            finally
            {
                Arduino.BytesReceived -= h;
            }
        }