#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c)H.Ishikawa 2014/06/20
import serial
import threading #(1)
import time
import datetime
import csv
import RPi.GPIO as GPIO #(2)
#GPIO LED用(2)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO17 = 17
GPIO.setup(GPIO17,GPIO.OUT)
# (3)USB-Serialを使用する場合、ttyUSB0
con=serial.Serial('/dev/ttyUSB0', baudrate=38400, timeout=0.0,dsrdtr=True)
print ('measurement start ' + datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S.%f") + ' ' +con.portstr)
onCounts = 0 #(4)この変数でThreadの間で値をもちまわる
def readGM10(): #(5)
global onCounts
onCounts = 0
LEDCounts = 0
GPIO.output(GPIO17,GPIO.LOW)
while True:
str=con.read(100)
newCounts=len(str) #
onCounts = onCounts + newCounts
LEDCounts = LEDCounts + 1
if newCounts != 0: #(2) eventがあれば、LEDつける
GPIO.output(GPIO17,GPIO.HIGH)
LEDCounts = 0
if LEDCounts >= 100: #(2) 適当な時間でLED消す
GPIO.output(GPIO17,GPIO.LOW)
LEDCounts = 0
if __name__ == '__main__': #(6)
t=threading.Thread(target=readGM10)
t.setDaemon(True)
t.start()
while True:
time.sleep(59.94)# (7) 1分 値はカットアンドトライ
cpm = onCounts
onCounts = 0
print datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S.%f") , cpm
outputfile = open('geiger/cpm.txt' , "a" ) #(8) ファイル出力
writer = csv.writer(outputfile)
now = datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")
buffer = [now, cpm]
writer.writerow(buffer)
outputfile.close()
|