问题:关闭端口后,python 仍然读取来自 arduino 的串行数据。串口无法关闭

Posted

技术标签:

【中文标题】问题:关闭端口后,python 仍然读取来自 arduino 的串行数据。串口无法关闭【英文标题】:Problem: The python still reads the serial data coming from arduino after closing its port. Serial port cannot be closed 【发布时间】:2018-09-15 20:07:32 【问题描述】:

我是 tkinter 的新手,使用 python 3.4。我正在使用***窗口开发 GUI,并从 Arduino 获取串行数据。这里唯一的问题是,每当从“class PETwithSC”中单击“home”按钮时,python 仍然会读取 StartPage 类中的数据并关闭 GUI。单击主页按钮后,我需要关闭串行端口。请帮忙。谢谢!

这是代码

    from tkinter import *
    import serial
    import time

    class StartPage() 
        def __init__(self,master)
             self.master = master
             self.master attributes('-fullscreen','true') 

             self.button = Button(self.master, command=self.pageOne)
             self.button.pack() 

        def pageOne(self)
             self.master.withdraw()
             self.pick = Toplevel(self.master)
             self.game = PETwithSC(sef.pick)

    class PETwithSC()
        def __init__(self,master)
             self.master = master
             self.master attributes('-fullscreen','true')

             ###### Home button
             self.button = Button(self.master, command=self.home)
             self.button.pack()

             self.data = StringVar()
             self.label = Label(self.master, textvariable = self.data)
             self.label.pack()
             self.master.after(1000,self.serial)

        def serial(self)
             self.ser = serial.Serial("/dev/ttyACM0",9600,timeout=0.1)
             self.ser.write(b'1') ##commanding the arduino to read the smartcard info
             self.info = self.ser.readline()
             self.data.set(self.info)
             self.master.after(1000,self.serial)
             print (A)

        def home(self)
             self.ser.close()
             self.master.withdraw()
             self.pick = Toplevel(self.master)
             self.game = StartPage(sef.pick)

    if __name__ == "__main__":
        app = Tk()
        myGUI = StartPage(app)
        app.mainloop

【问题讨论】:

【参考方案1】:

因为self.serial()在循环运行:self.master.after(1000, self.serial),而且每次都打开串口:self.ser = serial.Serial("/dev/ttyACM0",9600,timeout=0.1),只是关闭了串口,self.master.withdraw()不会停止下一次运行self.serial从再次打开它。尝试在每次使用后关闭端口:

def serial(self)
         self.ser = serial.Serial("/dev/ttyACM0",9600,timeout=0.1)
         self.ser.write(b'1') ##commanding the arduino to read the smartcard info
         self.info = self.ser.readline()
         self.ser.close()                   #closing here
         self.data.set(self.info)
         self.master.after(1000,self.serial)
         print (A)

并从self.home() 中删除它:

def home(self)
         self.master.withdraw()            #rather than here
         self.pick = Toplevel(self.master)
         self.game = StartPage(sef.pick)

另外,错字:

self.game = PETwithSC(sef.pick)

应该是:

self.game = PETwithSC(self.pick)

希望对你有帮助

【讨论】:

以上是关于问题:关闭端口后,python 仍然读取来自 arduino 的串行数据。串口无法关闭的主要内容,如果未能解决你的问题,请参考以下文章

查尔斯没有显示表格“连接来自”

如何解决python socket server重启后端口被占用的问题

即使在端口转发和禁用防火墙之后,端口 80 仍然被阻止

Python套接字服务器读取客户端的多命令[关闭]

Python如何读取xlsx文件并转换为csv而不写入目录[关闭]

输出流OutputStream简单理解