How to send correctly an unsigned long byte with Arduino?

0

I am testing the serial communication with Arduino like this:

  timeStamp = 0xFFFFFFFF;
  Serial.flush();   
  Serial.write((uint8_t*)timeStamp, 4)

On the other side, I am reading this byte with read(fd, &rx, 4) in a C program. I expect to read always the same value, but instead of reading 0xffffffff I have 0x370066bf so there is a problem with decoding. Ps: I have set the serial port to read raw bytes, so there are no dedicated bytes to encode carriage return or other special characters.

c
arduino-uno
serial-communication
asked on Stack Overflow Jul 27, 2018 by LiukPet • edited Jul 27, 2018 by LiukPet

1 Answer

2

I believe your problem is that your Serial.write((uint8_t*)timeStamp, 4) always sends 4 bytes from memory starting with address 0xFFFFFFFF. This is because timeStamp is not a pointer, but you cast it to a pointer. Please try casting the address of timeStamp instead: Serial.write(((uint8_t*)(&timeStamp)), 4); and see if that works.

answered on Stack Overflow Jul 30, 2018 by LiviuH

User contributions licensed under CC BY-SA 3.0