简体   繁体   中英

Exchanging data between Visual C++ and Python

Here's the story,

I have a Visual C++ code that runs a vehicle simulator. The simulator capture's the vehicle's state in a vector form, and can receive control inputs (eg. steering angle, pedal) either from the keyboard or manually (through code). My objective is to automate vehicle control, however, I have implemented the control class in Python.

I want to input the state of the vehicle from the C++ code to the python class, in which some processing is done. Then, the python class should output the corresponding control input, to be received by the C++ code.

Is there an easy way to achieve this? I have read similar posts but none of them was clear for me.

One way I thought about is using files, both classes read and write to a file from which they get the vehicle state and the control input, however, this could have tedious synchronization problems!

Pipelining is also feasible, but seems very complex, unless you could provide a nice simple solution :) :)

The bottomline is that, I want the C++ class and the python class to run concurrently while exchanging data between each other.

Thanks in advance!

您可以使用Boost :: Python来包装C ++代码。

If you need to run the two programs concurrently then there is no easy way to do it, and you will have to implement an asynchronous communication mechanism between the 2 separate processes.

One way to achieve this is to use sockets. That way your controller and C++ application could run on different machines if the need arises.

On the C++ side you could base you application around boost::asio to read messages from the Python control application and send responses. see: Boost Asio

For the python side see: Python sockets HowTo

Notes:

boost::python will allow you to call C++ from Python and Python from C++.

SWIG can also be used to wrap C++ classes so that they can be called from Python.

Could you try ctypes for that ( see here )?

To call things in python from C++, you can use the Python API .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM