from tkinter import * from R2Graph import * from math import * import socket import select import struct import time import threading SAMPLES = 400 TIMEINTERVAL = 0.01 PACKET_DURATION = SAMPLES*TIMEINTERVAL FRAMELEN = 8 + SAMPLES*2 MCAST_GRP = '224.1.1.22' MCAST_PORT = 1234 IS_ALL_GROUPS = True PACKETS_IN_LINE = 3 margin = 4 height = 600 - 2*margin width = 1000 - 2*margin animate = True finish = False rootWindow = Tk() rootWindow.title("Triangle") rootWindow.geometry("1000x600") drawArea = Canvas( rootWindow, width=1000-8*2, height=600-8*2-24, bg="white" ) drawArea.place(anchor="nw", x=8, y=8+24) def onClear(): drawArea.delete("all") clearButton = Button( text="Clear", width=60, height=20, command=onClear ) clearButton.place( anchor="nw", x=8, y=8, width=60, height=20 ) def onAnimate(): global animate global animateButton animate = not animate if animate: animateButton.configure(text="Pause") else: animateButton.configure(text="Start") animateButton = Button( text="Pause", width=60, height=20, command=onAnimate ) animateButton.place( anchor="nw", x=8 + 60 + 8, y=8, width=60, height=20 ) def endProgram(): global finish finish = True rootWindow.destroy() quitButton = Button( text="Quit", width=60, height=20, command=endProgram ) quitButton.place( anchor="nw", x=8 + 60*2 + 8*2, y=8, width=60, height=20 ) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) if IS_ALL_GROUPS: # on this port, receives ALL multicast groups sock.bind(('', MCAST_PORT)) else: # on this port, listen ONLY to MCAST_GRP sock.bind((MCAST_GRP, MCAST_PORT)) mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY) sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) sock.settimeout(10.) ### cv = threading.Condition() def receiving(): global animate global finish xshift = 0 try: frame = [ 0 for i in range(SAMPLES) ] t0 = 0 while not finish: ready = select.select([sock], [], [], 0.05) if len(ready[0]) == 0: continue try: b = sock.recv(1024) except socket.timeout: print("timeout on recv") continue except socket.error: print("socket error") continue if len(b) <= 0: continue # compute checksum checksum = ((b[0] + b[1]*256) & 65535) checksum = ((checksum*3 + (b[2] + b[3]*256)) & 65535) checksum = ((checksum*3 + (b[4] + b[5]*256)) & 65535) checksum = ((checksum*3 + (b[6] + b[7]*256)) & 65535) # print("Frame: ", int(b[0]), "len =", len(b)) t = 0 i = 7 while i >= 0: t = t*256 + int(b[i]) i -= 1 dt = t - t0 print("t =", dt) t0 = t for i in range(SAMPLES): j = 8 + 2*i v = int(b[j]) + 256*int(b[j+1]) frame[i] = v checksum = ((checksum*3 + v) & 65535) ck = b[8 + 2*SAMPLES] + b[8 + 2*SAMPLES + 1]*256 if checksum != ck: print("Corrupted package") xshift = PACKETS_IN_LINE - 1 continue # Statistics if xshift == 0: s1 = 0. s2 = 0. for i in range(SAMPLES): s1 += float(frame[i]) s2 += float(frame[i])**2 mean = s1/SAMPLES d = s2/SAMPLES - mean**2 sd = sqrt(d) ymin = mean - 4.*sd ymax = mean + 4.*sd xmin = 0 xmax = SAMPLES ycoeff = height / (ymax - ymin) xcoeff = (width/PACKETS_IN_LINE) / (xmax - xmin) if animate: if dt > PACKET_DURATION*1500: xshift = 0 else: xshift += 1 xshift %= PACKETS_IN_LINE if xshift == 0: drawArea.delete("all") x0 = margin + xshift*(width/PACKETS_IN_LINE) # Mean value y = margin + (ymax - mean)*ycoeff drawArea.create_line( (margin, y), (margin + width, y), fill="blue", width=1 ) y = margin + (ymax - (mean + sd))*ycoeff drawArea.create_line( (margin, y), (margin + width, y), fill="darkGray", width=1 ) y = margin + (ymax - (mean + sd*2))*ycoeff drawArea.create_line( (margin, y), (margin + width, y), fill="darkGray", width=1 ) y = margin + (ymax - (mean - sd))*ycoeff drawArea.create_line( (margin, y), (margin + width, y), fill="darkGray", width=1 ) y = margin + (ymax - (mean - 2*sd))*ycoeff drawArea.create_line( (margin, y), (margin + width, y), fill="darkGray", width=1 ) points = [] for i in range(SAMPLES): x = x0 + i*xcoeff y = margin + (ymax - frame[i])*ycoeff points.append((x, y)) drawArea.create_line( points, fill="red", width=3 ) except RuntimeError: pass receivingThread = threading.Thread(target=receiving) receivingThread.start() def closeHandler(): global finish finish = True rootWindow.destroy() rootWindow.protocol("WM_DELETE_WINDOW", closeHandler) rootWindow.mainloop()