在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;
}