方法argv []中的错误SWIG C ++ / Python,会发生什么?

问题描述 投票:0回答:1

我正在使用Swig将C ++函数与我的Python库连接。我设法编译了所有内容并创建了.so文件。但是在python中导入了我的C ++函数之后,我在argv上出现了一些错误...也许我是错误的,无法从外部将值传递给它,或者当我编译并创建.i swig文件时我错了C ++函数创建一个TCP套接字,并从一个带有我编写的C ++代码的终端发送一个十六进制代码到使用另一个十六进制代码进行回复的设备:

connect_PE_func 192.168.1.170 600000060

而且效果很好。而且效果很好。一旦通过python中的swig设置c ++函数,我会期望使用类似的语法,请输入:

answer = connect_PE_func.connect_PE_func(“ 192.168.1.170”,600000060,2)

但我收到此错误:

test=connect_PE_func.connect_pe_func(["192.168.2.170"],["2600000026"])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-12-f58f79b27fe5> in <module>
----> 1 test=connect_PE_func.connect_pe_func(["192.168.2.170"],["2600000026"])

~/mqtt_atenapy/C_connect_PE_dev/test_cpython/connect_PE_func.py in connect_pe_func(argv, argc)
     64
     65 def connect_pe_func(argv, argc):
---> 66     return _connect_PE_func.connect_pe_func(argv, argc)
     67
     68

TypeError: in method 'connect_pe_func', argument 1 of type 'char *[]'

我将代码附加到文件.c .h e .i,该文件与swig一起使用以获取.so。

谢谢。

connect_PE.func.c:

#include <stdio.h>
#include <errno.h>
#include <string>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <cstring>
#include <unistd.h>
#include <iostream>
#include <unistd.h>
#include <sstream>
#include "connect_PE_func.h"
using namespace std;

// to compile gcc connect_PE_func.cpp -lstdc++ -c
char* connect_pe_func(char *argv[],int argc)
{
    int sockfd, n;
    int connected = 0;
    struct sockaddr_in servaddr;
    std::string serveraddr = argv[1];

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = inet_addr(serveraddr.c_str());
    servaddr.sin_port = htons(9761);

    connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

    std::string pref_hex;
    std::string hex("0x");
    std::string test = argv[2];
    size_t numbytes = test.size() / 2;

    uint8_t command[numbytes];

    for (size_t w = 0, x = 0; w < numbytes; ++w, x += 2)
    {
        pref_hex = hex + test.substr(x, 2);
        cout << pref_hex;
        command[w] = stoi(pref_hex, nullptr, 16);
        //cout << test.substr(x, 2);
        //cout << "\n";
        //cout << command[w];
        //cout << "\n";
    }

    //uint8_t command[] = {0x26, 0x00, 0x00, 0x00, 0x26};
    int bytes_to_send = sizeof(command);

    send(sockfd, command, bytes_to_send, 0);
    uint8_t output_command[numbytes];
    recv(sockfd, output_command, bytes_to_send, 0);

    char test_out[10];

    for (size_t w = 0, x = 0; w < numbytes; ++w, x += 2)
    {
        test_out[x] = (char)output_command[w];
        //cout << unsigned(test_out[x]);
    }
    return test_out;
}

connect_PE_func.h:

// file: connect_PE_func.h
char* connect_pe_func(char *argv[], int argc);

connect_PE_func.i:

/* file: connect_PE_func.i */
%module connect_PE_func
%{
/* Everything in this block will be copied in the wrapper file. We include the C header file necessary to compile the interface
*/
#include "connect_PE_func.h"
//  extern char *connect_pe_func(int argc, char *argv[]);
%}

/* list functions to be interfaced: */
char* connect_pe_func(char *argv[], int argc);

我正在使用Swig将C ++函数与我的Python库连接。我设法编译了所有内容并创建了.so文件。但是在python中导入了我的C ++函数之后,我有了一些...

python c++ sockets swig argv
1个回答
0
投票

您放错了逗号func(["192.168.2.170"] , ["2600000026"])

© www.soinside.com 2019 - 2024. All rights reserved.