Fix CRC from read data

This commit is contained in:
Valentin Dabstep 2025-05-13 19:05:54 +03:00
parent 1122c97008
commit fca10d4140
4 changed files with 124 additions and 13 deletions

View file

@ -123,19 +123,18 @@ void send_can_with_id_crc(uint8_t id, uint8_t message_type, const void* data, si
msg_l.id = id;
// msg_l.len = 8; // Protocol-defined message length
memcpy(&msg_l.buf[0], &message_type, sizeof(uint8_t));
memcpy(&msg_l.buf[1], data, 1);
memcpy(&msg_l.buf[1], data, ID_SIZE);
// Prepare CRC calculation buffer (ID + data)
size_t crc_data_size = sizeof((uint8_t)msg_l.id) + 2 * sizeof(uint8_t);
uint8_t crc_data[7];
uint8_t crc_data[CAN_MSG_MAX_LEN];
// Copy message ID
memcpy(crc_data, (uint8_t*)&msg_l.id, sizeof(uint8_t));
memcpy(crc_data, (uint8_t*)&msg_l.id, ID_SIZE);
// Copy all data bytes
for(int i = 1;i < 7;i++)
memcpy(crc_data + i,(uint8_t*)&msg_l.buf[i - 1],1);
for(int i = 1;i < CAN_MSG_MAX_LEN;i++)
memcpy(crc_data + i,(uint8_t*)&msg_l.buf[i - 1], sizeof(uint8_t)); //As byte variable
// Calculate CRC
uint16_t crc_value = validate_crc16(crc_data, 7);
uint16_t crc_value = validate_crc16(crc_data, CAN_MSG_MAX_LEN);
// Insert CRC into buffer
msg_l.buf[6] = crc_value & 0xFF;