Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • That answers most of what I wanted, although the part about pointer-casting doesn't have the results I was hoping to hear. What happens if I send a uint32_t, for example, through Wire.write() (or an equivalent function) directly, though? Does that send in native byte order, or does Wire handle that automatically? I'd like to avoid bit-shifting for some of my more complex messages if possible. Commented Oct 3, 2019 at 16:43
  • I kinda had three sub-questions, which I'll admit kinda doesn't help matters, but they are also somewhat related. One was what the bit-shifting actually does, the second is if the Wire library handles endian-ness discrepancies on its own, and the third (which is a bit less related) is exactly how/when data is written or requested when the master requests data -- is there a chance of the slave not having it ready in time or writing too slowly (that would cause some later section of the message to be wrong while the first was correct). Commented Oct 3, 2019 at 16:46
  • 1
    @RDragonrydr: Wire.write() doesn't handle endianness. It sends either a single byte (Wire.write(value) sends only the LSB of value) or a byte array when called as Wire.write(const uint8_t *buffer, size_t length). Commented Oct 3, 2019 at 20:37
  • The union approach is also frowned upon these days, because it is undefined behavior to read a union member different than that which was last written. In this case, you write to .data, and then read from .bytes. You are only allowed to read from .data at that point (I don't know of a single compiler where this doesn't work, though, and a pedantic comment like this is obligatory). Certain analysis suites will flag it, but that's about it. Commented Oct 20, 2024 at 19:39
  • @ardnew: You are right, it's undefined behavior according to the language standard. However, gcc explicitly allows it, so it's safe in the Arduino context. At least as long as they don't switch to a compiler that doesn't provide this guarantee. Commented Oct 20, 2024 at 21:20