2023-11-21 20:28:23 +08:00
|
|
|
#include "crc.h"
|
|
|
|
#include <QDebug>
|
|
|
|
#include "QString"
|
|
|
|
|
|
|
|
crc::crc()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-11-27 14:31:00 +08:00
|
|
|
uint8_t crc::crc8(uint8_t *Ptr, uint8_t num)
|
2023-11-21 20:28:23 +08:00
|
|
|
{
|
|
|
|
|
2023-11-27 14:31:00 +08:00
|
|
|
uint8_t crc = 0;
|
|
|
|
uint16_t j, i;
|
2023-11-21 20:28:23 +08:00
|
|
|
|
2023-11-27 14:31:00 +08:00
|
|
|
for (j = 0; j < num; j++)
|
|
|
|
{
|
|
|
|
crc ^= *(Ptr + j);
|
|
|
|
for (i = 0; i < 8; i++)
|
2023-11-21 20:28:23 +08:00
|
|
|
{
|
2023-11-27 14:31:00 +08:00
|
|
|
if ((crc & 0x01) != 0)
|
|
|
|
{
|
|
|
|
crc >>= 1;
|
|
|
|
crc ^= 0x8c;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
crc >>= 1;
|
|
|
|
}
|
2023-11-21 20:28:23 +08:00
|
|
|
}
|
2023-11-27 14:31:00 +08:00
|
|
|
}
|
|
|
|
return crc;
|
2023-11-21 20:28:23 +08:00
|
|
|
}
|
|
|
|
|
2023-11-27 14:31:00 +08:00
|
|
|
void crc::crc16(uint8_t *data, int offset, int len, uint8_t *lb, uint8_t *hb)
|
2023-11-21 20:28:23 +08:00
|
|
|
{
|
2023-11-27 14:31:00 +08:00
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
uint16_t crc = 0xFFFF;
|
|
|
|
int i = offset;
|
|
|
|
for (; i < len; i++)
|
2023-11-21 20:28:23 +08:00
|
|
|
{
|
2023-11-27 14:31:00 +08:00
|
|
|
crc = (uint16_t)(crc ^ (data[i]));
|
|
|
|
for (int j = 0; j < 8; j++)
|
|
|
|
{
|
|
|
|
crc = (crc & 1) != 0 ? (uint16_t)((crc >> 1) ^ 0xA001) : (uint16_t)(crc >> 1);
|
|
|
|
}
|
2023-11-21 20:28:23 +08:00
|
|
|
}
|
2023-11-27 14:31:00 +08:00
|
|
|
uint8_t hi = (uint8_t)((crc & 0xFF00) >> 8); // 高位置
|
|
|
|
uint8_t lo = (uint8_t)(crc & 0x00FF); // 低位置
|
|
|
|
*lb = lo;
|
|
|
|
*hb = hi;
|
|
|
|
}
|
2023-11-21 20:28:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int8_t crc::CheckSumCode(uint8_t CODEMODE, uint8_t *pBuffer, uint16_t Len, uint16_t offset, uint8_t *CHKA, uint8_t *CHKB)
|
|
|
|
{
|
2023-11-27 14:31:00 +08:00
|
|
|
uint16_t i = 0;
|
|
|
|
uint8_t _CHKA = 0x00, _CHKB = 0x00;
|
|
|
|
|
|
|
|
for (i = offset; i < Len; i++)
|
|
|
|
{
|
|
|
|
_CHKA += pBuffer[i];
|
|
|
|
_CHKB += _CHKA;
|
|
|
|
}
|
|
|
|
if (CODEMODE == DECODE)
|
|
|
|
{
|
|
|
|
if (_CHKA != *CHKA)
|
|
|
|
return -1;
|
|
|
|
if (_CHKB != *CHKB)
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*CHKA = _CHKA;
|
|
|
|
|
|
|
|
*CHKB = _CHKB;
|
|
|
|
}
|
|
|
|
return 0;
|
2023-11-21 20:28:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int crc::CheckSumCode(uint8_t CODEMODE, QByteArray pBuffer, uint16_t Len, uint16_t offset, uint8_t *CHKA, uint8_t *CHKB)
|
|
|
|
{
|
2023-11-27 14:31:00 +08:00
|
|
|
uint16_t i = 0;
|
|
|
|
uint8_t _CHKA = 0x00, _CHKB = 0x00;
|
|
|
|
|
|
|
|
for (i = offset; i < Len; i++)
|
|
|
|
{
|
|
|
|
_CHKA += pBuffer[i];
|
|
|
|
_CHKB += _CHKA;
|
|
|
|
}
|
|
|
|
if (CODEMODE == DECODE)
|
|
|
|
{
|
|
|
|
if (_CHKA != *CHKA)
|
|
|
|
return -1;
|
|
|
|
if (_CHKB != *CHKB)
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*CHKA = _CHKA;
|
|
|
|
|
|
|
|
*CHKB = _CHKB;
|
|
|
|
}
|
|
|
|
return 0;
|
2023-11-21 20:28:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QString crc::byte_array_to_string(QByteArray data)
|
|
|
|
{
|
2023-11-27 14:31:00 +08:00
|
|
|
QString ret(data.toHex().toUpper());
|
|
|
|
int len = ret.length() / 2;
|
|
|
|
for (int i = 1; i < len; i++)
|
|
|
|
{
|
|
|
|
ret.insert(2 * i + i - 1, " ");
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2023-11-21 20:28:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// u16转为整形
|
|
|
|
QString crc::byte_array_to_int_string(QByteArray data)
|
|
|
|
{
|
2023-11-27 14:31:00 +08:00
|
|
|
QString str;
|
|
|
|
int len = data.size() / 2;
|
|
|
|
for (int i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
str.append(QString::number(data[i * 2] | (data[i * 2 + 1] << 8), 10));
|
|
|
|
str.append(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
2023-11-21 20:28:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t crc::crc32(const QByteArray &data_buf)
|
|
|
|
{
|
2023-11-27 14:31:00 +08:00
|
|
|
uint32_t temp, crc = 0xFFFFFFFF;
|
|
|
|
int i, j;
|
|
|
|
i = 0;
|
|
|
|
ConverBuf cov;
|
|
|
|
if ((data_buf.size() % 4) != 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
while (i < data_buf.size())
|
|
|
|
{
|
|
|
|
cov.c_databuf[0] = data_buf[i++];
|
|
|
|
cov.c_databuf[1] = data_buf[i++];
|
|
|
|
cov.c_databuf[2] = data_buf[i++];
|
|
|
|
cov.c_databuf[3] = data_buf[i++];
|
|
|
|
temp = cov.ul_data;
|
|
|
|
for (j = 0; j < 32; j++)
|
|
|
|
{
|
|
|
|
if ((crc ^ temp) & 0x80000000)
|
|
|
|
{
|
|
|
|
crc = 0x04C11DB7 ^ (crc << 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
crc <<= 1;
|
|
|
|
}
|
|
|
|
temp <<= 1;
|
|
|
|
}
|
|
|
|
crc &= 0xFFFFFFFF;
|
|
|
|
}
|
|
|
|
return crc;
|
2023-11-21 20:28:23 +08:00
|
|
|
}
|
|
|
|
|
2023-11-27 14:31:00 +08:00
|
|
|
QString crc::uint8_array_to_string(uint8_t *buf, int len)
|
2023-11-21 20:28:23 +08:00
|
|
|
{
|
2023-11-27 14:31:00 +08:00
|
|
|
QString temp, msg;
|
|
|
|
int j = 0;
|
2023-11-21 20:28:23 +08:00
|
|
|
|
2023-11-27 14:31:00 +08:00
|
|
|
while (j < len)
|
|
|
|
{
|
|
|
|
temp = QString("%1 ").arg((int)buf[j], 2, 16, QLatin1Char('0'));
|
|
|
|
msg.append(temp);
|
|
|
|
j++;
|
|
|
|
}
|
2023-11-21 20:28:23 +08:00
|
|
|
|
2023-11-27 14:31:00 +08:00
|
|
|
return msg;
|
2023-11-21 20:28:23 +08:00
|
|
|
}
|