如何将C API集成在机器人框架中

How to integrate C++ APIs in Robot framework?

本文关键字:机器人 框架 集成 API      更新时间:2023-10-16

我已经为我的项目开发了C API。还创建了一个Linux .SO共享库。我需要使用机器人框架关键字来调用这些API。

预先感谢。

c apis可以使用python库 ctypes 轻松地调用。您可能已经知道,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

现在您已经创建了Connect.SO共享库在与CPP文件相同的路径中。

2。为C APIS创建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)使用CTYPE调用API时,您应始终指定输入参数类型。否则,使用此Python库运行机器人文件时,您可能会遇到以下错误:

notimplemplementError:尚不支持的variadic函数;指定参数列表

在这种情况下,我们将参数指定为ctypes.c_char_p,用于字符串类型和ctypes.c_int作为整数。有关更多信息,您可以使用:

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

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

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 test.robot.txt

一切正常:)