You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a PDO which has 2 bit in one byte. Say bit 0 and bit 1.
If I set or reset bit 0 and read it back, all behaves as expected. But if I do set and reset bit 1 I actually only change bit 0 again.
I got to the bottom of this and the problem is in file pdo.py in the functions set_data and get_data.
In function set_data this line destroys all the correctly done bitshifting and masking to one bool that gets written at one byte location
squishes everything from one byte into one boolean which sits at bit 0. The shifting and masking will then zero out any bit above 0 that we try to read.
I could 'fix' this with this:
ifself.od.data_typeis1: #is of type booleandata=self.msg.data[byte_offset]
data= (data>>bit_offset) & ((1<<self.length) -1)
data=bool(data)
else:
data=od_struct.unpack_from(self.msg.data, byte_offset)[0]
data= (data>>bit_offset) & ((1<<self.length) -1)
#now comes the sign check and sign fixup
The magic number 1 here checks if the type is bool and then does not use unpack but rather shifts first and then converts to type bool later. If the type is not bool everything stays as before.
The text was updated successfully, but these errors were encountered:
I have a PDO which has 2 bit in one byte. Say bit 0 and bit 1.
If I set or reset bit 0 and read it back, all behaves as expected. But if I do set and reset bit 1 I actually only change bit 0 again.
I got to the bottom of this and the problem is in file pdo.py in the functions set_data and get_data.
In function set_data this line destroys all the correctly done bitshifting and masking to one bool that gets written at one byte location
using this line instead 'fixes' it. I don't quite know what other sideeffects this might have.
Although this just fixes the writing part. If your try to read bit 1 it still does not work. But bit 0 would report as set if bit 1 was set.
This happens because in the function get_data this line
squishes everything from one byte into one boolean which sits at bit 0. The shifting and masking will then zero out any bit above 0 that we try to read.
I could 'fix' this with this:
The magic number 1 here checks if the type is bool and then does not use unpack but rather shifts first and then converts to type bool later. If the type is not bool everything stays as before.
The text was updated successfully, but these errors were encountered: