I receive data from serial (pyserial) line by line.
Is it possible to put a condition for each new incoming line according to the previous line result ? exemple :
data3 == int(incoming_data_serial) + int(data2)
This is my current code I am editing with a previous answer deleted by its author. It works fine. It gives result like this :
[63, 0]
[64, 63]
[64, 64]
[63, 64]
[63, 63]
etc...
which is promising. But still I need to know how to incorporate an operator (substract for exemple) between those datas. Exemple : instead of [64, 63] I would like to get only one data of 64 - 63 meaning 1 !
This is my 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 for i 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')