From fc2916cf871e20ef30124a49e70dc0826433dc92 Mon Sep 17 00:00:00 2001 From: uhi22 Date: Fri, 12 May 2023 01:10:19 +0200 Subject: [PATCH] feature: convert pcap to values, and show as scope graph --- pcapConverter.py | 35 ++++++- scope.py | 232 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 265 insertions(+), 2 deletions(-) create mode 100644 scope.py diff --git a/pcapConverter.py b/pcapConverter.py index b98dd05..8894e1e 100644 --- a/pcapConverter.py +++ b/pcapConverter.py @@ -26,16 +26,26 @@ import pyshark import exiConnector import os +from helpers import combineValueAndMultiplier +import json # The path where the script will search for pcap files: directory = '../temp' +#"EVSEPresentVoltage.Multiplier": "0", +#"EVSEPresentVoltage.Value": "318", +#"EVSEPresentVoltage.Unit": "V", +#"DC_EVStatus.EVRESSSOC": "53", + def convertPcapToTxt(inputFileName): cap = pyshark.FileCapture(inputFileName, display_filter="ipv6") fileOut = open(inputFileName + '.decoded.txt', 'w') print("# generated by pcapConverter.py", file=fileOut) print("# https://github.com/uhi22/pyPLC", file=fileOut) + fileOutValues = open(inputFileName + '.values.txt', 'w') + print("# generated by pcapConverter.py", file=fileOutValues) + print("# https://github.com/uhi22/pyPLC", file=fileOutValues) # Example how to access the data: #print(cap) #print(cap[0]) @@ -58,11 +68,32 @@ def convertPcapToTxt(inputFileName): sHeader = "Packet #" + str(numberOfPackets) + " [" + str(packet.sniff_time) + "] " + strExi + " means:" pre = "DD" # decode DIN decoded=exiConnector.exiDecode(strExi, pre) - print(sHeader) - print(decoded) + #print(sHeader) + #print(decoded) print(sHeader, file=fileOut) print(decoded, file=fileOut) + try: + y = json.loads(decoded) + try: + u = combineValueAndMultiplier(y["EVSEPresentVoltage.Value"], y["EVSEPresentVoltage.Multiplier"]) + print("[" + str(packet.sniff_time) + "] EVSEPresentVoltage=" + str(u), file=fileOutValues) + i = combineValueAndMultiplier(y["EVSEPresentCurrent.Value"], y["EVSEPresentCurrent.Multiplier"]) + print("[" + str(packet.sniff_time) + "] EVSEPresentCurrent=" + str(i), file=fileOutValues) + except: + pass + try: + soc = y["DC_EVStatus.EVRESSSOC"] + print("[" + str(packet.sniff_time) + "] EVRESSSOC=" + str(soc), file=fileOutValues) + except: + pass + except: + pass + if ((numberOfPackets % 100)==0): + print(str(numberOfPackets) + " packets") + #if (numberOfPackets>=1000): + # break fileOut.close() + fileOutValues.close() # iterate over files in the directory diff --git a/scope.py b/scope.py new file mode 100644 index 0000000..12ffb85 --- /dev/null +++ b/scope.py @@ -0,0 +1,232 @@ + +from tkinter import * +import time +import sys # for argv + + +root = Tk() +root.geometry("600x500") +lastKey = '' +lblHelp = Label(root, justify= "left") +lblHelp['text']="press ctrl C to exit" +lblHelp.pack() + +canvas_width = 590 +canvas_height = 400 +divisionsPerScreen = 10 + +c = Canvas(root, + width=canvas_width, + height=canvas_height) +c.pack() + + +x0Scope = 10 +y0Scope = 45 +xSizeScope = canvas_width-30 +ySizeScope = canvas_height-50 +# osci background +c.create_rectangle(x0Scope, y0Scope, x0Scope+xSizeScope, y0Scope+ySizeScope, fill="black") +# osci divisions +for i in range(1, divisionsPerScreen): + dx= i*xSizeScope/divisionsPerScreen + c.create_line(x0Scope+dx, y0Scope, x0Scope+dx, y0Scope+ySizeScope, fill="#404040") + dy= i*ySizeScope/divisionsPerScreen + c.create_line(x0Scope, y0Scope+dy, x0Scope+xSizeScope, y0Scope+dy, fill="#404040") +# osci outer border +c.create_line(x0Scope, y0Scope, x0Scope+xSizeScope, y0Scope, fill="#FFFFFF") +c.create_line(x0Scope, y0Scope, x0Scope, y0Scope+ySizeScope, fill="#FFFFFF") +c.create_line(x0Scope+xSizeScope, y0Scope+ySizeScope, x0Scope+xSizeScope, y0Scope, fill="#FFFFFF") +c.create_line(x0Scope+xSizeScope, y0Scope+ySizeScope, x0Scope, y0Scope+ySizeScope, fill="#FFFFFF") + + +inputFileName = "../temp/2023-05-11_205813_tcpdump.pcap.values.txt" +fileIn = open(inputFileName, 'r') +Lines = fileIn.readlines() + +strCh1Color="#FFFF00" # yellow +strCh2Color="#10FF10" # green +strCh3Color="#4040FF" # blue + +strChannelName1="" +strChannelName2="" +strChannelName3="" +ch1values = [] +ch2values = [] +ch3values = [] +count = 0 +for line in Lines: + count += 1 + p1 = line.find("] ") + p2 = line.find("=") + if (p1>0) and (p2>p1+3): + s = line[p1+2:p2] + if (strChannelName1==""): + strChannelName1=s + else: + if ((strChannelName2=="") and (s!=strChannelName1)): + strChannelName2=s + else: + if ((strChannelName3=="") and (s!=strChannelName1) and (s!=strChannelName2)): + strChannelName3=s + if (s==strChannelName1): + sVal=line[p2+1:].strip() + ch1values.append(float(sVal)) + if (s==strChannelName2): + sVal=line[p2+1:].strip() + ch2values.append(float(sVal)) + if (s==strChannelName3): + sVal=line[p2+1:].strip() + ch3values.append(float(sVal)) + #print("Line{}: {}".format(count, line.strip())) +fileIn.close() + +vMin=ch1values[0] +vMax=ch1values[0] +for v in ch1values: + #print(v) + if (vvMax): + vMax = v + +print("min=" + str(vMin) + ", max=" + str(vMax)) +vDelta = vMax - vMin +perDiv1 = 1 +if (vDelta/perDiv1>10): + perDiv1=2 +if (vDelta/perDiv1>10): + perDiv1=5 +if (vDelta/perDiv1>10): + perDiv1=10 +if (vDelta/perDiv1>10): + perDiv1=20 +if (vDelta/perDiv1>10): + perDiv1=50 +if (vDelta/perDiv1>10): + perDiv1=100 +print("Scale: " + str(perDiv1) + "/div") + +offs1 = 0 +while ((offs1+perDiv1)vMax): + vMax = v + +print("min=" + str(vMin) + ", max=" + str(vMax)) +vDelta = vMax - vMin +perDiv2 = 1 +if (vDelta/perDiv2>10): + perDiv2=2 +if (vDelta/perDiv2>10): + perDiv2=5 +if (vDelta/perDiv2>10): + perDiv2=10 +if (vDelta/perDiv2>10): + perDiv2=20 +if (vDelta/perDiv2>10): + perDiv2=50 +if (vDelta/perDiv2>10): + perDiv2=100 +print("Scale: " + str(perDiv2) + "/div") + +offs2 = 0 +while ((offs2+perDiv2)vMax): + vMax = v + +print("min=" + str(vMin) + ", max=" + str(vMax)) +vDelta = vMax - vMin +perDiv3 = 1 +if (vDelta/perDiv3>10): + perDiv3=2 +if (vDelta/perDiv3>10): + perDiv3=5 +if (vDelta/perDiv3>10): + perDiv3=10 +if (vDelta/perDiv3>10): + perDiv3=20 +if (vDelta/perDiv3>10): + perDiv3=50 +if (vDelta/perDiv3>10): + perDiv3=100 +print("Scale: " + str(perDiv3) + "/div") + +offs3 = 0 +while ((offs3+perDiv3)