python - 同时运行 c++ 和 python 脚本会导致问题

标签 python c++ linux raspberry-pi

目前我遇到了问题,我被迫将我的项目拆分为 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)

最佳答案

为了让 C++ 程序完成它必须完成的工作,您可以从 Python 脚本中调用它,读取它的输出并在您需要做更多工作时发送一些输入。为了实现这一点,您可以使用 subprocess python提供的库。

然后您的 C++ 代码需要等待一些特殊输入(例如换行符 '\n' 或类似的东西),然后计算需要计算的内容,然后将数据写入 stdout 然后写一行指示数据集的结尾(例如简单地写'end',或者一个空行)

你的 python 代码看起来像这样:

import subprocess

#Start the c++ program
process = subprocess.Popen(['<path to the c++ executable>', <arg1>, <arg2], stdin=subprocess.PIPE)
#Grab its output
p_out = process.stdout
#Grab its input
P_in = p.stdin

while True:
    p_in.write('\n')
    lines = []
    line = p_out.readline().decode("utf-8").strip()
    while line:
      <process the read data>
      line = p_out.readline().decode("utf-8").strip() 

关于python - 同时运行 c++ 和 python 脚本会导致问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33084754/

相关文章:

c++ - 尝试构建xerces c++解析器。 Netbeans和命令行构建错误未定义对“xercesc_3_2…”的引用

linux - 仅复制可执行文件(跨平台)

linux - 我在哪里可以在 ubuntu 9.04 中找到 video4linux2 设备/开发/视频

python - 如何使用Featuretools为没有直接特征的单个表创建特征?

python - 使用 os.walk 获取 Python 中的文件路径

javascript - flask.abort() 究竟做了什么?

python - 如何在 Python 中模仿 dict 的节省空间的版本?

c++ - 如何使用这个Qproperty网格库?

c++ - 如何在嵌入式 v8 中解析 JSON?

c++ - std::getline 在遇到 eof 时抛出