如何连接一个Python和C程序? [英] How do I connect a Python and a C program?
问题描述
我有一个基于Python的程序,读取串行数据的关连到一个RS232电缆的端口。我想通过我这里得到一个C程序将处理事情的计算密集型侧的数据。我已经检查了网络和所有我发现是基于Linux的。
I have a python-based program that reads serial data off an a port connected to an rs232 cable. I want to pass the data I get here to a C-program that will handle the computation-intensive side of things. I have been checking up the net and all I've found are linux-based.
推荐答案
要解决这个最简单的方法可能是只使用的popen(3)
。在的popen
功能在Python和C语言中,将连接与其他使用管道要么语言的程序。
Use a pipe and popen
The easiest way to deal with this is probably to just use popen(3)
. The popen
function is available in both Python and C and will connect a program of either language with the other using a pipe.
>>> import subprocess
>>> print args
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args)
一旦你管,你应该通过它发送YAML或JSON,虽然我从来没有尝试无论是在C.阅读。如果这真是一个简单的流,你来就可以解析它。如果你喜欢XML,我想这是可用的。
Once you have the pipe, you should probably send yaml or json through it, though I've never tried to read either in C. If it's really a simple stream, just parse it yourself. If you like XML, I suppose that's available as well.
这篇关于如何连接一个Python和C程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!