[PYTHON] Just check serial communication with tk

# -*- coding: utf-8 -*-
import tkinter as tk
import serial as sl
import time
import datetime as dt


class App(tk.Frame):
    finFlag = True
    serial = None
    def __init__(self, master=None):
        super().__init__(master)
        self.master.protocol('WM_DELETE_WINDOW', self.On_Close) 
        #Here, decide the serial communication setting arbitrarily
        self.serial = sl.Serial('COM7', 19200, timeout=0)
        while( self.serial.is_open is False):
            time.sleep(100)
            
        self.createPage()
        
        self.after(10, self.readData)
        
    def createPage(self):
        self.page1 = tk.Frame(self.master)
        self.page1.pack(pady=5, padx=30)
        
        self.timelabel = tk.Label(self.page1)
        self.timelabel.grid(column=0, row=0)
        self.timelabel['text'] = dt.datetime.now().strftime("%Y/%m/%d %H:%M:%S")
        
        self.label = tk.Label(self.page1, font=("",20), relief='ridge')
        self.label.grid(column=1, row=0)
        self.label['text'] = '00.0A'
        
           
    def readData(self):
        line = None
        self.timelabel['text'] = dt.datetime.now().strftime("%Y/%m/%d %H:%M:%S")
        if ( self.serial.is_open):
            line = self.serial.readline()
            line = line.strip().decode('utf-8')

            
        if( (line is not None) and (line != '')):

            print(line)
            
            self.label['text'] = line
            
        if ( self.finFlag ):
            self.after(10, self.readData)
        
    def On_Close(self):
        self.finFlag = False
        self.serial.close()
        self.master.destroy()

def main():
    root = tk.Tk()
    app = App(root)
    app.mainloop()

if __name__ == '__main__':
    main()

Recommended Posts

Just check serial communication with tk
Serial communication with Python
Serial communication with python
Serial communication with Raspberry Pi + PySerial
Check uwsgi's uwsgi protocol communication with uwsgi-tools
Socket communication with Python
HTTP communication with Python
Domain check with Python
Check version with python
Serial communication control with python and I2C communication (using USBGPIO8 device)
Serial communication control with python and SPI communication (using USBGPIO8 device)
CNN implementation with just numpy
Check python coverage with pytest-cov
Use chainer with Jetson TK1
Introduction to serial communication [Python]
View Python communication with Fiddler
Check the code with flake8