Serial Communication With Tkinter
I'm writing some code in Python to make a GUI that controls an electronic board. I put buttons on the GUI and send commands by clicking on it. This part works. But I need to receiv
Solution 1:
Actually I succeeded in doing a script that reads what I send and allow me to write a chain when I push a button from serial import * from Tkinter import *
class serial_test(object):
def __init__(self):
self.serialPort = "COM1"
self.baudRate = 9600
self.ser = Serial(self.serialPort , self.baudRate, timeout=0, writeTimeout=0) #ensure non-blocking
#make a TkInter Window
self.root = Tk()
self.root.wm_title("Reading Serial")
# make a scrollbar
self.scrollbar = Scrollbar(self.root)
self.scrollbar.pack(side=RIGHT, fill=Y)
# make a text box to put the serial output
self.log = Text ( self.root, width=30, height=30, takefocus=0)
self.log.pack()
# attach text box to scrollbar
self.log.config(yscrollcommand=self.scrollbar.set)
self.scrollbar.config(command=self.log.yview)
#make our own buffer
self.buff=0
self.bou=Button(self.root,text='Valid',command=self.writeSerial)
self.bou.pack()
self.root.after(100, self.readSerial)
self.root.protocol("WM_DELETE_WINDOW", self.Intercept)
self.root.mainloop()
def Intercept(self):
try :
self.ser.close()
except:
pass
self.root.destroy()
def writeSerial(self):
self.ser.write("Testing")
def readSerial(self):
while True:
self.c = self.ser.read() # attempt to read a character from Serial
#was anything read?
if len(self.c) == 0:
break
# get the buffer from outside of this function
self.log.insert(END, self.c)
self.buff=self.buff+1
if self.c == '\r':
for i in range(30-self.buff):
self.log.insert(END, ' ')
self.buff=0
if self.buff==30:
self.buff=0
self.root.after(10, self.readSerial) # check serial again soon
serial_test()
Solution 2:
import serial
from tkinter import *
class serial_test(object):
def __init__(self):
self.serialPort = "COM5"
self.baudRate = 9600
self.ser = serial.Serial(self.serialPort , self.baudRate, timeout=0, writeTimeout=0) #ensure non-blocking
#make a TkInter Window
self.root = Tk()
self.root.wm_title("Reading Serial")
# make a scrollbar
self.scrollbar = Scrollbar(self.root)
self.scrollbar.pack(side=RIGHT, fill=Y)
# make a text box to put the serial output
self.log = Text ( self.root, width=30, height=30, takefocus=0)
self.log.pack()
# attach text box to scrollbar
self.log.config(yscrollcommand=self.scrollbar.set)
self.scrollbar.config(command=self.log.yview)
#make our own buffer
self.buff=0
self.bou=Button(self.root,text='Valid',command=self.writeSerial)
self.bou.pack()
self.root.after(100, self.readSerial)
self.root.protocol("WM_DELETE_WINDOW", self.Intercept)
self.root.mainloop()
def Intercept(self):
try :
self.ser.close()
except:
pass
self.root.destroy()
def writeSerial(self):
self.ser.write("Testing")
def readSerial(self):
while True:
self.c = self.ser.read() # attempt to read a character from Serial
#was anything read?
if len(self.c) == 0:
break
# get the buffer from outside of this function
self.log.insert(END, self.c)
self.buff=self.buff+1
if self.c == '\r':
for i in range(30-self.buff):
self.log.insert(END, ' ')
self.buff=0
if self.buff==30:
self.buff=0
self.root.after(10, self.readSerial) # check serial again soon
a=serial_test()
a.readSerial()
Post a Comment for "Serial Communication With Tkinter"