Thanks for all the help, this is a bit of a learning curve!
I've found a similar routine but am still a bit off!
I can get the message out on the lcd using this
mb_read_flow holds my message, I get on the screen what I want to end up with out on rs485
I then call calculate_crc like this,
I end up with a number, unfortunatly not the right one, and it doesnt change when I alter the message!
Do I underdtand this right, *mb_read_flow points at the first file in array, like setting a fsr in asm?
Cheers Geoff
I've found a similar routine but am still a bit off!
I can get the message out on the lcd using this
Code:
uint8_t mb_read_flow [] = {0x01,0x04,0x00,0x01,0x00,0x02} ;
int i=0;
uint8_t length = sizeof(mb_read_flow) / sizeof(mb_read_flow[0]);
for (i = 0; i < length; i++) {
printf("%02x", mb_read_flow[i]);
}
I then call calculate_crc like this,
Code:
uint16_t crc16 = calculate_crc(mb_read_flow,length);
printf("%2x",crc16);
Code:
uint16_t calculate_crc(uint8_t *mb_read_flow, uint8_t length) {
uint16_t crc = 0xFFFF;
uint8_t i;
while (length--) {
crc ^= *mb_read_flow++; // XOR byte into least sig. byte of crc
for (i = 8; i != 0; i--) { // Loop over each bit
if ((crc & 0x0001) != 0) { // If the LSB is set
crc >>= 1;
crc ^= 0xA001;
}
else {
crc >>= 1;
}
}
return crc;
}
}
Cheers Geoff
Last edited: