Exemplo n.º 1
0
        public static byte[] Serialize(ISerialize message)
        {
            byte[] frame = new byte[3];
            BitUtil.WriteUnsigned(ref frame, Constants.RTCM_PREAMBLE, 0, 8);
            BitUtil.WriteUnsigned(ref frame, (uint)message.ByteLength, 14, 10);

            List <byte> completeMessage = new List <byte>(frame);

            byte[] messageBytes = new byte[message.ByteLength];
            message.GetBytes(ref messageBytes);
            completeMessage.AddRange(messageBytes);

            byte[] crcBytes = BitConverter.GetBytes(CRC24Q.crc24q(completeMessage.ToArray(), completeMessage.Count, 0));
            completeMessage.Add(crcBytes[0]);
            completeMessage.Add(crcBytes[1]);
            completeMessage.Add(crcBytes[2]);

            return(completeMessage.ToArray());
        }
Exemplo n.º 2
0
        public ParsedMessage?Parse(byte[] data)
        {
            lock (_syncObject)
            {
                _buffer.AddRange(data);
                int preambleIndex = 0;
                while (preambleIndex < _buffer.Count && _buffer[preambleIndex] != Constants.RTCM_PREAMBLE)
                {
                    preambleIndex++;
                }

                if (preambleIndex >= _buffer.Count)
                {
                    _buffer.Clear();
                    return(null);
                }
                _buffer.RemoveRange(0, preambleIndex);
                if (_buffer.Count < 3)
                {
                    return(null);
                }
                byte[] buffer        = _buffer.ToArray();
                int    messageLength = (int)BitUtil.GetUnsigned(buffer, 14, 10);

                if (messageLength + 6 > buffer.Length)
                {
                    return(null);
                }

                _buffer.RemoveRange(0, messageLength + 6);
                uint crcValue = (uint)BitUtil.GetUnsigned(buffer, (messageLength + 3) * 8, 24);
                uint crcCalc  = CRC24Q.crc24q(buffer, messageLength + 3, 0);

                if (crcValue != crcCalc)
                {
                    throw new Exception("CRC is not valid");
                }

                byte[] messageBytes = new byte[messageLength];
                Array.Copy(buffer, 3, messageBytes, 0, messageLength);

                int messageNumber = (int)BitUtil.GetUnsigned(messageBytes, 0, 12);

                Enums.Messages messageEnum = Enums.Messages.Unknown;
                if (Enum.IsDefined(typeof(Enums.Messages), messageNumber))
                {
                    messageEnum = (Enums.Messages)(int) messageNumber;
                }

                if (messageEnum == Enums.Messages.Unknown)
                {
                    throw new Exception("MessageNumber: " + messageNumber + " cant be parsed, no valid C# type");
                }
                if (MESSAGE_DICTIONARY.ContainsKey(messageEnum))
                {
                    object message = Activator.CreateInstance(MESSAGE_DICTIONARY[messageEnum], messageBytes);
                    return(new ParsedMessage(messageEnum, message));
                }

                return(null);
            }
        }