1

I'm been struggling with this problem pretty long. I have to communicate with an Arduino and C++ program. I know it is easy in Python, but it has to be in C++ for school project. So I'm reading a lot of things, but no one help me further.

So I have a short Python script that communicate with the arduino very well and fast written in Python.

communication.py:

import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
def communicate(number): 
   ser.write(number)
   return ser.readline()

The Arduino gets the number and returns values of light sensors in a string. Now I want this to communicate with C++ program. My C++ code (this is only a small part of the full program)

mainwindow.cpp:

void MainWindow::run(){
while (ui->startstop->value() == 1){// as long that the program must run
    blockchange = 0;
    // get data from arduino
    Py_Initialize();
    const char* modulename = "communication";
    PyObject *pName = PyUnicode_FromString(modulename);
    PyObject *pModule = PyImport_Import(pName);
    if (pModule != NULL){
        PyObject *pDict = PyModule_GetDict(pModule);
        PyObject *pFunc = PyDict_GetItem(pDict, PyUnicode_FromString("communicate"));
        if (pFunc != NULL){
            PyObject_CallObject(pFunc, PyLong_FromLong(blockchange));
        }else{
            std::cout << "couldn't find func\n";
        }
    }else{
        std::cout << "pyhton module not found\n";
    }
}

It only gives "python module not found". Which means that PyImport_Import(pName) returns NULL. What is wrong?

I use Ubuntu 18.04 and my standard version of Python is 3.5 and the program is written in Qt Creator. I tried a lot of things, also without Python, but I haven't found anything that works. I only want that my Arduino reads one int from 0 to 6, and that the C++ program reads a string of 6 numbers separated with a ",".

gre_gor
  • 6,669
  • 9
  • 47
  • 52

0 Answers0