将大量数据非常快速地写入 C 子进程时,我遇到了 Broken Pipe 错误。
所以我正在从 python 脚本运行 ac 子进程:
process = subprocess.Popen("./gpiopwm", stdin=subprocess.PIPE)
while True:
process.stdin.write("m2000\n")
print "bytes written"
gpiopwm.c 的主循环部分:
printf("1\n");
while (1) {
fgets(input,7,stdin); // Takes input from python script
printf("2\n");
numbers = input+1; // stores all but first char of input
char first = input[0]; // stores first char of input
if (first=='m') {
printf("3\n");
printf("%s\n",numbers);
}
}
但是,此操作的输出如下:
1
bytes written
Traceback (most recent call last):
File "serial-receive-to-pwm.py", line 20, in <module>
process.stdin.write("m2000\n")
IOError: [Errno 32] Broken pipe
C 程序显然在该fgets
行中断,因为2
从未打印过。我做错了什么?我怎样才能避免这种情况?
编辑:我已经更新了该fgets
行,使其不包含取消引用参数,但仍然收到损坏的管道错误。
编辑:
input
初始化为char *input="m2000";