我有一个带有所有串行端口列表的复选框,用户从组合框中选择一个,以便以后与arduino接口
以下是有趣的部分:
# --- functions ---
def serial_ports():
return serial.tools.list_ports.comports()
def on_select(event=None):
# get selection from event
print("event.widget:", event.widget.get())
# or get selection directly from combobox
print("comboboxes: ", cb.get())
# --- functions ---
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')
# assign function to combobox
cb.bind('<<ComboboxSelected>>', on_select)
cb.bind()
获取对on_select()
的选择。但是,当我使用on_select()
函数打印用户选择的数据时,我得到:
event.widget: COM14 - Arduino Mega 2560 (COM14)
我只想得到COM端口,例如“COM14
”
这样就可以在变量中赋值,然后像这样使用:
ser = serial.Serial('COM14', baudRate, timeout=0, writeTimeout=0)
其中,我将使用变量而不是“COM14
”
pySerial是否有一种方法,只获取COM端口,而不提供完整的描述
serial.tools.list_ports.comports()
的结果是serial.tools.list_ports.ListPortInfo
实例的列表因此,如果您只想要端口名,请更改
到
相关问题 更多 >
编程相关推荐