コード例 #1
0
        public override TestResult test(SerialPort _port)
        {
            char character = (char) mRandom.Next(255);
            String toSend = new String(character, 1);

            _port.send(toSend);

            DateTime timeout = DateTime.Now + TimeSpan.FromMilliseconds(100);
            bool timedout = false;

            String detail = "";
            while (!_port.dataWaiting() && !timedout)
            {
                if (DateTime.Now > timeout)
                {
                    timedout = true;
                }
            }

            String response = "";
            bool success = false;

            if (!timedout)
            {
                response = _port.recvAmount(1);
                success = (response != toSend);
                if (success)
                {
                    detail = "valid echo";
                }
                else
                {
                    detail = "invalid echo";
                }
            }
            else
            {
                detail = "timed out";
            }

            return new TestResult(toSend, response, success, detail);
        }
コード例 #2
0
        public override TestResult test(SerialPort _port)
        {
            bool success = false;

            char marker = '#';
            char sending = (char) mRandom.Next(254);
            String toSend = new String(marker, 1) + new String(sending, 1);
            String expected = new String(marker, 1) + new String((char)(sending + 1), 1);

            _port.send(toSend);

            DateTime timeout = DateTime.Now + TimeSpan.FromMilliseconds(100);
            bool timedout = false;
            bool lockedOn = false;

            String received = "";
            while (received.Length < 2 && !timedout)
            {
                while (!_port.dataWaiting() && !timedout)
                {
                    if (DateTime.Now > timeout)
                    {
                        timedout = true;
                    }
                }

                if (!timedout)
                {
                    String rx = _port.recvAmount(1);
                    if (!lockedOn && rx[0] == marker)
                    {
                        received = "#";
                        lockedOn = true;
                    }
                    else if (lockedOn)
                    {
                        received += rx;
                    }
                }
            }

            String detail = "";
            if (timedout)
            {
                received = "";
                detail = lockedOn ? "synchronised but timed out" : "failed to synchronise and timed out";
            }
            else
            {
                success = (received == expected);
                if (success)
                {
                    detail = "synchronised with correct response";
                }
                else
                {
                    detail = "synchronised but incorrect response";
                }
            }

            return new TestResult(toSend, received, success, detail);
        }