Python Pexpect和Minicom无法正常工作

问题描述 投票:0回答:1

我需要执行许多只能通过串行端口执行的开关过程。为此,我使用Pexpect和Minicom。我可以执行命令child.sendcontrol(“c”)打开菜单。但是从菜单中我无法选择任何选项。

当我通过Minicom手动执行此操作时,不会出现问题。可能是什么问题?

注意,我使用child.after来验证操作是否已实际执行。

这是我的代码:

#!/usr/bin/python3

import pexpect

print("Waiting for connection....") # for check
child = pexpect.spawn("minicom")
child.expect(r".*Hit.*", timeout=120)
test = child.after
fileNameone = "/root/Desktop/fileone.txt"
file = open(fileNameone, "wb")
file.write(test)
file.close

child.sendcontrol("c")
print("menu") # for check

child.expect(".*selection.*", timeout=10)
testtwo = child.after
fileNametwo = "/root/Desktop/filetwo.txt"
file = open(fileNametwo, "wb")
file.write(testtwo)
file.close
child.sendline("1")
print("choose 1") # for check


child.expect(".*Gateway.*", timeout=40)
child.sendline("admin")
child.expect(".*Password.*", timeout=10)
child.sendline("admin")
print("connect!") # for check

这是按ctrl + c键打开菜单的请求(该日志是通过minicom从pexpect获取的:]

[23;80H [24;1H************ Hit 'Ctrl + C' for boot menu ************
[23;80H [24;1H
[23;80H [24;1H 2 

这是菜单(日志是通过minicom从pexpect获取的:]

[23;80H [24;1HWelcome to Embedded Boot Menu :
[23;80H [24;1H
[23;80H [24;1H[24;9H1. Start in normal Mode
[23;80H [24;1H[24;9H2. Start in debug Mode
[23;80H [24;1H[24;9H3. Start in maintenance Mode
[23;80H [24;1H[24;9H4. Restore to Factory Defaults (local)
[23;80H [24;1H[24;9H5. Install/Update Image from Network
[23;80H [24;1H[24;9H6. Restart Boot-Loader
[23;80H [24;1H[24;9H7. Run Hardware diagnostics
[23;80H [24;1H[24;9H8. Upload preset configuration file
[23;80H [24;1H[24;9H9. Delete preset configuration file
[23;80H [24;1H
[23;80H [24;1H[24;9HPlease enter your selection (pres

我正在尝试选择选项1进行测试。但是不执行。每个菜单选项都一样。

child.expect(".*selection.*", timeout=10)
child.sendline("1")

这是错误:(启动时间不应超过15秒)

Traceback (most recent call last):
  File "./test.py", line 33, in <module>
    child.expect(".*Gateway.*", timeout=40)
  File "/usr/local/lib/python3.6/site-packages/pexpect/spawnbase.py", line 344, in expect
    timeout, searchwindowsize, async_)
  File "/usr/local/lib/python3.6/site-packages/pexpect/spawnbase.py", line 372, in expect_list
    return exp.expect_loop(timeout)
  File "/usr/local/lib/python3.6/site-packages/pexpect/expect.py", line 181, in expect_loop
    return self.timeout(e)
  File "/usr/local/lib/python3.6/site-packages/pexpect/expect.py", line 144, in timeout
    raise exc
pexpect.exceptions.TIMEOUT: Timeout exceeded.
<pexpect.pty_spawn.spawn object at 0x7fefa0934a90>
command: /usr/bin/minicom
args: ['/usr/bin/minicom']
buffer (last 100 chars): b's ENTER to finish) :1\r\n\x1b[23;80H \x1b[24;1H'
before (last 100 chars): b's ENTER to finish) :1\r\n\x1b[23;80H \x1b[24;1H'
after: <class 'pexpect.exceptions.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 10862
child_fd: 5
closed: False
timeout: 30
delimiter: <class 'pexpect.exceptions.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
searcher: searcher_re:
    0: re.compile(b'.*Gateway.*')
python python-3.x serial-port pexpect
1个回答
0
投票

我能够解决问题,minicom必须手动输入。甚至child.sendline用Enter发送。由于某种原因,minicom无法进入。Child.send ("\r\n")也不起作用。

解决方案是:

[C0行之后

我添加了child.sendline("1")行以按Enter。

现在它可以正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.