-1

I'm sending a byte to my Raspi 3 and my Arduino via Bluetooth Serial.

e.g. 0b00000011 (mode), 0b01000001 (mode), 0b10010000 (direction)

The LSB indicates if the byte is a direction command or a instruction to switch mode, so I need to extract it in Arduino C and in Python.

Does anyone know how to do this? Thanks in advance!

the_dani
  • 2,466
  • 2
  • 20
  • 46

1 Answers1

1

Use a bit operation:

C code

char b = 0x01;

if( b & 0x01 ) {
   // LSB is set
}
else {
   // LSB is not set
}

Python code

b = 0x01
if (b&0x01)==0x01 :
    # LSB is set
else:
    # LSB is not set

LSB = Least Significant Bit (in your case)

Dorin
  • 1,016
  • 1
  • 11
  • 15
  • This error appears: Traceback (most recent call last): File "/home/pi/Desktop/test.py", line 11, in if ( b & 0x01 ): TypeError: unsupported operand type(s) for &: 'bytes' and 'int' – the_dani Apr 08 '17 at 16:01
  • Ok great, do you know how to do it in python? – the_dani Apr 09 '17 at 09:46