samedi 20 juin 2020

Python: how to fix for and if loop problem?

I made this function to code some values according to func_code value but it does not work as aaspected:

def reg_val_list_binary2(s):
   r = []
   for i in func_code(s):
       try: 
          if  i == 3:
             r = reg_val_list3(s)
          elif  i == 1: 
             r = reg_val_list_binary(s)
       except AttributeError:
          pass
 return r

Finction reg_val_list3(s) is coded as follows:

def reg_val_list3(s):
  v = []
 for p in s:
 if p.haslayer('ModbusADUResponse'):
     try:
         if p['ModbusADUResponse'][1].funcCode == 3:
             v += p['ModbusADUResponse'][1].registerVal
     except AttributeError:
         pass
 return v

And function reg_val_list_binary(s):

 def reg_val_list_binary(s):
     r = []
     for x in reg_val_list1(s):
     r += [int(bit)for bit in str( bin(x) )[2:].zfill(8)] [::-1]
 return r

And func_code:

def func_code(s):
    r = []
    for p in s:
       if p.haslayer('ModbusADURequest'):
       try:
         if p['ModbusADURequest'][1].funcCode == 3:
             r.append(3)
         elif p['ModbusADURequest'][1].funcCode == 1:
             r.append(1)
       except AttributeError:
           pass
  return r

The problem is I have the impression that it cannot find func_code == 1 while it's in the func_code(s) list which contains this values [3,3,3,3,1,1,3,3]

Any idea?

Aucun commentaire:

Enregistrer un commentaire