From 53bdd626527cbca9b276559eefef39d22317a003 Mon Sep 17 00:00:00 2001 From: "Eloan.Andre" Date: Fri, 24 Mar 2023 14:55:16 +0000 Subject: [PATCH] Factorisation de code --- src/__pycache__/ecritureBDD.cpython-39.pyc | Bin 941 -> 1411 bytes src/__pycache__/regles.cpython-39.pyc | Bin 0 -> 410 bytes src/decoder_propre.py | 43 ++++++++++++--------- 3 files changed, 25 insertions(+), 18 deletions(-) create mode 100644 src/__pycache__/regles.cpython-39.pyc diff --git a/src/__pycache__/ecritureBDD.cpython-39.pyc b/src/__pycache__/ecritureBDD.cpython-39.pyc index d403fd431e42605df7d367f47a17abd66899823e..10b8d6c9a487b16f05a7a006c033834086bb817a 100644 GIT binary patch delta 770 zcmY*X&2AGh5VqI*oAifp07VKSR0t7KE`Y>=ii$Sv30jmyLagAhT03FYWjC4i0@S$Yf@PAwHALc zztfsB$z#j6q#?ye=G)Sg7J5hWKI_=$lyDGoa(_l0y@R%kmi<5|*eN?>B`^3Xm;8)# zhRi_4h*&8Kqsw03EKMy6QyNV+;{`F%WJ|@Ik~B27*knVSrxvDJg@q|6r7cD2XuCj9 zF{OLKaPM~MVG)mXM6=0;SQr`NkpbhPCe4SetWCy+Q@BSK3@{oW)?~|!3x=zif$Egx zi^KtE((1FxK-&0r=ip?!zxQ-+chJ_yW3?Gb2`bCf=DQ$PIkfkChtJx59q2r3ANIO? zz4p$_|05rr8`vJ8=A9Gl4?0d|hdE>^RF3+f!d$DV?+%p?-vn`7i73&P4Qdpt%=bo0 z_m;F0u}V7i$_Ud+V0tC+BEBaB9nkp-*XeuTi=#~Ua!NG;onI$>5kL|pqsj}@L`FJF zlZr?F3T-_JLp9d6l3y6C&p++nwegPG1_I+Quc4V}8)(E;T@$cQnFw@mI6h36W@Er-#H39` zR1pr0^Y00k^BmAvoafj_M%No{zrbV^_w;*Sn6`Dr;_k6{&`wuw-8M_LTsO{aE$PCV zsu=4oy4OCgwR`eiPPjk46#$xJ;&~n#-nks7T<*@RK3a91Zj?{@A3UlfD(ENEmsV{z R`i9rG{1HK%%2XtnM1OA*PY?hA literal 0 HcmV?d00001 diff --git a/src/decoder_propre.py b/src/decoder_propre.py index d9072b1..8a8c780 100644 --- a/src/decoder_propre.py +++ b/src/decoder_propre.py @@ -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)