hi all,
i connected 2 arduinos unos via i2c , need send long variables. therefor convert long byte array , send it.
my problem re-conversion correctly received 4 bytes long variable:
here code fragment:
this gives this:
but why have 16 bit long? googled lot did not find solution :-/
all best
tim
i connected 2 arduinos unos via i2c , need send long variables. therefor convert long byte array , send it.
my problem re-conversion correctly received 4 bytes long variable:
here code fragment:
code: [select]
readbytes(arduino_slave_i2c_address, 8 , &rawdata[0]); // read 8 raw data bytes
long myvariable =( (rawdata[0] << 24) | (rawdata[1] << 16) | rawdata[2]<<8 | rawdata[3] ); // turn bytes signed 32-bit value
serial.print(rawdata[0],bin);serial.print(" ");serial.print(rawdata[1],bin);serial.print(" ");serial.print(rawdata[2],bin);serial.print(" ");serial.println(rawdata[3],bin);
serial.println(test, bin);
serial.println(test, dec);
this gives this:
quote
11 101100 1010000 10101000so bytes correct, controlled it! long number should "53235880"
101000010101000
20648
but why have 16 bit long? googled lot did not find solution :-/
all best
tim
integer expressions evaluated ints, if no operand long.
you cast bytes shifting unsigned longs.
you cast bytes shifting unsigned longs.
code: [select]
long myvariable =( (((unsigned long)rawdata[0]) << 24) | (((unsigned long)rawdata[1]) << 16) | (((unsigned long)rawdata[2])<<8) | rawdata[3] );
Arduino Forum > Using Arduino > Networking, Protocols, and Devices (Moderator: fabioc84) > Sending long variables via I2C but only 16bit for long as result
arduino
Comments
Post a Comment