Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
5.0 kB
2
Indexable
Never
from tkinter import *
from tkinter import ttk
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 
from openpyxl import Workbook
from openpyxl import load_workbook
# plot function is created for
# plotting the graph in
# tkinter window

# Create a Workbook
wb = Workbook()
ws =  wb.active
ws.title = "temp_values_3"
wb.save(filename = 'temp_values_3.xlsx')
wb = openpyxl.load_workbook('temp_values_3.xlsx') 
print(wb.sheetnames)
# Get workbook active sheet 
# from the active attribute. 
sheet = wb.active


x_1=[]
x_2=[]
x_3=[]
x_4=[]
x_5=[]
x_6=[]
x_7=[]
x_8=[]

y_1=[]
y_2=[]
y_3=[]
y_4=[]
y_5=[]
y_6=[]
y_7=[]
y_8=[]

j=0
row=sheet.max_row
column=1
client = ModbusTcpClient('192.168.127.254',502)
print(client.connect())
reg_1= 2049
reg_2= 2051
reg_3= 2053
reg_4= 2055
reg_5= 2057
reg_6= 2059
reg_7= 2061
reg_8= 2063
fig= Figure(figsize = (10,10),
				dpi = 100)

plot1 = fig.add_subplot(3,3,1 , facecolor='orange')
plot1.set_title('Channel 1', color='green')

plot2 = fig.add_subplot(3,3,2)
plot2.set_title('Channel 2')

plot3 = fig.add_subplot(3,3,3)
plot3.set_title('Channel 3')

plot4 = fig.add_subplot(3,3,4)
plot4.set_title('Channel 4')

plot5 = fig.add_subplot(3,3,5)
plot5.set_title('Channel 5')

plot6 = fig.add_subplot(3,3,6)
plot6.set_title('Channel 6')

plot7 = fig.add_subplot(3,3,7)
plot7.set_title('Channel 7')

plot8 = fig.add_subplot(3,3,8)
plot8.set_title('Channel 8')


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)
        y.append(datetime.now())

        Reg_2=client.read_input_registers(reg_2)
        x_2.append(Reg_2.registers)
        y_2.append(datetime.now())

        Reg_3=client.read_input_registers(reg_3)
        x_3.append(Reg_3.registers)
        y_3.append(datetime.now())
        
        Reg_4=client.read_input_registers(reg_4)
        x_4.append(Reg_4.registers)
        y_4.append(datetime.now())
        
        Reg_5=client.read_input_registers(reg_5)
        x_5.append(Reg_5.registers)
        y_5.append(datetime.now())

        Reg_6=client.read_input_registers(reg_6)
        x_6.append(Reg_6.registers)
        y_6.append(datetime.now())

        Reg_7=client.read_input_registers(reg_7)
        x_7.append(Reg_7.registers)
        y_7.append(datetime.now())

        Reg_8=client.read_input_registers(reg_8)
        x_8.append(Reg_8.registers)
        y_8.append(datetime.now())




        
        plot1.cla()
        plot1.plot(y,x_1, color= 'green')
        plot1.align_xlabelsylabel('Time')
        plot1.align_ylabelsylabel('Temperature')
        
        plot2.cla()
        plot2.plot(y_2,x_2)
        
        plot3.cla()
        plot3.plot(y_3,x_3
                   )
        plot4.cla()
        plot4.plot(y_4,x_4)
        
        plot5.cla()
        plot5.plot(y_5,x_5)
        
        plot6.cla()
        plot6.plot(y_6,x_6)

        plot7.cla()
        plot7.plot(y_7,x_7)

        plot8.cla()
        plot8.plot(y_8,x_8)
        
        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]
        sheet.cell(row, 4).value=x_3[j][0]
        sheet.cell(row, 5).value=x_4[j][0]
        sheet.cell(row, 6).value=x_5[j][0]
        sheet.cell(row, 7).value=x_6[j][0]
        sheet.cell(row, 8).value=x_7[j][0]
        sheet.cell(row, 9).value=x_8[j][0]
        
        row+=1
        j+=1
        wb.save('temp_values_3.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("8000x8000")
window.config(bg='light blue')
w = Button (window,text = "Start" ,bg='green', command= start)
v = Button (window,text = "Stop",bg='red', 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()