I have a piece of hardware that for instance returns the "response" below. Per the instructions, I would like to separate the response to the 1st and last 4 bits. But the lower bit response doesn't make sense when I convert it like below.
I'm quoting the instructions. The possible first 4 bits are: ['1010', '0110', '1000', '0100', '1100', '0100']
response = b'\x11' # returned by device
r_int = int(response.hex(),8) # convert to int from 8 bit
print('7', r_int>>7) # Bit 7
print('6',r_int>>6)# Bit 6
print('5',r_int>>5)# Bit 5
print('4',r_int>>4)# Bit 4
"... will respond to any code sent to it with a status update, which will be sent as 8-bit binary. The first four bits, referred to as the “upper nibble”, denote the current position. The last four bits, referred to as the “lower nibble”, denote the ‘mode’ of the wheel at the time of the status update "
Update1: I corrected the response list above. The following seems to be the right code to read the bits. For example for response = b'\x11' it returns '00010001'
response_hex = response.hex()
scale = 16
n_bits = 8
bits_read = bin(int(response_hex, scale))[2:].zfill(n_bits)
print(bits_read)
while expected "1000" the device returns "\x16" that converts to "0110".
int()function is the number base of the first parameter, not the width in bits.& 1.0x11to binary gives the result00010001. Splitting that into upper and lower nibbles gives0001 0001, which don't match any of the possible expected values. Is the value being read backwards?