0

In the following code I'm computing crc and setting it as last (11th) byte in the array. The problem is it's sent on I2C just when I print it on the serial terminal.

  static constexpr uint8_t CLICK_MESSAGE_SIZE{11};
  ...
  uint8_t buf[CLICK_MESSAGE_SIZE];
  memcpy(buf, msgMouseClick[clickMessageCounter], CLICK_MESSAGE_SIZE);
  buf[10] = dallas_crc8(buf, 10);
  Serial.println(buf[10], HEX); //!!!has to be here, otherwise is CRC in packet wrong!!!
  Wire.write(buf, CLICK_MESSAGE_SIZE);

When I'm assig the result of to

auto crc = dallas_crc8(buf, 10);
buf[10] = crc;
Serial.println(crc);

and Serial.print ing it. Then It's works. But with const auto crc = dallas_crc8(buf, 10); It does not!

I have slightly modified Wire library, where I'm passing pointer to the buffer and it's size. With precalculated CRCs on 11th position it's working fine even for hundreds of bytes arrays.

It feels like a black magic, or compiler error. I'm using latest arduino-1.8.10

Any tips or hints?

  • The code snippets you show look fine to me. Could you post a minimal, reproducible example showing your problem? – Edgar Bonet Oct 30 at 9:35
  • 1
    I don't get your problem. What do you mean by "it works" and what by "it does not"? Please provide exactly what you expect and what you get in both cases. – the busybee Oct 30 at 10:02
  • If the difference is just in serial monitor, maybe that missing HEX is the issue. – KIIV Oct 30 at 10:10
  • The problem is that without Serial.println(buf[10], HEX); the value of the last byte sent with Wire.write(buf, CLICK_MESSAGE_SIZE); is the same like in declaration. Not as calculated and set in buf[10] = dallas_crc8(buf, 10); The Serial.println(buf[10], HEX); is cruical for getting the last byte correctly. – Libor Tomsik Oct 30 at 13:36
  • 1
    try it with AVR boards package 1.6.21. install it in Boards Manager. it has older version of gcc – Juraj Oct 30 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.