コード例 #1
0
ファイル: CRC16.cs プロジェクト: hgim88/TM2_Driver_Console
        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);
            }
        }
コード例 #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());
        }
コード例 #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());
        }
コード例 #4
0
        public override bool ExtractData(byte[] buf, out byte[] receivedData)
        {
            // 데이터 검사에 필요한 최소 사이즈 검사
            if (buf.Length < 3)
            {
                receivedData = null;
                return(false);
            }

            var functionCode = buf[1];

            // 데이터 에러 검사 및 데이터 사이즈 계산
            int dataFrameLength;

            if ((functionCode & 0x80) == 0)
            {
                // 에러가 없을 때
                // 데이터 프레임 사이즈 계산
                if (functionCode == 0x05 || functionCode == 0x06)
                {
                    // Write function 의 경우, 응답 데이터 프레임에 data length가 없이 총 8바이트 이다.
                    dataFrameLength = 8;
                }
                else
                {
                    // Read 명령의 경우, 응답 데이터 프레임에 data length가 포함되어 있다.
                    // data size 외의 데이터는 총 5바이트가 있으므로 더해서 사용한다.
                    dataFrameLength = buf[2] + 5;
                }
            }
            else
            {
                // 에러가 있을 때
                // 데이터 프레임 사이즈 계산
                // Error의 경우 데이터는 총 5바이트 이다.
                dataFrameLength = 5;
            }

            // 데이터 프레임 사이즈 검사
            if (buf.Length < dataFrameLength)
            {
                receivedData = null;
                return(false);
            }

            // 데이터 프레임 생성 for checking CRC16
            var dataFrameBuffer = new List <byte>();

            for (int i = 0; i < dataFrameLength; i++)
            {
                dataFrameBuffer.Add(buf[i]);
            }

            // CRC 검사
            if (!CRC16.CheckCRC(dataFrameBuffer))
            {
                // crc 에러
                // 재송수신 필요
                receivedData = null;
                return(false);
            }

            receivedData = dataFrameBuffer.ToArray();
            return(true);
        }