本文整理汇总了C++中crc8函数的典型用法代码示例。如果您正苦于以下问题:C++ crc8函数的具体用法?C++ crc8怎么用?C++ crc8使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了crc8函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc, char **argv) {
fprintf(stderr, "util:: test\n");
uint8_t msg[] = {0x08, 0x0a, 0xe8, 0x80};
fprintf(stderr, "util::crc8(): odd parity: %02X\n", crc8(msg, 3, 0x80));
fprintf(stderr, "util::crc8(): even parity: %02X\n", crc8(msg, 4, 0x80));
return 0;
}
示例2: receiveMedtronicMessage
uint8_t receiveMedtronicMessage( uint8_t *message, uint16_t *length ) {
uint16_t i = 0;
uint8_t calcCRC = 0;
uint16_t calcCRC16 = 0;
RFST = RFST_SIDLE;
RFST = RFST_SRX;
PKTLEN = 0xFF;
lastData = 0xFF;
for( i = 0; i < 500 && lastData != 0x00; i++ ) {
while( !RFTXRXIF ) {
// TODO Add generic rx/tx uart code
//usbUartProcess( );
//usbReceiveData( );
}
rfMessage[i] = RFD;
lastData = rfMessage[i];
TCON &= ~0x02;
}
rfLength = i - 1;
RFST = RFST_SIDLE;
decode4b6b( rfMessage, rfLength, message, length );
calcCRC = crc8( message, (*length) - 1 );
if( calcCRC == message[(*length) - 1] ) {
return 0;
}
calcCRC16 = crc16( message, (*length) - 2 );
if( ((uint8_t)(calcCRC16 & 0x00FFu) == message[(*length) - 1]) &&
((uint8_t)((calcCRC16 >> 8) & 0x00FFu) == message[(*length) - 2]) ) {
return 0;
}
calcCRC = crc8( message, (*length) - 2 );
if( calcCRC == message[(*length) - 2] ) {
(*length) = (*length) - 1;
return 0;
}
calcCRC16 = crc16( message, (*length) - 3 );
if( ((uint8_t)(calcCRC16 & 0x00FFu) == message[(*length) - 2]) &&
((uint8_t)((calcCRC16 >> 8) & 0x00FFu) == message[(*length) - 3]) ) {
(*length) = (*length) - 1;
return 0;
}
crc16Init( );
return 1;
}
示例3: _nvram_commit
/* Regenerate NVRAM. Should be locked. */
int _nvram_commit(struct nvram_header *header)
{
// char *init, *config, *refresh, *ncdl;
char *ptr, *end;
int i;
struct nvram_tuple *t;
struct nvram_header tmp;
uint8 crc;
/* Regenerate header */
header->magic = NVRAM_MAGIC;
header->crc_ver_init = NVRAM_DEFAULT << NVRAM_OPT_SHIFT | NVRAM_VERSION << NVRAM_VER_SHIFT;
/* Clear data area */
ptr = (char *)header + sizeof(struct nvram_header);
bzero(ptr, NVRAM_SPACE - sizeof(struct nvram_header));
/* Leave space for a double NUL at the end */
end = (char *)header + NVRAM_SPACE - 2;
/* Write out all tuples */
for (i = 0; i < ARRAYSIZE(nvram_hash); i++) {
for (t = nvram_hash[i]; t; t = t->next) {
if ((ptr + strlen(t->name) + 1 + strlen(t->value) + 1) > end)
break;
ptr += sprintf(ptr, "%s=%s", t->name, t->value) + 1;
}
}
/* End with a double NUL */
ptr += 2;
/* Set new length */
header->len = ROUNDUP(ptr - (char *)header, 4);
/* Little-endian CRC8 over the last 11 bytes of the header */
tmp.crc_ver_init = htol32(header->crc_ver_init);
tmp.config_refresh = htol32(header->config_refresh);
tmp.config_ncdl = htol32(header->config_ncdl);
crc = crc8((char *)&tmp + 12, sizeof(struct nvram_header) - 12, CRC8_INIT_VALUE);
/* Continue CRC8 over data bytes */
crc = crc8((char *)&header[1], header->len - sizeof(struct nvram_header), crc);
/* Set new CRC8 */
header->crc_ver_init |= crc;
/* Reinitialize hash table */
return nvram_rehash(header);
}
示例4: crc8
//-------------------------------------------------------
bool t_tera_ranger_one_controller::check_echo(void)
{
if (trigger_started) {
if (Wire.available() == 3) {
byte buf[3];
buf[0] = Wire.read();
buf[1] = Wire.read();
buf[2] = Wire.read();
int distance = (buf[0] << 8) + buf[1]; // Calculate distance in mm
int crc = crc8(buf, 2); // Calculate checksum
if ((crc - buf[2]) == 0)
last_read_distance = distance;
else
last_read_distance = -1;
trigger_started = false;
return true;
}
else
return false;
}
else
return false;
}
示例5: ccodelte_encode
void
ccodelte_encode (int32_t numbits,
uint8_t add_crc,
uint8_t *inPtr,
uint8_t *outPtr,
uint16_t rnti)
{
uint32_t state;
uint8_t c, out, first_bit;
int8_t shiftbit=0;
uint16_t c16;
uint16_t next_last_byte=0;
uint32_t crc=0;
#ifdef DEBUG_CCODE
uint32_t dummy=0;
#endif //DEBUG_CCODE
/* The input bit is shifted in position 8 of the state.
Shiftbit will take values between 1 and 8 */
state = 0;
if (add_crc == 1) {
crc = crc8(inPtr,numbits);
first_bit = 2;
c = (uint8_t)(crc>>24);
} else if (add_crc == 2) {
示例6: DEBUG_LOG
void ErrorControl::re_transmit(int promptFactor)
{
for( char f = send_head; f != send_tail; inc_frame_id(f) )
{
if( !are_all_acked(f) && need_re_send(f, promptFactor) )
{
// count no. of remote player to re_send
int resendSuccess = 0;
int resendFail = 0;
char *ecMsg = send_queue[f].queue_buf;
DWORD ecMsgLen = send_queue[f].length();
for( char ecPlayerId = 1; ecPlayerId <= MAX_PLAYER; ++ecPlayerId )
{
// resend to specific remote player
if( dp_id[ecPlayerId-1] && !ack_flag[f][ecPlayerId-1] )
{
#if (defined(DEBUG) && DEBUG_LOG_LEVEL >= 2)
debugStr = "ec.remote : time-out retransmit frame ";
debugStr += f;
debugStr += " to ";
debugStr += ecPlayerId;
DEBUG_LOG(debugStr);
#endif
if( mp_ptr->send(dp_id[ecPlayerId-1], ecMsg, ecMsgLen))
resendSuccess++;
else
resendFail++;
}
if( resendSuccess > 0)
{
if( resendFail > 0)
{
// some resend fail, mark short resend time
mark_send_time(f, SHORT_TIME_OUT);
}
else
{
// all resend success, mark longer resend time
mark_send_time(f, TIME_OUT );
}
// mark the func_id of the message RE_SEND
((EcMsgHeader *)ecMsg)->func_id = RE_SEND;
// recalculate CRC
*((CRC_TYPE *) (ecMsg + ecMsgLen - CRC_LEN )) = crc8((unsigned char *)ecMsg, ecMsgLen - CRC_LEN);
}
else if( resendFail > 0)
{
// all fail, mark a shorter time
// mark send time, mark a shorter time
mark_send_time(f, SHORT_TIME_OUT);
}
}
}
}
}
示例7: crc8
bool Keys::isCRCOK()
{
uint8_t crc = crc8( (uint8_t*)data, sizeof(KeysCRCed), CRC8INIT );
Serial.println (data->crc8); // added by eqiglii
Serial.println (crc);
return (crc == data->crc8);
}
示例8: occ_read_device_info_cmd
/*==============================================
*
* 函数名称:static void occ_read_device_info_cmd(void)
* 函数功能:
* 入口参数:
* 出口参数:
* 修改日期:2015-12-08
* 修改作者:zhuchengzhi
*
* ============================================*/
static void occ_read_device_info_cmd(void)
{
int uuid_fd = 0,cfg_fd = 0,size = 0;
uuid_fd = open("/yaffs/uuid.bin",O_RDONLY);
if(uuid_fd < 0){
printf("open file error \n");
}
size = read(uuid_fd,board_info.uuid,16);
close(uuid_fd);
cfg_fd = open("/yaffs/dev_config.bin",O_RDONLY);
if(cfg_fd < 0){
printf("open file error \n");
}
size = read(cfg_fd,board_info.name,104);
close(cfg_fd);
memcpy(&uart485_data.uartx_txbuf[5],&board_info.uuid[0],16);
memcpy(&uart485_data.uartx_txbuf[21],&board_info.name[1],103);
uart485_data.uartx_txbuf[0] = 0x7E;
uart485_data.uartx_txbuf[1] = 0x00;
uart485_data.uartx_txbuf[2] = 0x7C;
uart485_data.uartx_txbuf[3] = 0x01;
uart485_data.uartx_txbuf[4] = board_info.name[0];
uart485_data.uartx_txbuf[124] = crc8(uart485_data.uartx_txbuf,(uart485_data.uartx_txbuf[1] << 8) | uart485_data.uartx_txbuf[2]);
uart485_data.uartx_txbuf[125] = 0x5A;
send_data_tty(com1_fd,uart485_data.uartx_txbuf,(((uart485_data.uartx_txbuf[1] << 8) | uart485_data.uartx_txbuf[2]) + 2));
}
示例9: occ_write_device_uuid_info
/*==============================================
*
* 函数名称:static void occ_write_device_uuid_info(uint8_t *src)
* 函数功能:
* 入口参数:
* 出口参数:
* 修改日期:2015-12-04
* 修改作者:zhuchengzhi
*
* ============================================*/
static void occ_write_device_uuid_info(uint8_t *src)
{
int uuid_fd = 0 ,size = 0,ret = 0;
uint8_t buffer[80];
uuid_fd = open("/yaffs/uuid.bin",O_RDWR | O_CREAT);
if(uuid_fd < 0){
printf("open file error \n");
}
write(uuid_fd,&src[4],16);
close(uuid_fd);
uuid_fd = open("/yaffs/uuid.bin",O_RDONLY);
if(uuid_fd < 0){
printf("open file error \n");
}
size = read(uuid_fd,buffer,sizeof(buffer));
close(uuid_fd);
ret = memcmp(buffer,&src[4],16);
if(!ret){
uart485_data.uartx_txbuf[4] = 0x00;
}else{
uart485_data.uartx_txbuf[4] = 0x01;
}
uart485_data.uartx_txbuf[0] = 0x7E;
uart485_data.uartx_txbuf[1] = 0x00;
uart485_data.uartx_txbuf[2] = 0x05;
uart485_data.uartx_txbuf[3] = 0x14;
uart485_data.uartx_txbuf[5] = crc8(uart485_data.uartx_txbuf,(((uart485_data.uartx_txbuf[1] << 8) | uart485_data.uartx_txbuf[2])));
uart485_data.uartx_txbuf[6] = 0x5A;
send_data_tty(com1_fd,uart485_data.uartx_txbuf,(((uart485_data.uartx_txbuf[1] << 8) | uart485_data.uartx_txbuf[2]) + 2));
}
示例10: ds18x20_read_temp
uint8_t ds18x20_read_temp(int16_t *t, int8_t *e)
{
uint8_t i;
if(ow_command(COMMAND_CONVERT_T,NULL) == RES_FAULT) return RES_FAULT;
//ow_strong_pullup_line();
_delay_ms(200); //
//ow_normal_line();
if(ow_command(COMMAND_READ_SP,NULL) == RES_FAULT) return RES_FAULT;
for (i=0; i<9; i++)
ds18x20_sp[i] = ow_byte_read();
if(crc8(ds18x20_sp,9) == CRC_OK)
{
*t = ((ds18x20_sp[1] << 8) | (ds18x20_sp[0])) / 16;
*e = ((ds18x20_sp[0] & M00001111) * 10) / 16;
return RES_OK;
}
else
{
//*t = 127;
//*e = 0;
return RES_FAULT;
}
}
示例11: to_alpus_dec
int to_alpus_dec(const struct tag *tag, char *output)
{
uint8_t cust_id = mirror_nibbles(tag->cust_id);
uint32_t data = mirror_nibbles(tag->data);
uint64_t code = ((uint64_t)cust_id << 8 * sizeof(data)) | data;
// Collect ID from data by bytes and conv them to DEC
uint8_t *code_bytes = (uint8_t*)&code;
char data_str[20 + 1] = "";
for (int i = 4; i >= 0; i--)
{
char byte[4 + 1];
snprintf(byte, 4 + 1, "%i:", code_bytes[i]);
strncat(data_str, byte, 4 + 1);
}
// Compute parity nibble and CRC byte
uint8_t parity = comp_parity(code);
uint8_t crc = crc8(code, parity);
// Print result
snprintf(output, OUTPUT_MAX_LEN, "255:%i:%s%i", 0xF0 + parity, data_str, crc);
return 0;
}
示例12: sendTagCellShield
void sendTagCellShield(char *mem, const char* tag, const char* data)
{
long int tsize = strlen(tag);
long int dsize = strlen(data);
unsigned char checksum = crc8(tag, 0);
checksum = crc8(data, checksum);
//Get hex of checksum
char hex1 = getHexOfNibble(checksum >> 4);
char hex2 = getHexOfNibble(checksum);
memcpy(mem, tag, strlen(tag));
mem[tsize] = '^';
memcpy(mem + tsize + 1, data, dsize);
mem[tsize + 1 + dsize] = ':';
mem[tsize + 2 + dsize] = hex1;
mem[tsize + 3 + dsize] = hex2;
}
示例13: sprom_read_pci
/*
* Read in and validate sprom.
* Return 0 on success, nonzero on error.
*/
static int
sprom_read_pci(uint16 *sprom, uint byteoff, uint16 *buf, uint nbytes, bool check_crc)
{
int off, nw;
uint8 chk8;
int i;
off = byteoff / 2;
nw = ROUNDUP(nbytes, 2) / 2;
/* read the sprom */
for (i = 0; i < nw; i++)
buf[i] = R_REG(&sprom[off + i]);
if (check_crc) {
/* fixup the endianness so crc8 will pass */
htol16_buf(buf, nw * 2);
if ((chk8 = crc8((uchar*)buf, nbytes, CRC8_INIT_VALUE)) != CRC8_GOOD_VALUE)
return (1);
/* now correct the endianness of the byte array */
ltoh16_buf(buf, nw * 2);
}
return (0);
}
示例14: ds_read_temp
ICACHE_FLASH_ATTR
static void ds_read_temp(uint8_t *addr) {
uint8_t data[12];
ds_reset();
select(addr);
write( DS1820_READ_SCRATCHPAD, 0);
uint8_t i;
for (i = 0; i < 9; i++) {
data[i] = read();
}
if(data[8] != crc8(data, 8)) {
os_printf("ERROR - DS18B20 - CRC mismatch\n");
mqttPublishError(&mqttClient, "CRC mismatch");
// return;
}
// float arithmetic isn't really necessary, tVal and tFract are in 1/10 �C
uint16_t tVal, tFract;
char tSign;
tVal = (data[1] << 8) | data[0];
if (tVal & 0x8000) {
tVal = (tVal ^ 0xffff) + 1; // 2's complement
tSign = '-';
} else
tSign = ' ';
// datasize differs between DS18S20 and DS18B20 - 9bit vs 12bit
if (addr[0] == DS18S20) {
tFract = (tVal & 0x01) ? 50 : 0; // 1bit Fract for DS18S20
tVal >>= 1;
} else {
示例15: extract_master_key
uint8_t *
extract_master_key(const char *s, int len)
{
uint8_t *mkey = NULL; /** Master key. */
int res;
uint8_t crc;
mkey = malloc(33);
if (mkey == NULL)
return NULL;
res = pem64_decode_bytes(s, len, mkey);
if (res == 33) {
/* 33 bytes in pem - last byte should be crc8 checksum */
crc = crc8(mkey, 32);
if (crc != mkey[32]) {
fprintf(stderr,
"The provided master key's check digit is incorrect.\n"
"Please check for typing errors and character substitution.\n");
free(mkey);
return NULL;
}
} else if (res != 32) {
fprintf(stderr,
"The key given did not decode to the correct length. (%d/32)\n",
res);
free(mkey);
return NULL;
}
return mkey;
}