python调用C程序代码

DHT11的驱动使用C语言编写   然后用python调用C的程序 显示温湿度

 

pycall.py文件如下:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import RPi.GPIO as GPIO
import time
import DHT11

pin=14

while True:
hum,tem=DHT11.read_data(pin)
print('hum:%f, tem:%f' %(hum,tem))
time.sleep(2)

 

c语言代码如下:

#include
#include
#include

//2019.1.18 时序还没调试完成
//编译使用
//运行需要使用 g++ -Wall -o LEDtest LEDtest.cpp -lwiringPi

//数据定义 sudo ./a.out
unsigned int U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata;
unsigned int U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,U8RH_data_L_temp,U8checkdata_temp;

typedef unsigned char BOOL;
#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif

#define DHT11_PIN 14
typedef unsigned short int u8;
typedef unsigned char uint8_t;
typedef unsigned int u16;


void mdelay(u16 ms);


void udelay(u16 us);


static void DHT11_DataPin_Configure_Output(int pin)
{
pinMode(pin,OUTPUT);
}

static void DHT11_DataPin_Configure_Input(int pin)
{
pinMode(pin,INPUT);
}

BOOL DHT11_get_databit(int pin)
{
int val;
val=digitalRead (pin);
if(val == LOW){
return FALSE;
}else{
return TRUE;
}
}

void DHT11_set_databit(BOOL level,int pin)
{
if(level == TRUE){
digitalWrite (pin, HIGH);
}else{
digitalWrite (pin, LOW);
}
}

static uint8_t DHT11_read_byte(int pin)
{
uint8_t i;
uint8_t data = 0;
long k=0;
for(i = 0; i < 8; i++)
{
data <<= 1;
while((!DHT11_get_databit(pin)));
delayMicroseconds(35);
if(DHT11_get_databit(pin)){
data |= 0x1;
while(DHT11_get_databit(pin))
{
k++;
if(k>200000)
{
//printf("a \r\n");
return 0;
}
}
}else{

}
}
return data;
}

static uint8_t DHT11_start_sampling(int pin)
{
DHT11_DataPin_Configure_Output(pin);
//主机拉低18ms? ?
DHT11_set_databit(FALSE,pin);
delay(20);
DHT11_set_databit(TRUE,pin);
//总线由上拉电阻拉高 主机延时20us
//udelay(10);
//udelay(10);
DHT11_DataPin_Configure_Input(pin);
delayMicroseconds(50);
//主机设为输入 判断从机响应信号
DHT11_set_databit(TRUE,pin);

DHT11_DataPin_Configure_Input(pin);

//判断从机是否有低电平响应信号 如不响应则跳出,响应则向下运行?? ? ?
if(!DHT11_get_databit(pin))//T !?? ? ?
{
long k=0;
//判断从机是否发出 80us 的低电平响应信号是否结束?? ?
while((!DHT11_get_databit(pin)))
{
k++;
if(k>100000) {printf("b \r\n");return 0;}
}
// printf("DHT11 answers.\r\n");
//判断从机是否发出 80us 的高电平,如发出则进入数据接收状态
k=0;
while((DHT11_get_databit(pin)))
{
k++;
if(k>100000){printf("c \r\n");;return 0;}
}
return 1;
}

return 0;
}

typedef struct
{
unsigned int hh;
unsigned int hl;
unsigned int th;
unsigned int tl;
}d_t;

d_t DHT11_get_data(int pin)
{
u8 temp;
d_t res;
if(DHT11_start_sampling(pin))
{
//printf("DHT11 is ready to transmit data\r\n");
//数据接收状态
U8RH_data_H_temp = DHT11_read_byte(pin);

U8RH_data_L_temp = DHT11_read_byte(pin);
res.hh=
U8T_data_H_temp = DHT11_read_byte(pin);
res.th=
U8T_data_L_temp = DHT11_read_byte(pin);
U8checkdata_temp = DHT11_read_byte(pin);

/* Data transmission finishes, pull the bus high */
DHT11_DataPin_Configure_Output(pin);
DHT11_set_databit(TRUE,pin);
//数据校验

temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp);
if(temp==U8checkdata_temp)
{
U8RH_data_H=U8RH_data_H_temp;
res.hh=U8RH_data_H;
U8RH_data_L=U8RH_data_L_temp;
res.hl=U8RH_data_L;
U8T_data_H=U8T_data_H_temp;
res.th=U8T_data_H;
U8T_data_L=U8T_data_L_temp;
res.tl=U8T_data_L;
U8checkdata=U8checkdata_temp;

//printf("DHT11 tempature %d.%d humidity %d.%d \r\n",U8T_data_H,U8T_data_L,U8RH_data_H,U8T_data_L);
}
else
{
//printf("checksum failure \r\n");
res.hh=0;res.hl=0;res.th=0;res.tl=0;
}
}
else
{
printf("DHT11 didn't answer. Sampling failed. \r\n");
}
return res;
}

void mdelay(u16 ms)
{
if(ms != 0){
delay(ms);
}
}

void udelay(u16 us)
{
while(us--);
}
/*
int main(void)
{
int pin=14
int res=wiringPiSetupGpio();
while(1)
{
DHT11_get_data(pin);
mdelay(1200);
}
}
*/
//
//使用python来调用c写的函数
//
//

#include "Python.h"
static DHT_read_data(PyObject* self,PyObject* args)
{
int pin=0;
if(PyArg_ParseTuple(args,"i",&pin)==0)
return NULL; //失败
d_t res=DHT11_get_data(pin);

float hum=res.hh+res.hl*1.0/100;
float tem=res.th+res.tl*1.0/100;

return Py_BuildValue("dd",hum,tem);
}

static PyMethodDef DHT11_methods[]=
{
//注册
{"read_data",DHT_read_data,METH_VARARGS}
};

void initDHT11()
{
//模块初始化函数
printf("dht11\n");
wiringPiSetupGpio();
Py_InitModule("DHT11",DHT11_methods);
}

//python setup.py build
//sudo python setup.py install

 

 

 

 

setup.py文件内容如下:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

from distutils.core import setup,Extension

DHT11_Extension=Extension('DHT11',sources=['main.c'],libraries=['wiringPi'])

setup(name='DHT11',ext_modules=[DHT11_Extension])

 单单编译一个C文件的话可以使用gcc main.c -lwiringPi

然后编译C文件 需要先编写setup.py 内容如上

 python  setup.py build 用于编译文件  会出现一些警告信息  可以忽略不理会
 
 再使用这个 sudo python setup.py install 把这个模块安装到python
 
然后再使用sudo python pycall.py  如下图显示:
python调用C程序代码_第1张图片
 
 
 
 
 
 

转载于:https://www.cnblogs.com/peter83ye/p/10293305.html

你可能感兴趣的:(python调用C程序代码)