c调用python脚本 载入脚本失败_从C ++调用Python脚本时出现分段错误

#!/usr/bin/env python2.7

# adapted from script by Alex Eames http://RasPi.tv

import time

import os

import subprocess

import smtplib

import string

import RPi.GPIO as GPIO

from time import gmtime, strftime

GPIO.setmode(GPIO.BCM)

#DEFINITIONS

#ADC channels

ULTRA_ADC_CH = 0

GAS_ADC_CH = 1

#Other definitions

adc_ch = [ULTRA_ADC_CH,GAS_ADC_CH] # Particular ADC channel to read

reps = 10 # how many times to take each measurement for averaging

time_between_readings = 1 # seconds between clusters of readings

V_REF = 5.0 #reference voltage

ultra_conv_factor=1./(0.0098/2.54) #Ultrasonic factor: cm/mV

# Define Pins/Ports

SPICLK = 22 # FOUR SPI ports on the ADC

SPIMISO = 5

SPIMOSI = 6

SPICS = 26

SPICS2 = 16

# read SPI data from MCP3002 chip, 2 possible adc channels (0 & 1)

# this uses a bitbang method rather than Pi hardware spi

def readadc(adc_ch, clockpin, mosipin, misopin, cspin):

if ((adc_ch > 1) or (adc_ch < 0)):

return -1

if (adc_ch == 0):

commandout = 0x6

else:

commandout = 0x7

GPIO.output(cspin, True)

GPIO.output(clockpin, False) # start clock low

GPIO.output(cspin, False) # bring CS low

commandout <<= 5 # we only need to send 3 bits here

for i in range(3):

if (commandout & 0x80):

GPIO.output(mosipin, True)

else:

GPIO.output(mosipin, False)

commandout <<= 1

GPIO.output(clockpin, True)

GPIO.output(clockpin, False)

adcout = 0

# read in one empty bit, one null bit and 10 ADC bits

for i in range(12):

GPIO.output(clockpin, True)

GPIO.output(clockpin, False)

adcout <<= 1

if (GPIO.input(misopin)):

adcout |= 0x1

GPIO.output(cspin, True)

adcout /= 2 # first bit is 'null' so drop it

return adcout

#MAIN FUNCTION: to get ultrasonic and gas sensor data

def main():

reps=10

#Set up ports

GPIO.setup(SPIMOSI, GPIO.OUT) # set up the SPI interface pins

GPIO.setup(SPIMISO, GPIO.IN)

GPIO.setup(SPICLK, GPIO.OUT)

GPIO.setup(SPICS, GPIO.OUT)

try:

for adc_channel in adc_ch: #adc_ch is the channel number

adctot = 0

# read the analog value

for i in range(reps): #Read same ADC repeatedly for # REPS

read_adc = readadc(adc_channel, SPICLK, SPIMOSI, SPIMISO, SPICS)

adctot += read_adc

#time.sleep(0.01) #Minimum 11.5us limit for acquisition & conversion

read_adc = adctot / reps / 1.0 # Take average value

# print read_adc

# convert analog reading to Volts = ADC * ( V_REF / 1024 )

volts = read_adc * ( V_REF / 1024.0)

# convert voltage to measurement

if (adc_channel==ULTRA_ADC_CH):

ultra_dist = volts * ultra_conv_factor

if ultra_dist < 50: # Filtering to reduce effect of noise below 50cm

reps = 100

else:

reps = 10

#print "\nUltrasonic distance: %d" %ultra_dist

elif (adc_channel==GAS_ADC_CH):

gas_val_percent = volts / V_REF *100

#print "Gas density: %d\n" %gas_val_percent

#time.sleep(time_between_readings)

except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt

GPIO.cleanup()

GPIO.cleanup()

return (ultra_dist, gas_val_percent)

# To call main function

if __name__ == '__main__':

main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值