Exemplo n.º 1
0
        /// <summary>
        /// Добавляет в конец указанного буфера его CRC и
        /// возвращает новый массив
        /// </summary>
        public static byte[] AddCRC(byte[] buffer)
        {
            byte[] res = new byte[buffer.Length + 2];
            buffer.CopyTo(res, 0);
            int crc = RelkonProtocol.GetCRC(buffer);

            res[res.Length - 1] = AppliedMath.Hi(crc);
            res[res.Length - 2] = AppliedMath.Low(crc);
            return(res);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Проверяет, является ли указанный буфер сообщением текущего протокола
        /// </summary>
        /// <param name="ExpectedResponseSize">Ожидаемый размер сообщения протокола</param>
        protected bool ResponseRecieved(byte[] buffer, int ExpectedResponseSize)
        {
            bool res = false;

            switch (_relkonProtocolType)
            {
            case ProtocolType.RC51BIN:
                res = (RelkonProtocol.GetCRC(buffer) == 0 || (ExpectedResponseSize != -1 && buffer.Length >= ExpectedResponseSize * 100));
                break;

            case ProtocolType.RC51ASCII:
                res = (buffer[buffer.Length - 1] == 0x0D || (ExpectedResponseSize != -1 && buffer.Length >= ExpectedResponseSize * 100));
                break;
            }
            return(res);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Преобразует указанный массив из формату текущего протокола
        /// </summary>
        public static byte[] ConvertFromCurrentProtocolType(byte[] buffer, ProtocolType protocol, ErrorType errorType)
        {
            byte[] res = buffer;
            switch (protocol)
            {
            case ProtocolType.RC51ASCII:
            case ProtocolType.RC51BIN:
                // Проверяем буфер на наличие ошибок
                if ((protocol == ProtocolType.RC51BIN && buffer.Length == 4 && buffer[1] == 0xFF) || buffer.Length < 3 ||
                    (protocol == ProtocolType.RC51ASCII && (buffer[buffer.Length - 1] != 0x0d || buffer[0] != '!')))
                {
                    errorType = ErrorType.DeviceReturnError;
                    res       = null;
                }
                if (res != null)
                {
                    if (protocol == ProtocolType.RC51ASCII)
                    {
                        res = RelkonProtocol.ConvertFromRC51ASCII(buffer);
                    }
                    // Проверяем crc (если crc верна, то crc от буффера с crc = 0)
                    if (RelkonProtocol.GetCRC(res) != 0)
                    {
                        errorType = ErrorType.InvalidCRC;
                        res       = null;
                    }
                    else
                    {
                        res = Utils.GetSubArray <byte>(res, 0, res.Length - 2);
                    }
                }
                break;

            case ProtocolType.ATCommand:
                res = Utils.GetSubArray <byte>(res, 0, res.Length - 2);
                break;
            }
            return(res);
        }