1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
# -- coding: cp1252 --
# script de controle temperature dans le chalet
import time
import tkinter as tk
import serial
import io
COM = 'COM6'
IDN = "Unknow"
def scan():
available = []
for i in range(256):
try:
s = serial.Serial('COM'+str(i))
available.append( (s.portstr))
s.close() # explicit close 'cause of delayed GC in java
except serial.SerialException:
pass
for s in available:
print ("%s" % (s)) #str
print ("Found ports:")
scan()
ser = serial.Serial()
ser.baudrate = 9600
ser.port = COM
ser.parity = serial.PARITY_NONE
ser.stopbits=serial.STOPBITS_ONE
ser.bytesize = serial.EIGHTBITS
ser.timeout=.5
ser.open()
def sortie(): # quitter le prog
ser.close()
app.destroy()
def send_value(value):
print (value)
ser.write(value.encode())
ser.write('\r'.encode())
buffer = ""
#while True:
# oneByte = ser.read(1)
# if oneByte == b"\n": #method should returns bytes
# print (buffer)
# break
# else:
# buffer += oneByte.decode()
retour = ser.readline()
window.insert(tk.INSERT,retour)
print (retour.decode("ascii",errors="ignore"))
idn.config(text=retour.decode("ascii",errors="ignore"))
#app.update
if __name__ == '__main__': #
app = tk.Tk()
app.geometry('400x500+800+300')
app.title('Chalet Serie')
tk.Label(text='Entrer port COM et ->').place(x=18,y=15)
ip_label = tk.Label(text=COM) # l'adresse port COM
ip_label.place(x=250,y=15)
tk.Label(text='Relève').place(x=18,y=45)
tk.Button(text= 'Go',command=lambda:send_value('G') ,fg = "blue").place(x=200,y=40)
idn = tk.Label(text = IDN)
idn.place(x=250,y=40)
tk.Label(text='mode Auto').place(x=18,y=75)
tk.Button(text= 'Go',command=lambda:send_value('A') ,fg = "red").place(x=200,y=70)
tk.Label(text='mode Manuel').place(x=18,y=105)
tk.Button(text= 'Go',command=lambda:send_value('M') ,fg = "red").place(x=200,y=100)
tk.Label(text='Active Chauffage').place(x=18,y=135)
tk.Button(text= 'Go',command=lambda:send_value('H') ,fg = "red").place(x=200,y=130)
tk.Label(text='stoppe chauffage').place(x=18,y=165)
tk.Button(text= 'Go',command=lambda:send_value('C') ,fg = "red").place(x=200,y=160)
scrollbar = tk.Scrollbar(app)
window = tk.Text(app, heigh=10,width=45,yscrollcommand=scrollbar.set)
window.place (x=10,y=220)
scrollbar.place (x=372,y=250)
scrollbar.config(command=window.yview)
app.after(5000,lambda:send_value('G'))
tk.Button(text= 'QUIT',command=sortie).place(x=195,y=440)
app.mainloop() |
Partager