0

I would love to embed this python program on a c++ one , but I'm kinda struggling there , I never studied python , and even after reading the explanation in others websites about the method I couldn't really apply it , because I don't know what are those python members ,I want to run this program under c++, this code is a good one , but couldn't find it in c++ version , this is why I decided to use the embedded option .

here is the python program

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Le Raspbery Pi envoie des messages à l'Arduino

import serial  # bibliothèque permettant la communication série
import time    # pour le délai d'attente entre les messages

ser = serial.Serial('/dev/ttyACM0', 9600)
compteur = 0
while True:     # boucle répétée jusqu'à l'interruption du programme
    if compteur < 6:
        compteur = compteur + 1
    else:
        compteur = 0
    ser.write(str(compteur))
    time.sleep(1)               # on attend pendant 2 secondes

and here is the embedded program that I tried , but I'm pretty sure that its wrong , because there is no call of python object

#include <iostream>
#include <Python.h>

int main(int argc, char **argv) {
    Py_Initialize();
    return 0;
}

Can anybody help me to do this !? thanks in advance .

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
MadHer
  • 91
  • 3
  • 11
  • @PaulWürtz thanks for your proposition , yes I did before this and I did choose this solution because its the more oprtimal for me , and for the example on the website its written on c , I know that we can execute it even in c++ , but in my case , this is not the good one . – MadHer Jun 23 '18 at 04:23
  • I'm trying right now to right the same code , using another library in c++ , for exactly those serial communication , will try to post it after , but , I would really love to see the embedded version of the python code , exactly because its a simple one – MadHer Jun 23 '18 at 04:29

1 Answers1

1

For simple application as this, I wouldn't link the python headers to your programm, but as a more easy solution I would trigger the system() command of C++ and process the output.

Here a example from this question:

#include <cstdio>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <array>

std::string exec() {
    std::array<char, 128> buffer;
    std::string result;
    const char* cmd = "./python script.py";
    std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
    if (!pipe) throw std::runtime_error("popen() failed!");
    while (!feof(pipe.get())) {
        if (fgets(buffer.data(), 128, pipe.get()) != nullptr)
            result += buffer.data();
    }
    return result;
}

You would then have to change the infinite loop in your python code, so that the python programm terminates and the rest of your C++ code executes, then you could probe the arduino with your python script from you C++ program.

Paul Würtz
  • 1,641
  • 3
  • 22
  • 35
  • actually this code will be executed under a QT gui , I m not sure if this solution is the right one for me , I just want to embedd this code hh , if you can help there . – MadHer Jun 23 '18 at 04:26
  • Using Pythons Qt modules seems off, since you are more familiar with C++? Here a post that migth help. http://doc.qt.io/archives/qq/qq23-pythonqt.html But in general triggering `system()` in a Qt program does not seem all to wrong for me ;) – Paul Würtz Jun 23 '18 at 04:36
  • I didnt get what you want to say for real , but I will explain , Im using QT with c++, and Ill be just adding the python path in my makefile .I see , hh but even a simple embedded programm c++ will work , because I will just execute it as a slot for a specific button – MadHer Jun 23 '18 at 04:44
  • ur solution worked , thank you so much , hh but Ill still waiting for somebody else maybe to give me this embedded version . – MadHer Jun 23 '18 at 05:12