Untitled
unknown
plain_text
3 years ago
2.6 kB
1
Indexable
Never
from tkinter import * from pymodbus.client.sync import ModbusTcpClient from matplotlib.figure import Figure from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk) from datetime import datetime from matplotlib import pyplot as plt from matplotlib.animation import FuncAnimation import openpyxl # plot function is created for # plotting the graph in # tkinter window wb = openpyxl.load_workbook('temp_values.xlsx') # Get workbook active sheet # from the active attribute. sheet = wb.active #x_3=[] x_1=[] x_2=[] y=[] y_2=[] j=0 row=sheet.max_row column=1 client = ModbusTcpClient('192.168.127.254',502) print(client.connect()) reg_1= 2053 reg_2= 2049 reg_3= 2051 reg_4= 2055 reg_5= 2057 reg_6= 2059 reg_7= 2061 reg_8= 2063 fig= Figure(figsize = (8,8), dpi = 100) plot1 = fig.add_subplot(1,2,1) plot2 = fig.add_subplot(1,2,2) run = False def start(): global run run= True def stop(): global run run= False def data(i): global row,column,j if run: Reg_1=client.read_input_registers(reg_1) x_1.append(Reg_1.registers) Reg_2=client.read_input_registers(reg_2) x_2.append(Reg_2.registers) y.append(datetime.now()) y_2.append(datetime.now()) plot1.cla() plot1.plot(y,x_1) plot2.cla() plot2.plot(y_2,x_2) sheet.cell(row, 1).value=y[j] sheet.cell(row, 2).value=x_1[j][0] sheet.cell(row, 3).value=x_2[j][0] row+=1 j+=1 wb.save('temp_values.xlsx') def plot(): canvas = FigureCanvasTkAgg(fig,master = window) canvas.draw() # placing the canvas on the Tkinter window canvas.get_tk_widget().pack() # creating the Matplotlib toolbar toolbar = NavigationToolbar2Tk(canvas,window) toolbar.update() # placing the toolbar on the Tkinter window canvas.get_tk_widget().pack() # the main Tkinter window window = Tk() # setting the title window.title('Plotting in Tkinter') # dimensions of the main window window.geometry("1000x1000") w = Button (window,text = "Start" ,command= start) v = Button (window,text = "Stop", command = stop ) # button that displays the plot v.pack() w.pack() g=plot() ani= FuncAnimation(fig,data,1000) #ani.save(r"C:\Users\MES03\Desktop\vinayak") # save the file #wb.save('sample_3.xlsx') # run the gui window.mainloop()