Пример #1
0
        /// <summary>
        /// Encode command.
        /// </summary>
        /// <returns>Result package</returns>
        public unsafe byte[] Encode()
        {
            byte[] result = new byte[5];
            fixed(byte *p0 = result)
            {
                byte *p = p0;

                *       p++ = (byte)FunctionCode;
                ushort *up  = (ushort *)p;

                *up++ = Address;
                *up++ = Count;
                HexHelper.SwapEvenOdd(p, 0, 4);
            }

            return(result);
        }
Пример #2
0
        /// <summary>
        /// Encode this command
        /// </summary>
        /// <returns>Encode result</returns>
        public unsafe byte[] Encode()
        {
            byte[] result = new byte[5 + Content.Length];
            fixed(byte *p0 = result)
            {
                byte *p = p0;

                *       p++ = (byte)Function;
                ushort *up  = (ushort *)p;

                *up++ = StartingAddress;
                *up++ = Quantity;
                HexHelper.SwapEvenOdd(p, 1, 4);
                p = (byte *)up;
                *p++ = (byte)(Content.Length);
                Array.Copy(Content, 0, result, 5, Content.Length);
            }

            return(result);
        }
Пример #3
0
        /// <summary>
        /// Encode command.
        /// </summary>
        /// <returns>Result package</returns>
        public unsafe byte[] Encode()
        {
            byte[] result = new byte[10 + Content.Length];
            fixed(byte *p0 = result)
            {
                byte *p = p0;

                *       p++ = (byte)FunctionCode;
                ushort *up  = (ushort *)p;

                *up++ = ReadStartingAddress;
                *up++ = ReadCount;
                *up++ = WriteStartingAddress;
                *up++ = WriteCount;
                HexHelper.SwapEvenOdd(p0, 1, 8);
                p = (byte *)up;
                *p++ = (byte)(Content.Length);
                Array.Copy(Content, 0, result, 10, Content.Length);
            }

            return(result);
        }
Пример #4
0
        /// <summary>
        /// Encode this frame
        /// </summary>
        /// <returns>Encoded data</returns>
        public unsafe byte[] Encode()
        {
            const int headerLength = 6;
            var       dataLength   = Content?.Length ?? 0;

            byte[] result = new byte[headerLength + dataLength];
            fixed(byte *p0 = result)
            {
                ushort *up = (ushort *)p0;

                *up++ = Verify;
                *up++ = Type;
                *up++ = (ushort)(dataLength);
                HexHelper.SwapEvenOdd(p0, 0, headerLength);
                if (dataLength > 0)
                {
                    Array.Copy(Content, 0, result, headerLength, Content.Length);
                }
            }

            return(result);
        }
Пример #5
0
        /// <summary>
        /// Encode this frame
        /// </summary>
        /// <returns>Encoded data</returns>
        public unsafe byte[] Encode()
        {
            const int headerLength = 3;
            var       dataLength   = Content?.Length ?? 0;

            byte[] result = new byte[headerLength + dataLength];
            fixed(byte *p0 = result)
            {
                byte *p = p0;

                *p++ = Address;
                if (dataLength > 0)
                {
                    Array.Copy(Content, 0, result, 1, Content.Length);
                    CRC = s_crc.Compute(result, 1, dataLength);
                }

                p += dataLength;
                *((ushort *)p) = CRC;
                HexHelper.SwapEvenOdd(p, 2);
            }

            return(result);
        }