我有2个功能。我的目标是要求用户连接一些预先定义的电缆(根据他们正在运行的具体测试),确认他们已完成此操作,然后要求用户确认他们希望使用哪些 COMPort。
我的问题(位于define_ports()函数中)是我想知道如何根据我之前定义的电缆连接数量动态地为用户吐出输入。如果这很简单,我深表歉意,这已经是漫长的一天了,我的大脑现在无法工作。
代码如下:
ports = list(serial.tools.list_ports.comports())
conec1 = 'USB Captive Cable'
conec2 = 'Serial RS232 Cable'
conec3 = None
cons = [conec1, conec2, conec3]
spar = 0
conec_list = []
for i in cons:
if i != None:
conec_list.append(i)
spar += 1
def Setup():
def test_setup(conec1, conec2, conec3):
"Details what cables need to be connected before test can progress"
print('\n> > For this test you will need to connect the following:\n')
for i in conec_list:
if i != None:
print(i)
while True:
yes = input('> > Please confirm this has been done. y/n ')
if yes != 'y':
continue
else:
break
return conec1, conec2, conec3
def define_ports():
"Asks user to confrim which Port numbers are to be used for each port connection"
test_setup(conec1, conec2, conec3)
print('\n> > The available Port connections are: ')
for p in ports:
print(p)
while True:
print('\n> > Please confirm which Port numbers are to be used for each connection\n')
global prt1
global prt2
global prt3
for i in conec_list:
if i != None:
prt1 = input(f'- Port number for {conec1}: ')
prt2 = input(f'- Port number for {conec2}: ')
prt3 = input(f'- Port number for {conec3}: ')
print('\n> > The Port numbers you have selected are: '
f'{conec1} = {prt1}, '
f'{conec2} = {prt2}'
f'{conec3} = {prt3}')
flash = input('Is this correct? y/n ')
if flash != 'y':
continue
else:
break
prt1 = str('COM'+prt1)
prt2 = str('COM'+prt2)
prt3 = str('COM'+prt3)
define_ports()
Setup()
我目前遇到的问题是获取输入(prt1、prt2、prt3) )根据 conec_list 中的项目数量呈现给用户。我尝试过各种 for 循环,但不确定这是否是最好的尝试方法。
你目前的代码中存在的其中一个问题是,你试图在
for
循环中为每个连接请求输入,但你并没有将连接名称
i
与对应的端口变量
prt1
、
prt2
、
prt3
关联起来。
与其尝试为每个端口使用单独的变量,不如使用字典来存储连接名称及其对应的端口。这将使代码更具动态性和可扩展性。
以下是更新后的代码:
import serial.tools.list_ports
ports = list(serial.tools.list_ports.comports())
conec1 = 'USB Captive Cable'
conec2 = 'Serial RS232 Cable'
conec3 = None
cons = [conec1, conec2, conec3]
spar = 0
conec_list = []
for i in cons:
if i is not None:
conec_list.append(i)
spar += 1
def Setup():
def test_setup(conec_list):
"Details what cables need to be connected before test can progress"
print('\n>>> For this test you will need to connect the following:\n')
for i in conec_list:
print(i)
while True:
yes = input('>>> Please confirm this has been done. y/n ')
if yes != 'y':
continue
else:
break
def define_ports():
"Asks user to confirm which Port numbers are to be used for each port connection"
test_setup(conec_list)
print('\n>>> The available Port connections are: ')
for p in ports:
print(p)
port_numbers = {}
while True:
print('\n>>> Please confirm which Port numbers are to be used for each connection\n')
for connection in conec_list:
port_num = input(f'- Port number for {connection}: ')
port_numbers[connection] = f'COM{port_num}'
print('\n>>> The Port numbers you have selected are: ')
for connection, port_num in port_numbers.items():
print(f'{connection} = {port_num}')
flash = input('Is this correct? y/n ')
if flash != 'y':
continue
else:
break
return port_numbers
port_numbers = define_ports()
# 现在你可以使用 port_numbers 字典来访问每个连接的端口号
# 例如:
# print(port_numbers['USB Captive Cable']) 将打印 USB Captive Cable 的端口号
Setup()
代码解释:
- 使用字典存储端口号:
-
我们创建了一个空字典
port_numbers
来存储连接名称及其对应的端口号。 -
迭代
conec_list
: -
在
define_ports
函数中,我们迭代conec_list
中的每个连接,并提示用户输入相应的端口号。 -
然后,我们将连接名称和端口号存储在
port_numbers
字典中。 -
打印选择的端口号:
-
我们迭代
port_numbers
字典并打印每个连接及其选择的端口号。 -
返回
port_numbers
: -
define_ports
函数现在返回port_numbers
字典,以便可以在Setup
函数中使用它来访问每个连接的端口号。
现在,代码将根据
conec_list
中的连接数动态地提示用户输入端口号。