python
1from os import close, path 2from types import ClassMethodDescriptorType 3import serial 4import time 5from datetime import datetime 6import sys 7import csv 8 9class Mysensor: 10 def sensorfnc(self): 11 12 #項目の入力(1回のみの処理) #行間隔をなくす 13 with open('abc.csv','a',newline='') as f: 14 writer = csv.writer(f) 15 writer.writerow(['日付時間','気温','湿度','明るさ','気圧','騒音','eTCVOC','CO2','不快指数','熱中症飽和度','振動情報','SI値','加速度','震度']) 16 17 18 def s16( self, value): 19 return -(value & 0x8000) | (value & 0x7fff) 20 21 def calc_crc( self, buf, length): 22 """ 23 CRC-16 calculation. 24 25 """ 26 crc = 0xFFFF 27 for i in range(length): 28 crc = crc ^ buf[i] 29 for i in range(8): 30 carrayFlag = crc & 1 31 crc = crc >> 1 32 if (carrayFlag == 1): 33 crc = crc ^ 0xA001 34 crcH = crc >> 8 35 crcL = crc & 0x00FF 36 return (bytearray([crcL, crcH])) 37 38 #データの抽出 39 def print_latest_data(self, data , path): 40 """ 41 print measured latest value. 42 """ 43 time_measured = datetime.now().strftime("%Y/%m/%d %H:%M:%S") 44 temperature = str( self.s16(int(hex(data[9]) + '{:02x}'.format(data[8], 'x'), 16)) / 100) 45 relative_humidity = str(int(hex(data[11]) + '{:02x}'.format(data[10], 'x'), 16) / 100) 46 ambient_light = str(int(hex(data[13]) + '{:02x}'.format(data[12], 'x'), 16)) 47 barometric_pressure = str(int(hex(data[17]) + '{:02x}'.format(data[16], 'x') 48 + '{:02x}'.format(data[15], 'x') + '{:02x}'.format(data[14], 'x'), 16) / 1000) 49 sound_noise = str(int(hex(data[19]) + '{:02x}'.format(data[18], 'x'), 16) / 100) 50 eTVOC = str(int(hex(data[21]) + '{:02x}'.format(data[20], 'x'), 16)) 51 eCO2 = str(int(hex(data[23]) + '{:02x}'.format(data[22], 'x'), 16)) 52 discomfort_index = str(int(hex(data[25]) + '{:02x}'.format(data[24], 'x'), 16) / 100) 53 heat_stroke = str(self.s16(int(hex(data[27]) + '{:02x}'.format(data[26], 'x'), 16)) / 100) 54 vibration_information = str(int(hex(data[28]), 16)) 55 si_value = str(int(hex(data[30]) + '{:02x}'.format(data[29], 'x'), 16) / 10) 56 pga = str(int(hex(data[32]) + '{:02x}'.format(data[31], 'x'), 16) / 10) 57 seismic_intensity = str(int(hex(data[34]) + '{:02x}'.format(data[33], 'x'), 16) / 1000) 58 59 #数値をcsvファイルに入力 60 with open('abc.csv','a',newline='') as f: 61 writer = csv.writer(f) 62 writer.writerow([time_measured,temperature,relative_humidity,ambient_light,barometric_pressure,sound_noise,eTVOC,eCO2,discomfort_index,heat_stroke,vibration_information,si_value,pga,seismic_intensity]) 63 64 def now_utc_str( self ): 65 """ 66 Get now utc. 67 """ 68 return datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S") 69 70 71 # if __name__ == '__main__': 72 def sensormain(self): 73 # LED display rule. Normal Off. 74 DISPLAY_RULE_NORMALLY_OFF = 0 75 76 # LED display rule. Normal On. 77 DISPLAY_RULE_NORMALLY_ON = 1 78 79 # Serial. 80 ser = serial.Serial("/dev/ttyUSB0", 115200, serial.EIGHTBITS, serial.PARITY_NONE) 81 82 try: 83 # LED On. Color of Green. 84 command = bytearray([0x52, 0x42, 0x0a, 0x00, 0x02, 0x11, 0x51, DISPLAY_RULE_NORMALLY_ON, 0x00, 0, 255, 0]) 85 command = command + self.calc_crc(command, len(command)) 86 ser.write(command) 87 time.sleep(0.1) 88 ret = ser.read(ser.inWaiting()) 89 90 while ser.isOpen(): 91 # Get Latest data Long. 92 command = bytearray([0x52, 0x42, 0x05, 0x00, 0x01, 0x21, 0x50]) 93 command = command + self.calc_crc(command, len(command)) 94 tmp = ser.write(command) 95 time.sleep(0.1) 96 data = ser.read(ser.inWaiting()) 97 self.print_latest_data(data) 98 time.sleep(1) 99 100 except KeyboardInterrupt: 101 # LED Off. 102 command = bytearray([0x52, 0x42, 0x0a, 0x00, 0x02, 0x11, 0x51, DISPLAY_RULE_NORMALLY_OFF, 0x00, 0, 0, 0]) 103 command = command + self.calc_crc(command, len(command)) 104 ser.write(command) 105 time.sleep(1) 106 # script finish. 107 sys.exit 108 close('abc.csv') 109 110 111
センサからデータを取得しファイルに書き込んで保存するコードなのですが、ファイルにpathを渡して引数として設定したいです(abc.csvを引数にしてどんなファイルでも大丈夫なようにしたい)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/09/06 04:39
2021/09/06 04:47
2021/09/06 04:47
2021/09/06 04:54
2021/09/06 10:03