gpt4 book ai didi

python - 我怎样才能添加一个for循环来减少这段代码?

转载 作者:太空宇宙 更新时间:2023-11-04 11:12:26 25 4
gpt4 key购买 nike

此代码用于设置系统(我向系统发送命令以使其闪烁,但首先我需要通过逐个命令发送命令来设置环境,并读取系统的输出以查看我发送的命令是否解释得很好)。我需要优化它。

优化此代码的最优雅方法是什么?我正在考虑添加一个包含所有命令的数组,然后将它们放入 for 循环中!!请分享您的想法!

try:
ser = serial.Serial("COM32", baudrate=115200, timeout=0, stopbits=serial.STOPBITS_ONE)
if ser.isOpen():
print(ser.name + ' is open...')
else:
print(ser.name + ' is closed...')

if command1 == 'exit' or command2 == 'exit' or command3 == 'exit' or command4 == 'exit' or command5 == 'exit' or command6 == 'exit' :

ser.close()
exit()
else:

l2 = []
for c in command1: # in Python, a string is just a sequence, so we can iterate over it! "setenv qnx_ifs_name 'ifs-rcar_h3.bin';"
l2.append(ord(c))
l2.append(13)
l2.append(10)
print(l2)
l2_byte = bytearray(l2)
print(l2_byte)
ser.write(l2_byte)
out = ser.read(1000000000)
print(out)
time.sleep(1)

l2 = []
for c in command2: # in Python, a string is just a sequence, so we can iterate over it!
l2.append(ord(c))
l2.append(13)
l2.append(10)
print(l2)
ser.write(l2)
out = ser.read(1000000000)
print(out)
time.sleep(1)

l2 = []
for c in command3: # in Python, a string is just a sequence, so we can iterate over it!
l2.append(ord(c))
l2.append(13)
l2.append(10)
l2.append(10)
l2.append(10)
print(l2)
ser.write(l2)
out = ser.read(1000000000)
print(out)
time.sleep(5)

l2 = []
for c in command4: # in Python, a string is just a sequence, so we can iterate over it!
l2.append(ord(c))
l2.append(13)
l2.append(10)
print(l2)
ser.write(l2)
out = ser.read(1000000000)
print(out)
time.sleep(1)
#
l2 = []
for c in command5: # in Python, a string is just a sequence, so we can iterate over it!
l2.append(ord(c))
l2.append(13)
l2.append(10)
l2.append(10)
l2.append(10)
print(l2)
ser.write(l2)
out = ser.read(1000000000)
print(out)
time.sleep(8)
#

l2 = []
for c in command6: # in Python, a string is just a sequence, so we can iterate over it!
l2.append(ord(c))
l2.append(13)
l2.append(10)
print(l2)
ser.write(l2)
out = ser.read(1000000000)
print(out)
time.sleep(10)
#
l2 = []
for c in command7: # in Python, a string is just a sequence, so we can iterate over it!
l2.append(ord(c))
l2.append(13)
l2.append(10)
l2.append(10)
l2.append(10)
print(l2)
ser.write(l2)
out = ser.read(1000000000)
print(out)
time.sleep(10)
#
l2 = []
for c in command8: # in Python, a string is just a sequence, so we can iterate over it!
l2.append(ord(c))
l2.append(13)
l2.append(10)
l2.append(10)
l2.append(10)
print(l2)
ser.write(l2)
out = ser.read(1000000000)
print(out)
time.sleep(10)

l2 = []
for c in command9: # in Python, a string is just a sequence, so we can iterate over it!
l2.append(ord(c))
l2.append(13)
l2.append(10)
print(l2)
ser.write(l2)
out = ser.read(1000000000)
print(out)
time.sleep(1)

l2 = []
for c in command10: # in Python, a string is just a sequence, so we can iterate over it!
l2.append(ord(c))
l2.append(13)
l2.append(10)
print(l2)
ser.write(l2)
out = ser.read(1000000000)
print(out)
time.sleep(1)


l2 = []
for c in command11: # in Python, a string is just a sequence, so we can iterate over it!
l2.append(ord(c))
l2.append(13)
l2.append(10)
print(l2)
ser.write(l2)
out = ser.read(1000000000)
print(out)
time.sleep(1)

l2 = []
for c in command12: # in Python, a string is just a sequence, so we can iterate over it!
l2.append(ord(c))
l2.append(13)
l2.append(10)
print(l2)
ser.write(l2)
out = ser.read(1000000000)
print(out)
time.sleep(1)


l2 = []
for c in command13: # in Python, a string is just a sequence, so we can iterate over it!
l2.append(ord(c))
l2.append(13)
l2.append(10)
l2.append(10)
l2.append(10)
print(l2)
ser.write(l2)
out = ser.read(1000000000)
print(out)
time.sleep(5)


l2 = []
for c in command14: # in Python, a string is just a sequence, so we can iterate over it!
l2.append(ord(c))
l2.append(13)
l2.append(10)
print(l2)
ser.write(l2)
out = ser.read(1000000000)
print(out)
time.sleep(10)


l2 = []
for c in command15: # in Python, a string is just a sequence, so we can iterate over it!
l2.append(ord(c))
l2.append(13)
l2.append(10)
print(l2)
ser.write(l2)
out = ser.read(1000000000)
print(out)
time.sleep(20)

while ser.read():
print('serial open')

print('serial closed')
ser.close()
except serial.serialutil.SerialException:
print ('exception')

最佳答案

您可以将所有命令保存到列表中并遍历列表,并根据命令类型将额外的 \n 字符添加到 l2 数组并相应地管理 sleep 时间也是如此。希望以下代码对您有所帮助-

try:
ser = serial.Serial("COM32", baudrate=115200, timeout=0, stopbits=serial.STOPBITS_ONE)
if ser.isOpen():
print(ser.name + ' is open...')
else:
print(ser.name + ' is closed...')

all_commands = [command1, command2, command3, command4, command5, command6, command7, command8,
command9, command10, command11, command12, command13, command14, command15]
exit_commands = all_commands[:6]
if 'exit' in exit_commands:
ser.close()
exit()
else:
sleep10_commands = all_commands[5:8] + all_commands[13:14]
sleep5_commands = all_commands[2:3] + all_commands[12:13]
sleep8_commands = all_commands[4:5]
sleep20_commands = all_commands[14:15]
extra_nw_line_commands = [all_commands[2], all_commands[4], all_commands[6], all_commands[7], all_commands[12]]
for command in all_commands:
l2 = [ord(c) for c in command]
l2.extend([13, 10])
if command in extra_nw_line_commands:
l2.extend([10, 10])
print(l2)
l2_byte = bytearray(l2)
print(l2_byte)
ser.write(l2_byte)
out = ser.read(1000000000)
print(out)
sleep_time = 1
if command in sleep10_commands: sleep_time = 10
elif command in sleep5_commands: sleep_time = 5
elif command in sleep8_commands: sleep_time = 8
elif command in sleep20_commands: sleep_time = 20
time.sleep(sleep_time)

while ser.read():
print('serial open')

print('serial closed')
ser.close()
except serial.serialutil.SerialException:
print ('exception')

关于python - 我怎样才能添加一个for循环来减少这段代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57884084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com