Pic to esp32

Thread Starter

geoffers

Joined Oct 25, 2010
500
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

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]);
        }
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,

Code:
uint16_t crc16 = calculate_crc(mb_read_flow,length);
        
        printf("%2x",crc16);
I end up with a number, unfortunatly not the right one, and it doesnt change when I alter the message!


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;
    }
}
Do I underdtand this right, *mb_read_flow points at the first file in array, like setting a fsr in asm?

Cheers Geoff
 
Last edited:

Thread Starter

geoffers

Joined Oct 25, 2010
500
Thanks, thats handy to check with!
Confirms what I was thinking, the result I end up with returning is wrong and should also change when I change one element of my array.
Is this the correct way to define my message?

uint8_t mb_read_flow [] = {0x01,0x04,0x00,0x01,0x00,0x02} ;

then,

*unit8_t mb_read_flow, should be a pointer the the first element in the array?

The result I get is always the same, it seems to me that the calculate_crc routine is working on a different block of data to the one I'm expecting
 

Thread Starter

geoffers

Joined Oct 25, 2010
500
This line?
crc ^= *mb_read_flow++;
I guess increase the value of the pointer, "pause!" Just changed the first byte of the message and that does change the result so I guess the pointer isn't increasing?
 

Thread Starter

geoffers

Joined Oct 25, 2010
500
Been googling a bit more;

Code:
uint16_t crc16_ccitt(const uint8_t *mb_read_flow, uint8_t length) {
    uint16_t crc = 0xFFFF; // Initial value
    for (uint8_t i = 0; i < length; i++) {
        crc ^= mb_read_flow[i];
        for (int j = 0; j < 8; j++) {
            if (crc & 0x0001){
                crc >>=1;
                crc ^= 0xA001;
            }else
                crc >>= 1;
        }
    }
    return crc;
}
This works and returns the right crc! Thanks for all the help so far, next step is to pickup the speed from the tractor output, hopefully should be a little more straight forward...
 
Top