| 12
 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
 
 | #!/usr/bin/env python3
from tkinter import *
from serial import *
from tkinter import filedialog
import subprocess
import os
import time
fenetre = Tk()
def espion():
        fe=Toplevel(master=fenetre)
      # # fe.attributes('-fullscreen', 1)
        serialPort = "/dev/ttyUSB0"
        baudRate = 9600
        ser = Serial(serialPort, baudRate,bytesize=SEVENBITS, parity=PARITY_EVEN, stopbits=STOPBITS_ONE, timeout=0, writeTimeout=0, xonxoff= False)
        Frame16 = Frame(fe, borderwidth=2, relief=GROOVE)
        Frame16.pack(side=LEFT, fill=Y)
       # make a scrollbar
        scrollbar = Scrollbar(Frame16)
        scrollbar.pack(side=RIGHT, fill=Y)
       # make a text box to put the serial output
        log = Text ( Frame16, width=30, height=30, takefocus=0)
        log.pack()
       #  attach text box to scrollbar
        log.config(yscrollcommand=scrollbar.set)
        scrollbar.config(command=log.yview)
       #make our own buffer
       #useful for parsing commands
       #Serial.readline seems unreliable at times too
        serBuffer = ""
        def readSerial():
            while True:
                c = ser.read() # attempt to read a character from Serial
        #was anything read?
                if len(c) == 0:
                    break
        # get the buffer from outside of this function
                global serBuffer
        # check if character is a delimeter
                if c == '0D':
                    c = '\r' # don't want returns. chuck it
 
                if c == '02':
                    serBuffer += "\n" # add the newline to the buffer
            #add the line to the TOP of the log
                    log.insert('0.0', serBuffer)
                    serBuffer = "" # empty the buffer
                else:
                    serBuffer += c # add to the buffer
            fe.after(10, readSerial) # check serial again soon
# after initializing serial, an arduino may need a bit of time to reset
        fe.after(100, readSerial)
 
        fe.mainloop()
bouton= Button (fenetre, text='espion', command=espion, height=10, widht=11)
bouton.pack(side=BOTTOM)
fenetre.mainloop() | 
Partager