Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.7 kB
1
Indexable
Never
from pymodbus.client.sync import ModbusTcpClient
from matplotlib.animation import FuncAnimation
from matplotlib import pyplot as plt
import matplotlib.dates as mdates
import csv
import time
from datetime import datetime
import tkinter as tk 

csvfile= "temp_145.csv"
#connect to modnet-----------------------------------------------------------------------------------
client = ModbusTcpClient('127.0.0.1',502)
print(client.connect())

#--Reading data---------------------------------------------------------------------------------------

while client.connect() is True :
    client.read_holding_registers(0,3)
    result=client.read_holding_registers(0,3)
  
    #reg1.registers[0]
    #print("Reg1 :" + str(result.registers[0]))
    #print("Reg2 :" + str(result.registers[1]))
    #print("Reg3 :" + str(result.registers[2]))
    timeC= time.strftime("%I")+':' +time.strftime("%M")+':'+time.strftime("%S")
    temprature = (result.registers[0])
     #print(temprature)
    data=[temprature, timeC]
    with open (csvfile,"a") as output:
        writer = csv.writer(output, delimiter=",",lineterminator='\n')
        writer.writerow(data)
        time.sleep(10)
 #-----------Plot---------------

    x_vals=[]
    y_vals=[]
fig = Figure(figsize=(5, 4), dpi=100)

def animate(i):
    
    x_vals.append(temprature)
    y_vals.append(timeC)
    plt.cla()
    plt.plot(x_vals,y_vals)
    # ani = animation.FuncAnimation(plt.gcf(),animate, interval = 6000)   
  
    
def plot ():
    canvas = FigureCanvasTkAgg(fig, master=window)  # A tk.DrawingArea.
    canvas.draw()
    
    
window = Tk()

# setting the title
window.title('Plotting in Tkinter')
Print ('hello')
# dimensions of the main window
window.geometry("8000x8000")
window.config(bg='light blue')
ani= FuncAnimation(fig,data,animate, 1000)
g=plot()

plt.show()  
window.mainloop()

# yahi rehna hai-----------------

#-----------Plot---------------

    x_vals=[]
    y_vals=[]
fig = Figure(figsize=(5, 4), dpi=100)

def animate(i):
    
    x_vals.append(temprature)
    y_vals.append(timeC)
    plt.cla()
    plt.plot(x_vals,y_vals)
    # ani = animation.FuncAnimation(plt.gcf(),animate, interval = 6000)   
  
    
def plot ():
    canvas = FigureCanvasTkAgg(fig, master=window)  # A tk.DrawingArea.
    canvas.draw()
    
    
window = Tk()

# setting the title
window.title('Plotting in Tkinter')
Print ('hello')
# dimensions of the main window
window.geometry("8000x8000")
window.config(bg='light blue')
ani= FuncAnimation(fig,data,animate, 1000)
g=plot()

plt.show()  
window.mainloop()