Warm tip: This article is reproduced from serverfault.com, please click

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

(其他 - How to solve the segmentation fault issue when I use Python ctypes to call rs232.c?)

发布于 2020-11-30 01:25:17

我将rs232.c构建为共享库,并尝试使用python3进行调用。但是,当尝试获取COM端口tcgetattr()的属性时,出现了“ Segmentation fault”错误。有人知道这是什么问题吗?我的操作系统是树莓派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;
}
Questioner
Roger Lin
Viewed
0
Joseph Sible-Reinstate Monica 2020-11-30 12:08:57

问题是你为变量命名error并使其成为全局变量作为GNU扩展,glibc添加了一个名为的函数error,并且你的库最终使两者混淆,并尝试写入tcgetattr称为的函数的返回值error要对其进行修复,请重命名error为其他名称,static对其进行声明,或者将其声明移入RS232_OpenComport