Open kbhse opened 4 years ago
yes sure that
while( Anemometer_buf[1] != 0x03 ) { // if received message has an error
is quite approximate, however it prevents against most errors. Your code is very interesting, it is a good way to verify the checksum and surely I will reuse it for several of my projects. Thank you for sharing this.
Philip
Here's another issue I've been working on: I see that you're testing the 2nd byte in Anenometer_buff[] for RS485 trasmission errors:
I found that wasn't sufficient to trap all the errors. I've implemented code to do a full CRC-16/MODBUS checksum calculation. I found reference to it on stackoverflow.com and slightly modified it for C++ (Arduino/ESP8266 IDE) data types. The linked article refers to two methods of calculation. The bytewise version is much faster and that's the one I used. It uses 512 bytes of ROM which shouldn't be a problem on the ESP8266. I've attached the code I'm using. It works very reliably for me. Perhaps you might want to incorporate it into your code.
Here's the function, CRC16, that calculates the checksum:
And here's the code that calls the CRC16 function. It passes the first 5 bytes from responseFrameBuff[] to the CRC16 function, which returns the 2 byte CRC. This is tested against the CRC in responseFrameBuff[] (last 2 bytes). The code will loop until an Inquiry returns data with a valid checksum. (In my code the receive buffer is called responseFrameBuff[] rather than Anenometer_buff[] because I use it for both Anenometer and Wind Direction sensors):