I have code written in C++ that I am translating to Python. The C++ code is:
WriteBuffer[0] = unsigned char (0xc0 + (Steer & 0x1F)); // change the signal to conform to jrk motor controller
WriteBuffer[1] = unsigned char (Steer >> 5) & 0x7F; // two-word feedback signal
if(!WriteFile(hSerial[1], &WriteBuffer, 2, &BytesWritten, NULL)){
//error occurred. Report to user.
cout<<"error writing T2 \n";
cout<<BytesWritten;
}
The code I have written in Python, which does not work, is this:
rightWheel = bytearray(b'\xc000')
rightWheel[0] = rightWheel[0] + (rightWheelSteer & 0x1F)
rightWheel[1] = (rightWheelSteer >> 5) & 0x7F
rightWheel = bytes(rightWheel)
ser2.write(rightWheel)
The first byte of rightWheel seems to contain the correct data, but the second byte does not. I want rightWheel[1] to contain a single byte, but it does not.
What Python code will let me set up a single byte containing a variable shifted right five bits and then bitwise-anded with 0x7F?