gpt4 book ai didi

c++ - 如何在Robot框架中集成C++ API?

转载 作者:行者123 更新时间:2023-11-30 20:20:07 24 4
gpt4 key购买 nike

我为我的项目开发了 C++ API。还由此创建了一个 linux .so 共享库。我需要使用机器人框架关键字调用这些 API。

提前致谢。

最佳答案

可以使用Python库ctypes轻松调用C++ API。您可能已经知道 python 库可以集成到机器人框架中。

假设您必须使用机器人框架调用 SendMesg C++ API。请按照以下步骤操作:

<强>1。创建C++ API库.so文件

connect.cpp

extern "C"
{

int Initialize(char* ip, int port)
{
//creates socket connection with remote host
}

int SendMesg(char* msg)
{
//Send mesg code
}
}

g++ -std=c++11 -fpic -c connect.cpp

g++ -std=c++11 -shared -g -o connect.so connect.o

现在您已经在与 cpp 文件相同的路径中创建了 connect.so 共享库。

<强>2。为 C++ API 创建 python 包装器

connectWrapper.py

import ctypes

class connectWrapper:

def __init__(self, ip , port):
self.Lib = ctypes.cdll.LoadLibrary('absolute path to connect.so')
self.Lib.Initialize.argtypes = [ctypes.c_char_p, ctypes.c_int]
self.Lib.Initialize(ip, port)

def SendMessageWrapper(self, msg):
self.Lib.SendMesg.argtypes = [ctypes.c_char_p]
print self.Lib.SendMesg(msg)

创建 Python 包装器 API 时要记住的 4 件事。

a) python 文件名与类名相同

b) 使用 ctypes 调用 API 时,您应该始终指定输入参数类型。否则,使用此 python 库运行机器人文件时可能会出现以下错误:

NotImplementedError: variadic functions not supported yet; specify aparameter list

在本例中,我们将字符串类型的参数指定为 ctypes.c_char_p,将整数类型的参数指定为 ctypes.c_int。欲了解更多信息,您可以使用以下内容:

[http://python.net/crew/theller/ctypes/tutorial.html#specifying-the-required-argument-types-function-prototypes][1]

c) connect.so 的绝对路径在 ctypes.cdll.LoadLibrary API 中给出。

d) 使用 chmod +x connectWrapper.py 将 python 文件设置为可执行文件

<强>3。将Python库添加到机器人文件

test.robot.txt

** * Settings * **

Library "absoulte path to connectWrapper.py" 10.250.0.1 8080

** * Test Cases * **

Send Message

SendMessageWrapper "Hello World"

正如您可能注意到的,Python 库已添加到设置部分,参数作为 IP 和端口传递。我们在测试用例部分添加了 SendMessage 关键字,并以字符串消息“Hello World”作为输入参数。

我希望运行命令后:

机器人测试.robot.txt

一切正常:)

关于c++ - 如何在Robot框架中集成C++ API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47427011/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com