简体   繁体   English

在Visual C ++和Python之间交换数据

[英]Exchanging data between Visual C++ and Python

Here's the story, 这是故事,

I have a Visual C++ code that runs a vehicle simulator. 我有一个运行车辆模拟器的Visual C ++代码。 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. 我的目标是自动化车辆控制,但是,我已经在Python中实现了控件类。

I want to input the state of the vehicle from the C++ code to the python class, in which some processing is done. 我想从C ++代码输入车辆的状态到python类,其中进行了一些处理。 Then, the python class should output the corresponding control input, to be received by the C++ code. 然后,python类应该输出相应的控制输入,由C ++代码接收。

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. 底线是,我希望C ++类和python类在彼此之间交换数据的同时运行。

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. 这样,如果需要,您的控制器和C ++应用程序可以在不同的机器上运行。

On the C++ side you could base you application around boost::asio to read messages from the Python control application and send responses. 在C ++方面,您可以将应用程序作为boost::asio以便从Python控件应用程序中读取消息并发送响应。 see: Boost Asio 见: Boost Asio

For the python side see: Python sockets HowTo 对于python方面,请参阅: Python套接字HowTo

Notes: 笔记:

boost::python will allow you to call C++ from Python and Python from C++. boost::python将允许您从Python调用C ++,从C ++调用Python。

SWIG can also be used to wrap C++ classes so that they can be called from Python. SWIG还可以用于包装C ++类,以便可以从Python调用它们。

Could you try ctypes for that ( see here )? 你可以尝试ctypes见这里 )?

To call things in python from C++, you can use the Python API . 要从C ++中调用python中的东西,可以使用Python API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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