Example #1
0
        public static bool CheckCRC(List <byte> dataFrame)
        {
            var datas       = dataFrame.GetRange(0, dataFrame.Count - 2);
            var crc         = CRC16.CalculateCRC(datas.ToArray());
            var receivedcrc = dataFrame.GetRange(dataFrame.Count - 2, 2);

            if (crc[0] == receivedcrc[0] && crc[1] == receivedcrc[1])
            {
                // CRC Normal
                return(true);
            }
            else
            {
                // CRC Error
                return(false);
            }
        }
Example #2
0
        private static byte[] MakeRetriveDataFrame(byte address, RetrieveCommand command)
        {
            var dataFrame = new List <byte>();

            dataFrame.Add(address);

            var commandCodes = BitConverter.GetBytes((int)command);

            dataFrame.Add(commandCodes[2]); // Function Code
            dataFrame.Add(commandCodes[1]); // Register Address (High)
            dataFrame.Add(commandCodes[0]); // Register Address (Low)

            dataFrame.Add(0x00);            // Register Count (High)
            dataFrame.Add(0x01);            // Register Count (Low)

            var crc = CRC16.CalculateCRC(dataFrame.ToArray());

            dataFrame.AddRange(crc);

            return(dataFrame.ToArray());
        }
Example #3
0
        private static byte[] MakeUpdateDataFrame(byte address, UpdateCommand command, ushort data)
        {
            var dataFrame = new List <byte>();

            dataFrame.Add(address);

            var commandCodes = BitConverter.GetBytes((int)command);

            dataFrame.Add(commandCodes[2]); // Function Code
            dataFrame.Add(commandCodes[1]); // Register Address (High)
            dataFrame.Add(commandCodes[0]); // Register Address (Low)

            var datas = BitConverter.GetBytes(data);

            dataFrame.Add(datas[1]); // data (High)
            dataFrame.Add(datas[0]); // data (Low)

            var crc = CRC16.CalculateCRC(dataFrame.ToArray());

            dataFrame.AddRange(crc);

            return(dataFrame.ToArray());
        }