bash Python:如何让脚本进入和退出minicom终端?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25448438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:12:44  来源:igfitidea点击:

Python : How to make a script enter and exit minicom terminal?

pythonlinuxbashloopssubprocess

提问by Tolga Varol

For entering minicom and saving the log of it, I use "sudo minicom -C nameoffile", but I want to do this in a loop, opening minicom can be done by using subprocess but I couldn't find anything to exit minicom in my loop and continue looping, since you need to enter "ctrl-a, then x" or "ctrl-a, then q" and after must press enter for confirming this. Anybody have any idea or suggestion?

为了进入 minicom 并保存它的日志,我使用了“sudo minicom -C namefile”,但我想在循环中执行此操作,可以使用 subprocess 打开 minicom 但我在我的计算机中找不到任何退出 minicom 的东西循环并继续循环,因为您需要输入“ctrl-a,然后x”或“ctrl-a,然后q”,之后必须按回车键确认。有人有任何想法或建议吗?

回答by Arnon Zilca

The first (and obvious) solution

第一个(也是显而易见的)解决方案

might take a little more effort (or not - you decide) and it is probably the bestway to do this is to open the device yourself using pySerial(and here's an example) and then do whatever you like with the data - write it to a file, parse it, send it to NASA or all of the above. :)



If you insist on working around that solution:

可能需要更多的努力(或不-您决定),这样做的最佳方法可能是使用 pySerial 自己打开设备这是一个示例),然后对数据执行任何您喜欢的操作-将其写入一个文件,解析它,将其发送给 NASA 或以上所有。:)



如果您坚持解决该解决方案:

One possibility I can think of...

我能想到的一种可能...

is piping the minicom command to tee. when you pipe through teeyou can give it a file name (to log to) and pipe teeas input to your own binary / script. That should take care of logging + parsing the input. (I Haven't tried it with minicom and i'm not sure how you will exit your piped program like that).

正在将 minicom 命令通过管道传输到tee. 当您通过管道tee传输时,您可以给它一个文件名(用于登录)并管道tee作为您自己的二进制文件/脚本的输入。那应该负责记录+解析输入。(我还没有用 minicom 试过它,我不确定你将如何退出你的管道程序)。

Another possibility I can think of...

我能想到的另一种可能性......

is redirect to a file (>) and then write a different binary / script that will read the file as it is being written (like tail -fdoes). Here'san example for reading a file in python while it is being written.

重定向到一个文件 ( >),然后编写一个不同的二进制文件/脚本,该脚本将在写入文件时读取该文件(就像这样tail -f做)。 是在编写文件时在 python 中读取文件的示例。

Since it's serial data, I'm guessing it's pretty slow (especially NMEA), so I don't think that you will have much latency if you write it to a file and read it using a different binary / script.

由于它是串行数据,我猜它很慢(尤其是 NMEA),所以我认为如果将它写入文件并使用不同的二进制文件/脚本读取它,你不会有太多延迟。