发送串行数据时程序卡住

时间:2021-04-05 21:08:24

标签: python-3.x tkinter pyserial

我正在开发一个小型应用程序,用于与我的 arduino 交互。

但是,为了让 arduino 开始发送串行数据,我必须使用特定的字符串来触发它。

当我按下将发送字符串并启动串行读取的按钮时,程序卡住了。 终端上没有显示错误。

相关代码如下:

    #setting the serial object
    def on_select(event=None):
        global ser
        COMPort = cb.get()
        ser = serial.Serial(port = COMPort, baudrate=9600)
    
   # read serial data
    def readSerial():
        ser_bytes = ser.readline()
        ser_bytes = ser_bytes.decode("utf-8")
        text.insert("end", ser_bytes)
        after_id=root.after(100,readSerial)
    
    # this function is triggered, when button 'Measure all Sensors' is pressed, on frame 2
    def measure_all():    
       global stop
       stop_ = False
       ser.write(str.encode('rf')) #Send string 'rf to arduino', which means Measure all Sensors
       readSerial() #Start Reading data

前两个函数起作用,因为我能够读取串行数据,当 arduino 只是吐出数据时,不需要通过发送文本来激活。

这是第三个使程序卡住的函数。

编辑:这是更多的代码 - 更大的图景。

import tkinter as tk
import tkinter.ttk as ttk
import serial.tools.list_ports #for a list of all the COM ports
from tkinter import scrolledtext

#to be used on our canvas
HEIGHT = 800
WIDTH = 800

#hardcoded baud rate
baudRate = 9600

# this is the global variable that will hold the serial object value
ser = None #initial  value. will change at 'on_select()'

# --- functions ---

#the following two functtions are for the seria port selection, on frame 1

#this function populates the combobox on frame1, with all the serial ports of the system
def serial_ports():    
    return serial.tools.list_ports.comports()


#when the user selects one serial port from the combobox, this function will execute
def on_select(event=None):
    global ser
    COMPort = cb.get()
    string_separator = "-"
    COMPort = COMPort.split(string_separator, 1)[0] #remove everything after '-' character
    COMPort = COMPort[:-1] #remove last character of the string (which is a space)
    ser = serial.Serial(port = COMPort, baudrate=9600)

def readSerial():
    ser_bytes = ser.readline()
    ser_bytes = ser_bytes.decode("utf-8")
    text.insert("end", ser_bytes)
    if vsb.get()[1]==1.0: #if the scrollbar is down to the bottom, then autoscroll
       text.see("end")
    after_id=root.after(100,readSerial)



# this function is triggered, when button 'Measure all Sensors' is pressed, on frame 2
def measure_all():    
   global stop_
   stop_ = False
   button_stop['state']='normal' #make the 'Stop Measurement' button accessible
   ser.write("rf".encode()) #Send string 'rf to arduino', which means Measure all Sensors
   readSerial() #Start Reading data


# --- main ---
root = tk.Tk() #here we create our tkinter window
root.title("Sensor Interface")

#we use canvas as a placeholder, to get our initial screen size (we have defined HEIGHT and WIDTH)
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()

# --- frame 1 ---
frame1 = tk.Frame(root)
frame1.place(relx=0, rely=0.05, relheight=0.03, relwidth=1, anchor='nw') #we use relheight and relwidth to fill whatever the parent is - in this case- root

label0 = tk.Label(frame1, text="Select the COM port that the device is plugged in: ")
label0.config(font=("TkDefaultFont", 8))
label0.place(relx = 0.1, rely=0.3, relwidth=0.3, relheight=0.5)


cb = ttk.Combobox(frame1, values=serial_ports())
cb.place(relx=0.5, rely=0.5, anchor='center')
cb.bind('<<ComboboxSelected>>', on_select)
# --- frame 1 ---

# --- frame 2 ---
frame2 = tk.Frame(root, bd=5) #REMOVED THIS bg='#80c1ff' (i used it to see the borders of the frame)
frame2.place(relx=0, rely=0.1, relheight=0.07, relwidth=1, anchor='nw')

#Button for 'Measure All Sensors'
#it will be enabled initially
button_all = tk.Button(frame2, text="Measure all Sensors", bg='#80c1ff', fg='red', state='normal', command=measure_all)  #bg='gray'
button_all.place(relx=0.2, rely=0.5, anchor='center')


#frame 6 will be the area with the texct field
# --- frame 6 ---
frame6 = tk.Frame(root, bg='#80c1ff') #remove color later
frame6.place(relx=0.0, rely=0.4, relheight=1, relwidth=1, anchor='nw')

text_frame=tk.Frame(frame6)
text_frame.place(relx=0, rely=0, relheight=0.6, relwidth=1, anchor='nw')
text=tk.Text(text_frame)
text.place(relx=0, rely=0, relheight=1, relwidth=1, anchor='nw')
vsb=tk.Scrollbar(text_frame)
vsb.pack(side='right',fill='y')
text.config(yscrollcommand=vsb.set)
vsb.config(command=text.yview)
# --- frame 6 ---


stop_=True # Stop Flag. True when no measuring is happening

root.mainloop() #here we run our app
# --- main ---

0 个答案:

没有答案