feature: convert pcap to values, and show as scope graph

This commit is contained in:
uhi22 2023-05-12 01:10:19 +02:00
parent 123ad659cd
commit fc2916cf87
2 changed files with 265 additions and 2 deletions

View file

@ -26,16 +26,26 @@
import pyshark import pyshark
import exiConnector import exiConnector
import os import os
from helpers import combineValueAndMultiplier
import json
# The path where the script will search for pcap files: # The path where the script will search for pcap files:
directory = '../temp' directory = '../temp'
#"EVSEPresentVoltage.Multiplier": "0",
#"EVSEPresentVoltage.Value": "318",
#"EVSEPresentVoltage.Unit": "V",
#"DC_EVStatus.EVRESSSOC": "53",
def convertPcapToTxt(inputFileName): def convertPcapToTxt(inputFileName):
cap = pyshark.FileCapture(inputFileName, display_filter="ipv6") cap = pyshark.FileCapture(inputFileName, display_filter="ipv6")
fileOut = open(inputFileName + '.decoded.txt', 'w') fileOut = open(inputFileName + '.decoded.txt', 'w')
print("# generated by pcapConverter.py", file=fileOut) print("# generated by pcapConverter.py", file=fileOut)
print("# https://github.com/uhi22/pyPLC", 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: # Example how to access the data:
#print(cap) #print(cap)
#print(cap[0]) #print(cap[0])
@ -58,11 +68,32 @@ def convertPcapToTxt(inputFileName):
sHeader = "Packet #" + str(numberOfPackets) + " [" + str(packet.sniff_time) + "] " + strExi + " means:" sHeader = "Packet #" + str(numberOfPackets) + " [" + str(packet.sniff_time) + "] " + strExi + " means:"
pre = "DD" # decode DIN pre = "DD" # decode DIN
decoded=exiConnector.exiDecode(strExi, pre) decoded=exiConnector.exiDecode(strExi, pre)
print(sHeader) #print(sHeader)
print(decoded) #print(decoded)
print(sHeader, file=fileOut) print(sHeader, file=fileOut)
print(decoded, 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() fileOut.close()
fileOutValues.close()
# iterate over files in the directory # iterate over files in the directory

232
scope.py Normal file
View file

@ -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 (v<vMin):
vMin = v
if (v>vMax):
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)<vMin):
offs1+=perDiv1
print("Offset: " + str(offs1))
x = x0Scope
for v in ch1values:
yPix = y0Scope+ySizeScope - (v - offs1)/perDiv1 * ySizeScope / divisionsPerScreen
x1, y1 = ( x - 1 ), ( yPix - 1 )
x2, y2 = ( x + 1 ), ( yPix + 1 )
c.create_line(x1, y1, x2, y2, fill=strCh1Color)
c.create_line(x1, y2, x2, y1, fill=strCh1Color)
x+=0.035
vMin=ch2values[0]
vMax=ch2values[0]
for v in ch2values:
#print(v)
if (v<vMin):
vMin = v
if (v>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)<vMin):
offs2+=perDiv2
print("Offset: " + str(offs2))
x = x0Scope
for v in ch2values:
yPix = y0Scope+ySizeScope - (v - offs2)/perDiv2 * ySizeScope / divisionsPerScreen
x1, y1 = ( x - 1 ), ( yPix - 1 )
x2, y2 = ( x + 1 ), ( yPix + 1 )
c.create_line(x1, y1, x2, y2, fill=strCh2Color)
c.create_line(x1, y2, x2, y1, fill=strCh2Color)
x+=0.035
vMin=ch3values[0]
vMax=ch3values[0]
for v in ch3values:
#print(v)
if (v<vMin):
vMin = v
if (v>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)<vMin):
offs3+=perDiv3
print("Offset: " + str(offs3))
x = x0Scope
for v in ch3values:
yPix = y0Scope+ySizeScope - (v - offs1)/perDiv3 * ySizeScope / divisionsPerScreen
x1, y1 = ( x - 1 ), ( yPix - 1 )
x2, y2 = ( x + 1 ), ( yPix + 1 )
c.create_line(x1, y1, x2, y2, fill=strCh3Color)
c.create_line(x1, y2, x2, y1, fill=strCh3Color)
x+=0.035
y0Legend = 0
ySizeLegend = 40
# legend background
c.create_rectangle(x0Scope, y0Legend, x0Scope+xSizeScope, y0Legend+ySizeLegend, fill="black")
c.create_text(x0Scope,y0Legend+0, text=strChannelName1, anchor="nw", fill=strCh1Color)
c.create_text(x0Scope,y0Legend+10, text=str(perDiv1) + "/div", anchor="nw", fill=strCh1Color)
c.create_text(x0Scope,y0Legend+20, text="Offs " + str(offs1), anchor="nw", fill=strCh1Color)
c.create_text(x0Scope+150,y0Legend+0, text=strChannelName2, anchor="nw", fill=strCh2Color)
c.create_text(x0Scope+150,y0Legend+10, text=str(perDiv2) + "/div", anchor="nw", fill=strCh2Color)
c.create_text(x0Scope+150,y0Legend+20, text="Offs " + str(offs2), anchor="nw", fill=strCh2Color)
c.create_text(x0Scope+300,y0Legend+0, text=strChannelName3, anchor="nw", fill=strCh3Color)
c.create_text(x0Scope+300,y0Legend+10, text=str(perDiv3) + "/div", anchor="nw", fill=strCh3Color)
c.create_text(x0Scope+300,y0Legend+20, text="Offs " + str(offs3), anchor="nw", fill=strCh3Color)
lastKey = ''
root.update()
nMainloops=0
while lastKey!="x":
time.sleep(.03) # 'do some calculation'
nMainloops+=1
# print(str(nMainloops) + " " + str(nKeystrokes)) # show something in the console window
root.update()