python与c语言交互应用实例
1、python向c语言写数据
1) 先将接收端编译成一个共享链接库
gcc/arm-linux-gnueabihf-gcc -o bluetooth_proxy.so -shared -fPIC bluetooth_proxy.c
bluetooth_proxy.c
#include <stdio.h>
struct bluetooth_t{
int status;
char buf[];
};
int bluetooth_proxy_cb(struct bluetooth_t bluetooth)
{
printf("bluetooth status:%d, buf:%s \n", bluetooth.status, bluetooth.buf);
return ;
}
2)运行发送端python脚本即可将python数据发送到c语言接口函数。
bt_msg_send_simple.py
#!/usr/bin/python
import ctypes
from ctypes import * class bluetooth(Structure):
_fields_=[('status',c_int),('buf',c_char * 128)] if __name__ == "__main__": func = ctypes.cdll.LoadLibrary("./bluetooth_proxy.so")
func.bluetooth_proxy_init()
s = bluetooth()
s.status = 555
s.buf = bytes('hello,world')
func.bluetooth_proxy_cb(s)
注意:如果python调用的函数参数仅仅是个简单的指针,可以不用映射。
例如char *data;
func.bluetooth_proxy_cb(data)
2、python从c语言读取数据
既然能调用c语言链接库函数参数来发送数据,接收数据也可以从通过c语言函数返回值传递了。
python_data = func.bluetooth_proxy_cb(var)
3、python的c语言拓展
用c语言写好so,然后 import xxxx 来无缝结合
test.c
#include<Python.h>
static PyObject *test(PyObject *self, PyObject *args){
int arg1, arg2;
if(!(PyArg_ParseTuple(args, "ii", &arg1, &arg2))){
return NULL;
}
return Py_BuildValue("i", arg1 + arg2 * );
} static PyMethodDef testMethods[] = {
{"test", test, METH_VARARGS, "This is test"},
{NULL, NULL}
}; PyMODINIT_FUNC inittest(){
Py_InitModule("test", testMethods);
}
gcc -I /usr/include/python2.7/ -fpic --shared -o test.so test.c
test.py
import test
print test.test(1, 2) # 输出 21
demo在github上
https://github.com/zhoudd1/python_call_c
python和C语言混编的几种方式
http://www.cnblogs.com/Colin-Cai/p/7594551.html
python与c语言交互应用实例的更多相关文章
- nodejs与c语言交互应用实例
nodejs与c/c++交互目前主流的方式有两种,node addon c++ 和 node-ffi . 1.node addon c++ 1)nodejs从c语言读取数据 addon.c #incl ...
- python ctypes 探究 ---- python 与 c 的交互
近几天使用 python 与 c/c++ 程序交互,网上有推荐swig但效果都不理想,所以琢磨琢磨了 python 的 ctypes 模块.同时,虽然网上有这方面的内容,但是感觉还是没说清楚.这里记录 ...
- [转]python ctypes 探究 ---- python 与 c 的交互
近几天使用 python 与 c/c++ 程序交互,网上有推荐swig但效果都不理想,所以琢磨琢磨了 python 的 ctypes 模块.同时,虽然网上有这方面的内容,但是感觉还是没说清楚.这里记录 ...
- 2015/9/20 Python基础(16):类和实例
面向对象编程编程的发展已经从简单控制流中按步的指令序列进入到更有组织的方式中,依靠代码块可以形成命名子程序和完成既定的功能.结构化的或过程性编程可以让我们把程序组织成逻辑快,以便重复或重用.创造程序的 ...
- UML标准建模语言与应用实例
一.基本信息 标题:UML标准建模语言与应用实例 时间:2012 出版源:科技创新导报 领域分类:UML标准建模语言 面向对象 系统分析与设计 二.研究背景 问题定义:UML建模语言用图形来表现典型的 ...
- 比较分析C++、Java、Python、R语言的面向对象特征,这些特征如何实现的?有什么相同点?
一门课的课后题答案,在这里备份一下: 面向对象程序设计语言 – 比较分析C++.Java.Python.R语言的面向对象特征,这些特征如何实现的?有什么相同点? C++ 语言的面向对象特征: 对象模 ...
- 总结:Python学习 和 Python与C/C++交互
本篇仅仅是Python的学习和Python和C++数据对接过程中的一些总结. 由于工作的需要,用一周的时间学习 Python. Python是基于C实现的一门解释型语言,由于其易用性,俘获了不少开发者 ...
- Python爬虫教程-17-ajax爬取实例(豆瓣电影)
Python爬虫教程-17-ajax爬取实例(豆瓣电影) ajax: 简单的说,就是一段js代码,通过这段代码,可以让页面发送异步的请求,或者向服务器发送一个东西,即和服务器进行交互 对于ajax: ...
- python 面向对象六 类属性和实例属性
一.实例属性 Python是动态语言,根据类创建的实例可以任意绑定属性. >>> class Student(object): ... def __init__(self, name ...
随机推荐
- 工作中Hadoop,Spark,Phoenix,Impala 集群中遇到坑及解决方案
1.HDFS 修复 问题描述:其他部门在yarn平台上跑spark 程序错误的生成了海量的不到100K的小文件,导致namenode压力过大,其中一个namenode宕机后,没有及时发现 使得edit ...
- gin入门
Download and install it: $ go get github.com/gin-gonic/gin Import it in your code: import "gith ...
- [android] AndroidManifest.xml - 【 manifest -> Application -> activity 】
<activity android:allowTaskReparenting=["true" | "false"] android:alwaysRetai ...
- i2c 异常之i2c1 prob 检测超时
在没加atl 的fpga 时 i2c1上的tvp5150 vpss驱动加载没问题, 加了之后出现超时 I2C: timed out in wait_for_bb: I2C_IRQSTATUS=1000 ...
- Think Python: How to Think Like a Computer Scientist
Think Python: How to Think Like a Computer Scientist:http://greenteapress.com/thinkpython/html/index ...
- mysql存储过程,获取指定数据库的某个表的字段信息
DROP PROCEDURE IF EXISTS Proc; DELIMITER //CREATE PROCEDURE Proc(database_name varchar(50),table_nam ...
- Excel随机生成数据
CONCATENATE函数是一个文本连接函数,非常简单,和&的效果一样. CONCATENATE是一个文本连接函数 语法:CONCATENATE(text1,text2,text3...... ...
- jQuery监控文本框事件并作相应处理的方法
本文实例讲述了jQuery监控文本框事件并作相应处理的方法.分享给大家供大家参考.具体如下: //事情委托 $(document) .on('input propertychange', '#que ...
- leetcode 153: Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Qt计算两个时间差
QTime startTime = QTime::currentTime(); QThread::msleep(SLEEP_TIME_MILL); QTime stopTime = QTime::cu ...