python - 从 stdout 获取原始流数据到 python 程序

标签 python linux subprocess pipe

我有一个 hackrf 硬件单元,它正在将连续的原始 uint8 数据流提供给 linux shell 管道。

例如,这会将连续数据通过管道传输到 linux shell 中的另一个应用程序,如下所示:

hackrf_transfer -r/dev/stdout -f 92700000 -s 8000000 - | (另一个应用程序阳 ionic )

在 python 中,这将做同样的事情:

hackout = subprocess.Popen(['hackrf_transfer', '-r', '/dev/stdout', '-f', '92700000', '-s', '8000000'], stdout=subprocess.PIPE)

但是我无法将 Hackrf 管道流导入 python 脚本。例如,我可能想抽取原始数据流或以某种方式对其进行操作,然后将其发送到另一个子进程应用程序等。像这样:

(HackRF)source subprocess >> a python script >> sink subprocess (eg. baudline)

或在单个 python 脚本中:

source hackrf >> my_function >> sink application

我可以在 python 脚本中执行 source >> sink,其中两个应用程序都已经接受了一个 shell 命令,例如将 hackrf 子进程管道插入 Baudline 子进程标准输入管道。换句话说,如果这两个应用程序使用管道在 shell 中工作,则它在 python 子进程调用中工作。但是我无法在这个 shell 管道之间获取 python 函数来使用 python 脚本或函数更改数据。

请问有人对我如何解决这个问题有任何想法吗?

最佳答案

hackrf_transfer 的输出是一个字节流,不是面向行的,所以 readlines() 不起作用;使用 read(8*1024) 相反。

If I use hackout.stdout.read() or hackout.communicate it 'sinks' the data stream.

是的,那些没有参数的调用不能用于并行读取连续的数据流。

这就是我告诉我使用 read(8*1024) 的原因.

Its not running errors or messages with this: data = hackout.stdout.readlines(8*1024) but I want to take this and feed it to stdout. I tried sys.stdin.write(data) its not writing but It's seeing 'data' as a list. So its captured the data but I can't write that captured data back out.

我希望你的意思是read而不是 readlines ,出于我在本文开头所述的原因。

这是工作代码的草图,基于您在同时删除的“答案”中发布的内容:

hackout = subprocess.Popen(['hackrf_transfer', …], stdout=subprocess.PIPE)
# We need to start the "sink subprocess" at the outset, and that with stdin=PIPE
baudline = subprocess.Popen("baudline … -stdin …", stdin=subprocess.PIPE, shell=True)

def decimator():
    for iq_samples in iter(lambda: bytearray(hackout.stdout.read(8*1024)), b''):
        # convert the samples chunk for use by numpy, if you wish
        dat = np.array(iq_samples)
        dat = dat.astype(float)/255
        # dat = … do further processing, as you wish    
        # now convert data back for use by baudline -format u8 and write out
        baudline.stdin.write((dat*255).astype('i1').tostring())

decimator()

关于python - 从 stdout 获取原始流数据到 python 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49141994/

相关文章:

python - 在 Python 中使用空格有什么陷阱吗?

Python PIL导入Mac兼容性

linux - 比较两个文件并仅获取差异 - shell 脚本

python - 子进程模块、envoy、sarge 和 pexpect 之间的区别?

python - 我怎么知道我的子进程是否在等待我的输入?(在 python3 中)

python - 使用双递归从列表中删除 John Wick

python - 迭代项目列表并将它们分组到具有相似度分数的字典中的最快方法是什么

linux - 关于 PID Shell 脚本

linux - Ubuntu 更新管理器的问题

python - 在保持命名空间和传递参数的同时,Python 中执行脚本的最佳方式是什么?