I'm novice in python and I develop this function to retrieve coilstatus but in binary format:
def reg_val_list1(s):
v = []
for p in s:
if p.haslayer('ModbusADUResponse'):
try:
if p['ModbusADUResponse'][1].funcCode == 1:
v += p['ModbusADUResponse'][1].coilStatus
except AttributeError:
pass
return v
I want to obtain a binary values instead of a decimals ones. Here is my function
This function returns me values in decimal like 64,3, ... and I would like to have these same values in binary ie:
0,0,0,0,0,0,0,1,0,1,1,0,0,0,0,0,0: 0,0,0,0,0,0,1,0 = 64 and 1,1,0,0,0,0,0,0 = 3
I modified the function by putting several solutions but none of them work. I made this change:
def reg_val_list1(s):
v = []
u = []
for p in s:
if p.haslayer('ModbusADUResponse'):
try:
if p['ModbusADUResponse'][1].funcCode == 1:
v += p['ModbusADUResponse'][1].coilStatus
u = "{0:b}".format(v)
except AttributeError:
pass
return u
I also tried this:
def reg_val_list1(s):
v = []
for p in s:
if p.haslayer('ModbusADUResponse'):
try:
if p['ModbusADUResponse'][1].funcCode == 1:
v += bin (p['ModbusADUResponse'][1].coilStatus)
except AttributeError:
pass
return v
and encode() too
But nothing works. any help please ?