|
|
@ -10,6 +10,22 @@ import scapy.contrib.modbus as mb
|
|
|
|
|
|
|
|
|
|
|
|
from triPacket import triPacket
|
|
|
|
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):
|
|
|
|
def decode(pkt):
|
|
|
|
'''Insert the packet's `pkt` the [type_call,address,status] informations into the `connec` database '''
|
|
|
|
'''Insert the packet's `pkt` the [type_call,address,status] informations into the `connec` database '''
|
|
|
|
if "ModbusADU" in pkt:
|
|
|
|
if "ModbusADU" in pkt:
|
|
|
@ -38,7 +54,7 @@ def decode(pkt):
|
|
|
|
#Response for a read request packet's
|
|
|
|
#Response for a read request packet's
|
|
|
|
if "Response" in modpkt.payload.name:
|
|
|
|
if "Response" in modpkt.payload.name:
|
|
|
|
#Number of byte that have been read
|
|
|
|
#Number of byte that have been read
|
|
|
|
byte_count = modpkt.payload.getfieldval("byteCount")
|
|
|
|
#byte_count = modpkt.payload.getfieldval("byteCount")
|
|
|
|
status = []
|
|
|
|
status = []
|
|
|
|
#Response for a read coils request packet's
|
|
|
|
#Response for a read coils request packet's
|
|
|
|
if "C" in type_call:
|
|
|
|
if "C" in type_call:
|
|
|
@ -46,17 +62,11 @@ def decode(pkt):
|
|
|
|
value = modpkt.payload.getfieldval("coilStatus")
|
|
|
|
value = modpkt.payload.getfieldval("coilStatus")
|
|
|
|
|
|
|
|
|
|
|
|
#Transform the byte's values into a list of bits values for each byte
|
|
|
|
#Transform the byte's values into a list of bits values for each byte
|
|
|
|
for j in range(byte_count):
|
|
|
|
status = BytesToBits(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(value[j]))[2:].rjust(8,'0')[::-1]
|
|
|
|
|
|
|
|
#Add the values on `status`
|
|
|
|
|
|
|
|
status.extend(k for k in [*val])
|
|
|
|
|
|
|
|
#Response for a read registers request packet's
|
|
|
|
#Response for a read registers request packet's
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
#Get the list of byte's values that have been read
|
|
|
|
#Get the list of byte's values that have been read
|
|
|
|
status = modpkt.payload.getfieldval("registerVal")
|
|
|
|
status = modpkt.payload.getfieldval("registerVal")
|
|
|
|
|
|
|
|
|
|
|
|
#Add the read's status into the `connect` database
|
|
|
|
#Add the read's status into the `connect` database
|
|
|
|
for j in range(len(status)):
|
|
|
|
for j in range(len(status)):
|
|
|
|
triPacket([miniL[0][1],miniL[2]+j,status[j]],connec)
|
|
|
|
triPacket([miniL[0][1],miniL[2]+j,status[j]],connec)
|
|
|
@ -81,30 +91,27 @@ def decode(pkt):
|
|
|
|
#Get the starting address
|
|
|
|
#Get the starting address
|
|
|
|
addr = modpkt.payload.getfieldval("startAddr")
|
|
|
|
addr = modpkt.payload.getfieldval("startAddr")
|
|
|
|
#Get the list of values that have been read
|
|
|
|
#Get the list of values that have been read
|
|
|
|
output_value = modpkt.payload.getfieldval("outputsValue")
|
|
|
|
value = modpkt.payload.getfieldval("outputsValue")
|
|
|
|
status = []
|
|
|
|
status = []
|
|
|
|
#Multiple write coils request (register's values are already on the good format)
|
|
|
|
#Multiple write coils request (register's values are already on the good format)
|
|
|
|
if "C" in type_call:
|
|
|
|
if "C" in type_call:
|
|
|
|
#Transform the byte's values into a list of bits values for each byte
|
|
|
|
#Transform the byte's values into a list of bits values for each byte
|
|
|
|
for j in range(len(output_value)):
|
|
|
|
status = BytesToBits(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])
|
|
|
|
|
|
|
|
#Add the write's status into the `connect` database
|
|
|
|
#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)
|
|
|
|
triPacket([miniL[0][1],addr+j,status[j]],connec)
|
|
|
|
#Single write request
|
|
|
|
#Single write request
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
#Single write coil request
|
|
|
|
#Single write coil request
|
|
|
|
if "C" in type_call:
|
|
|
|
if "C" in type_call:
|
|
|
|
addr = modpkt.payload.getfieldval("outputAddr")
|
|
|
|
addr = modpkt.payload.getfieldval("outputAddr")
|
|
|
|
output_value = modpkt.payload.getfieldval("outputValue")
|
|
|
|
value = modpkt.payload.getfieldval("outputValue")
|
|
|
|
#Single write register request
|
|
|
|
#Single write register request
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
addr = modpkt.payload.getfieldval("registerAddr")
|
|
|
|
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
|
|
|
|
#Add the write's status into the `connect` database
|
|
|
|
triPacket([miniL[0][1],addr,output_value],connec)
|
|
|
|
triPacket([miniL[0][1],addr,output_value],connec)
|
|
|
|
|
|
|
|
|
|
|
|