添加自研批检仪检测命令
This commit is contained in:
274
base/crc.cpp
274
base/crc.cpp
@@ -2,191 +2,179 @@
|
||||
#include <QDebug>
|
||||
#include "QString"
|
||||
|
||||
|
||||
crc::crc()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
uint8_t crc::crc8(uint8_t *Ptr,uint8_t num)
|
||||
uint8_t crc::crc8(uint8_t *Ptr, uint8_t num)
|
||||
{
|
||||
|
||||
uint8_t crc = 0;
|
||||
uint16_t j,i;
|
||||
uint8_t crc = 0;
|
||||
uint16_t j, i;
|
||||
|
||||
for (j = 0; j < num; j++)
|
||||
for (j = 0; j < num; j++)
|
||||
{
|
||||
crc ^= *(Ptr + j);
|
||||
for (i = 0; i < 8; i++)
|
||||
{
|
||||
crc ^= *(Ptr+j);
|
||||
for ( i = 0; i < 8; i++)
|
||||
{
|
||||
if ((crc & 0x01) != 0)
|
||||
{
|
||||
crc >>= 1;
|
||||
crc ^= 0x8c;
|
||||
}
|
||||
else
|
||||
{
|
||||
crc >>= 1;
|
||||
}
|
||||
}
|
||||
if ((crc & 0x01) != 0)
|
||||
{
|
||||
crc >>= 1;
|
||||
crc ^= 0x8c;
|
||||
}
|
||||
else
|
||||
{
|
||||
crc >>= 1;
|
||||
}
|
||||
}
|
||||
return crc;
|
||||
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void crc::crc16(uint8_t *data, int offset, int len,uint8_t *lb,uint8_t *hb)
|
||||
void crc::crc16(uint8_t *data, int offset, int len, uint8_t *lb, uint8_t *hb)
|
||||
{
|
||||
if (len > 0)
|
||||
if (len > 0)
|
||||
{
|
||||
uint16_t crc = 0xFFFF;
|
||||
int i = offset;
|
||||
for (; i < len; i++)
|
||||
{
|
||||
uint16_t crc = 0xFFFF;
|
||||
int i = offset;
|
||||
for (; i < len; i++)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
uint8_t hi = (uint8_t)((crc & 0xFF00) >> 8); //高位置
|
||||
uint8_t lo = (uint8_t)(crc & 0x00FF); //低位置
|
||||
*lb=lo;*hb=hi;
|
||||
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);
|
||||
}
|
||||
}
|
||||
uint8_t hi = (uint8_t)((crc & 0xFF00) >> 8); // 高位置
|
||||
uint8_t lo = (uint8_t)(crc & 0x00FF); // 低位置
|
||||
*lb = lo;
|
||||
*hb = hi;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int8_t crc::CheckSumCode(uint8_t CODEMODE, uint8_t *pBuffer, uint16_t Len, uint16_t offset, uint8_t *CHKA, uint8_t *CHKB)
|
||||
{
|
||||
uint16_t i = 0;
|
||||
uint8_t _CHKA = 0x00,_CHKB = 0x00;
|
||||
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;
|
||||
|
||||
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;
|
||||
*CHKB = _CHKB;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int crc::CheckSumCode(uint8_t CODEMODE, QByteArray pBuffer, uint16_t Len, uint16_t offset, uint8_t *CHKA, uint8_t *CHKB)
|
||||
{
|
||||
uint16_t i = 0;
|
||||
uint8_t _CHKA = 0x00,_CHKB = 0x00;
|
||||
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;
|
||||
|
||||
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;
|
||||
*CHKB = _CHKB;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
QString crc::byte_array_to_string(QByteArray data)
|
||||
{
|
||||
QString ret(data.toHex().toUpper());
|
||||
int len = ret.length()/2;
|
||||
for(int i=1;i<len;i++)
|
||||
{
|
||||
ret.insert(2*i+i-1," ");
|
||||
}
|
||||
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;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
// u16转为整形
|
||||
QString crc::byte_array_to_int_string(QByteArray data)
|
||||
{
|
||||
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(',');
|
||||
}
|
||||
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;
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
uint32_t crc::crc32(const QByteArray &data_buf)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
QString crc::uint8_array_to_string(uint8_t * buf,int len)
|
||||
{
|
||||
QString temp,msg;
|
||||
int j = 0;
|
||||
|
||||
while (j<len)
|
||||
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++)
|
||||
{
|
||||
temp = QString("%1 ").arg((int)buf[j], 2, 16, QLatin1Char('0'));
|
||||
msg.append(temp);
|
||||
j++;
|
||||
if ((crc ^ temp) & 0x80000000)
|
||||
{
|
||||
crc = 0x04C11DB7 ^ (crc << 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
crc <<= 1;
|
||||
}
|
||||
temp <<= 1;
|
||||
}
|
||||
|
||||
return msg;
|
||||
crc &= 0xFFFFFFFF;
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
QString crc::uint8_array_to_string(uint8_t *buf, int len)
|
||||
{
|
||||
QString temp, msg;
|
||||
int j = 0;
|
||||
|
||||
while (j < len)
|
||||
{
|
||||
temp = QString("%1 ").arg((int)buf[j], 2, 16, QLatin1Char('0'));
|
||||
msg.append(temp);
|
||||
j++;
|
||||
}
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
Reference in New Issue
Block a user