繁体   English   中英

当我使用 Python ctypes 调用 rs232.c 时,如何解决分段错误问题?

[英]How to solve the segmentation fault issue when I use Python ctypes to call rs232.c?

我将 rs232.c 构建为共享库,并尝试使用 python3 调用它。 但是在尝试获取 com 端口 tcgetattr() 的属性时,出现“分段错误”错误。 有谁知道这是什么问题? 我的操作系统是树莓派 p3。

testcom.py

from ctypes import *
comdll = cdll.LoadLibrary("rs232.so")
comdll.RS232_OpenComport(c_int(22),c_int(115200),c_char_p(b'8N1'))

rs232.c

#include <termios.h>
#include <unistd.h>
#define RS232_PORTNR  39
int Cport[RS232_PORTNR],error;
struct termios old_port_settings[RS232_PORTNR];

int RS232_OpenComport(int comport_number, int baudrate, const char *mode)
{
    error = tcgetattr(Cport[comport_number], old_port_settings + comport_number); //segmentation fault at this line
    return error;
}

问题是您将变量error命名为全局。 作为 GNU 扩展, glibc 添加了一个名为error的 function ,您的库最终混淆了两者并试图将 tcgetattr 的返回值写入名为errortcgetattr之上。 要修复它,请将error重命名为其他名称,将其声明为static ,或将其声明RS232_OpenComport中。

暂无
暂无

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

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