Pyserial和tkinter不工作了?

2024-10-02 10:23:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在做一个需要串行通信和图形用户界面的项目。我是python新手,所以我真的被卡住了,找不到关于这个的任何信息,所以我想这是一个逻辑问题,但我无法解决它。任何学科的帮助都是非常感谢的!在

代码:

from tkinter import *
import serial, time, os

codeRead = open ('/home/riverans/Desktop/com/acticode.taws', 'r')
code = codeRead.read()
codeRead.close()

ser = serial.Serial('/dev/ttyACM0', 9600)

while 1:
    serialInput = ser.readline()

    if serialInput == b'1001\r\n':      #Read Input 1001
        file1 = open('/home/riverans/Desktop/com/I1.taws', 'w')
        file1.write(code)
        file1.close()

    if serialInput == b'1002\r\n':      #Read Input 1002
        if os.path.isfile('/home/riverans/Desktop/com/I1.taws'):
            os.remove('/home/riverans/Desktop/com/I1.taws')

    if serialInput == b'2001\r\n':      #Read Input 2001
        file2 = open('/home/riverans/Desktop/com/I2.taws', 'w')
        file2.write(code)
        file2.close()

    if serialInput == b'2002\r\n':      #Read Input 2002
        if os.path.isfile('/home/riverans/Desktop/com/I2.taws'):
            os.remove('/home/riverans/Desktop/com/I2.taws')

    if serialInput == b'3001\r\n':      #Read Input 3001
        file3 = open('/home/riverans/Desktop/com/I3.taws', 'w')
        file3.write(code);
        file3.close()

    if serialInput == b'3002\r\n':      #Read Input 3002
        if os.path.isfile('/home/riverans/Desktop/com/I3.taws'):
            os.remove('/home/riverans/Desktop/com/I3.taws')

    if os.path.isfile('/home/riverans/Desktop/com/I1.taws'):        #Write Pin 10
        ser.write(b'1')

    if os.path.isfile('/home/riverans/Desktop/com/I2.taws'):        #Write Pin 8
        ser.write(b'2')

    if os.path.isfile('/home/riverans/Desktop/com/I3.taws'):
        ser.write(b'3')

    print (serialInput)


class Window(Frame):
    def __init__(self, master = None):
        Frame.__init__(self, master)
        self.master = master
        self.init_window()

    def init_window(self):
        self.master.title("MISSYS")
        self.pack(fill=BOTH, expand=1)

        menu = Menu(self.master)
        self.master.config(menu=menu)

        file = Menu(menu)
        file.add_command(label='Exit', command=self.client_exit)
        menu.add_cascade(label='File', menu=file)

        edit = Menu(menu)
        edit.add_command(label='Undo')
        menu.add_cascade(label='Edit', menu=edit)


        button1 = Button(self, text="Turn on LED", command=self.serial_write_led)
        button1.place(x=0, y=0)

        button2 = Button(self, text="Turn off LED", command=self.serial_write_led2)
        button2.place(x=100, y=0)

    def client_exit(self):
        exit()

    def serial_write_led(self):
        ser.write(b'1')
        print("Turning on LED")

    def serial_write_led2(self):
        ser.write(b'2')
        print("Turning off LED")

root = Tk()
root.geometry("500x300")

app = Window(root)

root.mainloop()

Tags: selfmastercomhomereadifosserial

热门问题