首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Pexpect和Minicom未按预期工作

Python Pexpect和Minicom未按预期工作
EN

Stack Overflow用户
提问于 2020-05-31 14:55:17
回答 1查看 397关注 0票数 0

我需要执行许多进程来切换,这些进程只能通过串口执行。为此,我使用了Pexpect和Minicom。我可以执行命令child.sendcontrol(“c”)来打开菜单。但是我不能从菜单中选择任何选项。

当我通过Minicom手动执行此操作时,它可以正常工作。可能的问题是什么?

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

这是我的代码:

代码语言:javascript
复制
#!/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打开菜单的请求(日志取自pexpect到minicom):

代码语言:javascript
复制
[23;80H [24;1H************ Hit 'Ctrl + C' for boot menu ************
[23;80H [24;1H
[23;80H [24;1H 2 

这是菜单(日志取自pexpect到minicom):

代码语言:javascript
复制
[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,用于测试。但是它没有被执行。每个菜单选项也是如此。

代码语言:javascript
复制
child.expect(".*selection.*", timeout=10)
child.sendline("1")

这是错误:(引导时间不应超过15秒)

代码语言:javascript
复制
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.*')
EN

回答 1

Stack Overflow用户

发布于 2020-05-31 20:40:41

我解决了这个问题,minicom必须手动输入。即使是child.sendline发送的回车。由于某种原因,迷你通信无法进入。Child.send ("\r\n")也不能工作。

解决方案是:

child.sendline("1")行之后

我为按enter添加了child.sendcontrol("m")行。

现在它可以正常工作了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62112671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档