Beispiel #1
0
        public SbepMessage ReceiveSbep()
        {
            var         i       = 0;
            SbepMessage message = null;
            var         sw      = Stopwatch.StartNew();

            while (sw.ElapsedMilliseconds < 2000 && i < _receiveBuffer.Length)
            {
                var avail = _port.BytesToRead;
                if (avail == 0)
                {
                    Thread.Sleep(20);
                    continue;                     //Don't restart stopwatch.
                }
                _port.Read(_receiveBuffer, i, avail);
                i      += avail;
                message = new SbepMessage(_receiveBuffer, i);
                if (!message.Incomplete)
                {
                    break;
                }
                sw.Restart();                 //Give some more time for bytes
            }
            return(message);
        }
Beispiel #2
0
        public void SendSbep(SbepMessage message)
        {
            int attempts = 0;

            while (true)
            {
                _port.DiscardInBuffer();
                _port.DiscardOutBuffer();
                var packetSize = message.Bytes.Length;

                _port.Write(message.Bytes, 0, packetSize);

                if (!message.ExpectAck)
                {
                    return;
                }

                //Wait for response
                var sw  = Stopwatch.StartNew();
                int ack = 0;
                while (sw.ElapsedMilliseconds < 1000)
                {
                    if (_port.BytesToRead < 1)
                    {
                        Thread.Sleep(10);
                        continue;
                    }
                    ack = _port.ReadByte(); break;
                }

                if (ack == 0x50)
                {
                    return;
                }

                if (attempts == 4)
                {
                    throw new Exception("The radio failed to acknowledge command. Try power cycling the radio and running the operation again.");
                }
                Thread.Sleep(500);
                attempts++;
            }
        }