我正在将浮点变量转换为C中的字符串,并使用Linux中的命名管道将其发送到Python。问题是,我阅读了乱七八糟的字符以及Python方面的实际值。
C代码将浮点数转换为字符串
char str[64];
sprintf(str, "%f\n", angle);
write(fd_fifo, str, sizeof(str));Python代码读取接收到的值并在终端上打印
#!/usr/bin/python
import os
import errno
import time
FIFO = '/tmp/bldc_fifo'
try:
os.mkfifo(FIFO)
except OSError as oe:
if oe.errno != errno.EEXIST:
raise
print("Opening FIFO...")
with open(FIFO, encoding='utf-8', errors='ignore') as fifo:
print("FIFO opened")
while True:
time.sleep(0.1)
data = fifo.read()
print(data)在终端打印中我看到了这样的东西:
4\W`U7z3\ENU11.415311
,我的期望是看到:
11.415311
发布于 2019-03-31 18:57:46
使用strlen()而不是sizeof
char foo[99] = "the quick fox";
sizeof foo; /* 99 */
strlen(foo); /* 13 */在代码中,错误出现在对write()的调用中。
//write(fd_fifo, str, sizeof(str));
write(fd_fifo, str, strlen(str));https://stackoverflow.com/questions/55442948
复制相似问题