![](/img/trans.png)
[英]Is there a way to continuously collect output from a Python script I'm running into a c++ program?
[英]Printing continuously from Python script to C program
我正在考慮將數據發送到 C 程序以用於 RTAI FIFO 通信,據我了解,沒有 RTAI 支持直接從 Python 腳本通信到 Z50484C19F1AFDAF3841A0D82ZED3,因此是我的解決方案。
我正在使用subprocess
來處理這些通信,但是它不起作用。
我的 Python 腳本:
import subprocess
from random import seed
from random import random
p = subprocess.Popen(['./data_reciever'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while True:
seed(1)
network_data = f"{random()}, {random()}, {random()}".encode()
print(network_data, "from py")
p.stdin.write(network_data)
p.stdin.flush()
我的 C 代碼:
#include <stdio.h>
int main(void){
while(1){
char data;
scanf("%c", &data);
printf("%c", data);
}
return(0);
}
我認為我的問題在於stdin.write
或我的scanf
,因為我的 python 打印正在打印到終端,但我的 C 程序不是。
我在這里嘗試了許多類似的例子,但我無法讓它們工作,任何幫助將不勝感激。
我對Popen
沒有很多經驗,但這似乎是因為您說該過程的stdout
將通過管道傳輸,但是您不會嘗試將 pipe 數據輸出。 從參數列表中刪除stdout=subprocess.PIPE,
為我修復了它。
p = subprocess.Popen(['./data_reciever'],
stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
此外,您不應該在循環中播種。 這就是為什么你的隨機數據都是一樣的。
C 循環也可以簡化一點:
while(1){
char data = getchar();
printf("%c", data);
}
雖然這與問題無關。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.