python串行数据 - 根据前一行结果传入的数据

时间:2016-02-06 13:38:26

标签: python

我逐行接收串行(pyserial)数据。

是否可以根据前一行结果为每个新的输入行设置条件?例如:

data3 == int(incoming_data_serial) + int(data2)

这是我正在编辑的当前代码,其作者删除了之前的答案。它工作正常。它给出了这样的结果:

[63, 0]
[64, 63]
[64, 64]
[63, 64]
[63, 63]
etc...

这很有希望。但是我仍然需要知道如何在这些数据之间加入运算符(例如减法)。 例子:而不是[64,63]我只想得到一个64 - 63的数据,意思是1!

这是我的代码:

#!/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')

2 个答案:

答案 0 :(得分:0)

#ser is the serial object
#data is the variable u want to 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) for d in temp.strip().split())]  
    data = [a+b for 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 for 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 for i in last_data]   #uncomment this if the code doesnt work as is
        f.write(sensor_data.__str__() + '\n')
    except:
        f.close()

此代码假定您要将最后接收的传感器数据行添加到当前传感器数据行。这不是一笔运行金额。

答案 1 :(得分:-1)

解决方案!

#!/usr/bin/python

import serial
import time
import operator

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]
    b = reduce(operator.__sub__, self.last_val)
    print((b))
    return b

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')