-2

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".

4
  • 2
    The second parameter of the int() function is the number base of the first parameter, not the width in bits. Commented Apr 22, 2023 at 20:43
  • 2
    To only get the value of one bit and not also all higher bits, the already shifted value must be modified by & 1. Commented Apr 22, 2023 at 20:44
  • 1
    Converting hex 0x11 to binary gives the result 00010001. Splitting that into upper and lower nibbles gives 0001 0001, which don't match any of the possible expected values. Is the value being read backwards? Commented Apr 22, 2023 at 20:45
  • The first nimble seems to be read correctly. I expect "0001" and that is correct. so I doubt it is being read backwards. Commented Apr 22, 2023 at 23:55

1 Answer 1

0

If the device is sending the x11 when initially connecting, or after the initial attempt to get data from it, it could be that it's trying to initiate a software flow control, or software flow control is enabled. Without knowing more about the device and how it's connected its hard to say for sure.

The hex code \x11 in example above is 00010001 or 0001 0001 is the ASCII control DC1, which is used for software flow control to indicate it's ready to receive commands.

https://www.asciihex.com/character/control/17/0x11/dc1-device-control-one-xon-

Sign up to request clarification or add additional context in comments.

3 Comments

That is what confusing me. Now I can read the bits correctly like below, however the response is not what is expected. It's a mechanical optical filter wheel and it actually moves to position 1 and has to return "1010" as the upper nimble however, it returns 0011. Please see update1 in my original post.
Oddly enough \x16 or 0001 0110 is another control code, this time synchronous idle or sync, which tells you the device is now idle and will take commands, its basically away to synchronize communications over serial. Outside of that it could also be a valid code, assuming this is right wheel Andover Filter Wheel it could also mean it believes itself to be jammed. as 0110 for the lower nimble would be a 12 position wheel in 12 o'clock position mode that jammed in run mode. try sending \x73.
Yes, that is the wheel. Reviewing this again, I'm realizing the bits are read backwards as @Nick ODell mentioned above. In "00010110" the bit numbers are 87654321. Now everything makes sense. Thanks for your input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.