python调用C语言里面的函数

     在arm做接收DTU参数的时候,始终无法获取到DTU映射文件(/dev/dtu)里面的数据(可能没找到方法 快哭了)
     刚开始用os去打开
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os

fd = os.open('/dev/dtu',os.O_RDWR)
ret = os.read(fd,10)
print ret

    得到的始终为空,后来发现用C语言里面的函数可以得到数据,写了一个check.c文件:
#include <stdarg.h>
#include <termio.h>
#include <sys/timeb.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <stdlib.h>

#define  MCU_DIDO_IOC_MAGIC    'd'
#define  IO_GPIO_SET_OUTPUT    _IO(MCU_DIDO_IOC_MAGIC,0)
#define  IO_GPIO_RESET_OUTPUT  _IO(MCU_DIDO_IOC_MAGIC,1)
#define  IO_GPIO_GET_INPUT     _IOR(MCU_DIDO_IOC_MAGIC,2,int)

int testdi()
{
	int dtu_fd;
	unsigned int input_value = 0 ;
	int i;
	int ret;
	
	if((dtu_fd = open("/dev/dtu", O_RDWR)) < 0) 
	{
		perror("open /dev/dtu error");
		return -1;
	}
	
	ret = ioctl(dtu_fd,IO_GPIO_GET_INPUT,&input_value);
	close(dtu_fd);
	if(ret == 0)
		return input_value;
	else
		return -1	
}
      用arm-linux-gcc -o check.so -shared -fPIC check.c生成check.so文件(每个人的交叉编译链不一样)
      写一个get_value.py:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import ctypes  
ll = ctypes.cdll.LoadLibrary   
lib = ll("/check.so")    
print lib.testdi() 
      运行后得到自己想要的数据 大笑


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值