【发布时间】:2015-10-12 15:24:56
【问题描述】:
目前我遇到了问题,我不得不将我的项目拆分为 python 和 c++ 脚本,因为它们具有我需要的硬件指定库。作为我项目的一部分,c++ 脚本为我的 LED 灯条创建 rgb 数据,python 脚本将它们上传到 LED 灯。因此,我需要在程序之间进行信息交换,因为 LED 必须一直刷新,这将我们引向主要问题。 c++ 脚本将数据写入 .txt 文件并写入 trigger2.txt 文件。 python 脚本一直在等待触发文件,直到它出现才开始。 Python 脚本完成后,它还会创建一个 trigger.txt 文件,以便 c++ 脚本知道它可以重新启动。 当我运行我的程序时,它会上传一次 rgb 数据,然后停止。似乎在c ++脚本启动python脚本并上传了rgb数据后,c ++无法继续,因为python脚本在while循环中要求trigger2.txt。就像 c++ 脚本作为进程列表中的第二个等待完成 python 脚本一样。
这是 c++ 脚本:
int main(){
int counter = 0;
int a;
while(1){
a=0;
if (counter>0){ //If counter==0 it's the first time an no trigger is needed
while(a<1){
std::ifstream FileTest("trigger.txt"); //If the trigger file exists the script can start again
if(FileTest){
a++;
system("sudo rm trigger.txt"); //cleaning the folder
}
else
std::cout << "Can't find trigger.txt" << std::endl;
}
}
if (a==1||counter==0){
/////The rgb data is generated here
std::fstream file; //Writing the .txt to transfer the rgb data
file.open("rgb.txt", std::ios::out);
for (int i = 0; i < 3;i++)
file << rgbmatrix[i]<< std::endl;
file.close();
system("sudo touch trigger2.txt"); //The Python script can start now to refresh the led's
if (counter==0){ //The first time led.py will be started manually
system("sudo python led.py");
zaehler++;
}
}
}
return 0;
}
这是python脚本:
rgb = [1,2,3]
i=0
import time
import sys
import os
while 1:
if (os.path.exists('/home/pi/rgbwired_v2/trigger2.txt')):
os.system("sudo rm trigger2.txt")
file = open("rgb.txt","r")
rgb = file.readlines()
file.close()
rgb = [int(i) for i in rgb]
#RGB data will be uploaded here
sys.stdout.write('led printed')
os.system("sudo rm rgb.txt")
os.system("sudo touch trigger.txt")
else:
sys.stdout.write('File not found')
time.sleep(1)
感谢您的所有帮助。谢谢。 (我正在使用 Raspberry Pi)
【问题讨论】:
-
从python脚本中调用c++程序会不会有问题?因为如果没有,你可以试试。
-
感谢您的回答,它并没有解决问题,但现在我很确定我的怀疑是正确的。现在,在我更改了我的代码之后,它只会不断地运行我的 c++ 脚本的 while 循环。脚本似乎真的不能交替。我能做些什么呢:/?
标签: python c++ linux raspberry-pi