python serial data - incoming data according to previous line result -
i receive data serial (pyserial) line line.
is possible put condition each new incoming line according previous line result ? exemple :
data3 == int(incoming_data_serial) + int(data2) this current code editing previous answer deleted author. works fine. gives result :
[63, 0] [64, 63] [64, 64] [63, 64] [63, 63] etc... which promising. still need know how incorporate operator (substract exemple) between datas. exemple : instead of [64, 63] 1 data of 64 - 63 meaning 1 !
this code :
#!/usr/bin/python import serial import time ser = serial.serial('/dev/ttys1', 9600, timeout=0.1) class _share: def __init__(self): self.last_val = [0 in range(2)] def calculate(self, val): #prepare data, convert self.last_data = val self.last_val = [self.last_data] + self.last_val[:-1] print((self.last_val)) return self.last_val share = _share() def sensors(theimput): while true: try: time.sleep(0.01) ser.flushinput() sensor_reception = ser.readline() sensor_reception_split = sensor_reception.split() #data_sensor_milli = int(receptionsplit[3]) data_sensor_pho_1 = int(sensor_reception_split[2]) #data_sensor_tem_1 = int(receptionsplit[1]) #data_sensor_hum_1 = int(receptionsplit[0]) return str(share.calculate(data_sensor_pho_1)) except: pass time.sleep(0.1) f = open('da.txt', 'ab') while 1: arduino_sensor = sensors('1') f.write(arduino_sensor) f.close() f = open('da.txt', 'ab')
#ser serial object #data variable u want update def sensors(last_data): temp = [] while true: c = ser.read() if not c: time.sleep(0.1) continue if c == '\n' or c == '\r': break temp.append(c) temp = ''.join(temp) print('raw readings:', temp) new_data = [int(d) d in temp.strip().split())] data = [a+b a, b in zip(last_data, new_data)] print('last data: ', last_data) print('new data:', new_data) print('current sum:', data) last_data = [i in new_data] return data, last_data last_data = [0] * 4 f = open('da.txt', 'a') while true: try: sensor_data, last_data = sensors(last_data) #last_data = [i in last_data] #uncomment if code doesnt work f.write(sensor_data.__str__() + '\n') except: f.close() this code assumes want add last received line of sensor data current line of sensor data. not running sum.
Comments
Post a Comment