Factorisation de code

master
Eloan ANDRÉ 2 years ago
parent 812a9b2c9c
commit 53bdd62652

@ -10,6 +10,22 @@ import scapy.contrib.modbus as mb
from triPacket import triPacket
def BytesToBits(value):
status = []
for i in range(len(value)):
#Transform the i'th byte into 1 binary string
val = str(bin(value[i]))
#Delete the '0x' header
val = val[2:]
#Fill with '0' to have 8 values
val = val.rjust(8,'0')
#Revert the list to get into the growing order
val = val[::-1]
#Add the 8 values on `status`
status.extend(j for j in [*val])
return status
def decode(pkt):
'''Insert the packet's `pkt` the [type_call,address,status] informations into the `connec` database '''
if "ModbusADU" in pkt:
@ -38,7 +54,7 @@ def decode(pkt):
#Response for a read request packet's
if "Response" in modpkt.payload.name:
#Number of byte that have been read
byte_count = modpkt.payload.getfieldval("byteCount")
#byte_count = modpkt.payload.getfieldval("byteCount")
status = []
#Response for a read coils request packet's
if "C" in type_call:
@ -46,17 +62,11 @@ def decode(pkt):
value = modpkt.payload.getfieldval("coilStatus")
#Transform the byte's values into a list of bits values for each byte
for j in range(byte_count):
#Transform the j'th value to binary -> delete the '0x' header, fill with '0' to have 8 values
#and then revert the list to get into the growing order
val=str(bin(value[j]))[2:].rjust(8,'0')[::-1]
#Add the values on `status`
status.extend(k for k in [*val])
status = BytesToBits(value)
#Response for a read registers request packet's
else:
#Get the list of byte's values that have been read
status = modpkt.payload.getfieldval("registerVal")
#Add the read's status into the `connect` database
for j in range(len(status)):
triPacket([miniL[0][1],miniL[2]+j,status[j]],connec)
@ -81,30 +91,27 @@ def decode(pkt):
#Get the starting address
addr = modpkt.payload.getfieldval("startAddr")
#Get the list of values that have been read
output_value = modpkt.payload.getfieldval("outputsValue")
value = modpkt.payload.getfieldval("outputsValue")
status = []
#Multiple write coils request (register's values are already on the good format)
if "C" in type_call:
#Transform the byte's values into a list of bits values for each byte
for j in range(len(output_value)):
#Transform the j'th value to binary -> delete the '0x' header, fill with '0' to have 8 values
#and then revert the list to get into the growing order
val=str(bin(output_value[j]))[2:].rjust(8,'0')[::-1]
#Add the values on `status`
status.extend(k for k in [*val])
status = BytesToBits(value)
#Add the write's status into the `connect` database
for j in range(len(status)):
#Get the number of bytes to be write in order to not reset to 0, address on the same bytes of the written one's
byte_count = modpkt.payload.getfieldval("quantityOutput")
for j in range(byte_count):
triPacket([miniL[0][1],addr+j,status[j]],connec)
#Single write request
else:
#Single write coil request
if "C" in type_call:
addr = modpkt.payload.getfieldval("outputAddr")
output_value = modpkt.payload.getfieldval("outputValue")
value = modpkt.payload.getfieldval("outputValue")
#Single write register request
else:
addr = modpkt.payload.getfieldval("registerAddr")
output_value = modpkt.payload.getfieldval("registerValue")
value = modpkt.payload.getfieldval("registerValue")
#Add the write's status into the `connect` database
triPacket([miniL[0][1],addr,output_value],connec)

Loading…
Cancel
Save