linux python调用c,arm-linux下python调用c函数

本文介绍了在ARM环境下,由于Python的限制,ctypes无法正常工作。作者通过尝试swig和cython等方法未果后,最终采用DL库成功解决了C库在ARM上的调用问题。示例代码展示了如何使用DL库代替ctypes来初始化、发送和接收数据,实现了与TCP.so库的交互。
摘要由CSDN通过智能技术生成

在PC机下面,用ctypes没有任何问题,如下:

#!/usr/bin/env python

import time

from ctypes import *

class gprs:

def __init__(self):

self.tcp = CDLL('./tcp.so')

#-------------------

def start(self):

self.tcp.start()

#-------------------

def send(self,addr,port,content):

self.tcp.send(addr,port,content)

#-------------------

def receive(self,len):

res=self.tcp.receive(len)

return c_char_p(res).value

#-------------------------------------------  end of class gprs

if __name__ =='__main__':

gh=gprs()

gh.start();

gh.send('124.16.138.205',8008,";;123;;")

st=time.time();

while 1:

gh.receive(0)

"""ct=time.time()-st

if ct>60:

print ct

break"""

gh.receive(0)

但是在arm下,由于PYTHON的自身原因,使得ctypes不能移植成功。

然后又尝试swig,cython等都很复杂,最后终于找到了DL.成功解决!

如下:

#!/usr/bin/env python

import dl,time

class gprs:

def __init__(self):

self.tcp = dl.open('./tcp.so')

#-------------------

def start(self):

self.tcp.call('start')

#-------------------

def send(self,addr,port,content):

self.tcp.call('send',addr,port,content)

#-------------------

def receive(self,len):

res=self.tcp.call('receive',len)

return res

#-------------------------------------------  end of class gprs

if __name__ =='__main__':

gh=gprs()

gh.start();

gh.send('124.16.138.205',8001,";;abc;;")

st=time.time();

while 1:

gh.receive(0)

"""ct=time.time()-st

if ct>60:

print ct

break"""

gh.receive(0)

//tcp.c void start()

{

ptty=powerOnAttachNet();

if(ptty==NULL)exit(1);

}

void send(char * addr,int port,char * s)

{

if(ptty==NULL) start();

tcpSend(ptty,addr,port,s);

}

char * receive(int len)

{

char * recbuf;

if(ptty==NULL) start();

recbuf=tcpReceive(ptty,len);

if(recbuf==NULL)return "receive a unmatched pacage from stty!/n";

else return recbuf;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值