title: Embedding python serial controller in C++
body: <p>I've been searching around for several hours trying to get this code working and I just can't quite seem to get it.</p>
  
  <p>I'm working on a function in C++ where I can call one of a number of python scripts, which have variable numbers of arguements. The Python works, but I keep getting segfaults in my C++. </p>
  
  <pre><code>  double run_python(motor_command command){
      //A routine that will run a python function that is in the same directory.
      Py_Initialize();
      PySys_SetPath(".");
      string pyName; //Declaration of the string and int
      int speed;
      if (command.action == READ){
          pyName = "read_encoders"; //Name of one python module
      }else{
          pyName = "drive_motor"; //Name of the other python module
          speed = command.speed;  //struct
      }
  
      int board_address = command.board_address;
      int motor = command.motor_num;
  
      //PyObject* moduleName = PyString_FromString(pyName.c_str()); 
      //  Py_INCREF(myModule);
      //PyObject* myFunction = PyObject_GetAttrString(myModule, "run"); //Both of these python functions have subroutine 'run'
  
      PyObject* args;
      if(command.action == READ){
          args = PyTuple_Pack(2,PyInt_FromLong(board_address),PyInt_FromLong(motor)); //Appropriate args for the read_encoders
      }else{
          args = PyTuple_Pack(3,PyInt_FromLong(board_address),PyInt_FromLong(motor), PyInt_FromLong(speed)); //Appropriate args for the drive_motor
          }
      Py_INCREF(args);
      cout &lt;&lt; "I got here" &lt;&lt; endl;
      PyObject* myModule = PyImport_Import((char*)pyName.c_str());//Python interface
      cout &lt;&lt; "args = " &lt;&lt; args &lt;&lt; " modlue = " &lt;&lt; myModule &lt;&lt; endl;
      //Py_INCREF(myModule);
      PyObject* myResult = PyObject_CallObject(myModule, args); //Run it and store the result in myResult
      Py_INCREF(myResult);
  
      double result = PyFloat_AsDouble(myResult);
      Py_DECREF(myResult);
  
      return result;
  
  }
  </code></pre>
  
  <p>So far, what I can figure out is that somehow my myModule is not geting imported correctly and is returning a NULL value. As a result, when I attempt the _CallObject, it throws a segfault and I'm up a creek. When I uncommend the Py_INCREF for myModule, it throws a segfault there, and so I guess taht I'm not importing my python code correctly. </p>
  
  <p>Oh, useful information: OS: Angstorm Linux, on a MinnowBoard (x86 architecture). </p>
  
  <p>General structure of the python program: </p>
  
  <pre><code>import sys
  import serial
  
  board_num = sys.argv[1]
  motor = sys.argv[2]
  speed = sys.argv[3]
  
  
  def run(board_num, motor, speed):
      # Command arguments: Board number (0x80, 0x81...), motor number (0 or 1) and speed(2's complement signed integer)
  
      ser = serial.Serial('/dev/ttyPCH1', 38400)
  
      motor_min = 0
      motor_max = 1 # These are the two acceptable values for motor enumerated values. 
      e_code = -1 # Error code
  
      try:
          board_num = int(board_num, 0)
      except:
          print "Invalid address format: Must be a number"
          exit(e_code)
      try:
          motor = int(motor, 0)
      except:
          print "Motor must be either motor 0 or 1. Or possibly one or two..."
          exit(e_code)
      try:
          speed = int(speed, 0)
      except:
          print "Motor speed must be an integer."
          exit(e_code)
  
      #print board_num
  </code></pre>
  
  <p>Thank you in advance! If you have any alternative ways to get this working in the same way, I'm open for suggestions! </p>