我正在为我的串行通信程序创建一个图形界面。 gui的布局设置了我想要的,我已经开始尝试添加一些串行代码。但是,我遇到了一个问题。当我单击“测试连接”按钮或b1时,程序冻结并停止响应。不是下面的代码遗漏了我认为不必要的东西。 我已将问题缩小到read = ser.read(1)。出于某种原因,ser.read()正在杀死它。 感谢您提前提供任何帮助!
class COMS_app(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.platform_check()
self.initialize()
def initialize(self):
self.grid()
self.scrollbar = Tkinter.Scrollbar(self, orient = 'vertical', bd = 5)
self.scrollbar.grid(column = 2, row = 0, rowspan = 2, sticky = 'NSE')
self.com = Tkinter.Text(self, fg = "blue", height = 20, borderwidth = 5, padx = 5, pady = 5, relief = 'ridge')
self.com.grid(column = 0, row =0, columnspan = 2)
self.com.insert('1.0', 'COMS Terminal:')
self.scrollbar.config(command = self.com.yview)
self.inf = Tkinter.Text(self, fg = "blue", height = 8, borderwidth = 5, padx = 5, pady = 5, relief = 'ridge')
self.inf.grid(column = 0, row =7, columnspan = 2)
self.tb2Variable = Tkinter.StringVar()
self.tb2 = Tkinter.Entry(self,textvariable = self.tb2Variable)
self.tb2.grid(column=1,row=2, sticky='W')
self.tb2.bind("<Return>", self.OnPressEnter)
self.tb2Variable.set(u"Enter File Name")
b1 = Tkinter.Button(self, text = u"Test Connection", borderwidth = 5, relief = 'ridge', command = self.OnButtonClick1)
b1.grid(column = 0, row = 1, sticky = 'W')
b2 = Tkinter.Button(self, text = u"Receive File", borderwidth = 5, relief = 'ridge', command = self.OnButtonClick2)
b2.grid(column = 0, row = 2, sticky = 'W')
b3 = Tkinter.Button(self, text = u"Send AT C/D", borderwidth = 5, relief = 'ridge', command = self.OnButtonClick3)
b3.grid(column = 0, row =3, sticky = 'W')
b4 = Tkinter.Button(self, text = u"Send Text", borderwidth = 5, relief = 'ridge', command = self.OnButtonClick4)
b4.grid(column = 0, row = 4, sticky = 'W')
b5 = Tkinter.Button(self, text = u"End Program", borderwidth = 5, relief = 'ridge', command = self.OnButtonClick5)
b5.grid(column = 0, row = 5, sticky = 'W')
b6 = Tkinter.Button(self, text = u"Get Help", borderwidth = 5, relief = 'ridge', command = self.OnButtonClick6)
b6.grid(column = 0, row = 6, sticky = 'W')
self.resizable(False, False)
def OnButtonClick1(self):
self.test_con()
def OnButtonClick2(self):
pass
def OnButtonClick3(self):
pass
def OnButtonClick4(self):
pass
def OnButtonClick5(self):
pass
def OnButtonClick6(self):
pass
def OnPressEnter(self,event):
pass
def test_con(self):
read = ser.read(1)
tran = 'OUTWAY'
rec = 'INWAY'
ser.write(tran)
self.inf.insert(Tkinter.INSERT, '\n<<' + tran)
while not read:
pass
if read == rec:
self.inf.insert(Tkinter.INSERT, '\n<<' +rec)
elif read == FALSE:
self.inf.insert(Tkinter.INSERT, '\n- - NO RESPONSE FROM SERVER')
else:
self.inf.insert(Tkinter.INSERT, '\n- - INVALID RESPONSE FROM SERVER: SERVER:<<' + read)
if __name__ == "__main__":
app = COMS_app(None)
app.title('INS Communications Beta 2.0')
app.mainloop()
答案 0 :(得分:0)
我对pyserial一无所知,但听起来像ser.read(1)
阻塞直到它返回一个字节。如果这是阻塞调用,那将冻结您的GUI。您必须配置串行端口以允许非阻塞调用,或者在单独的线程中执行读取。