Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
来自arduino串行监视器的python 3 readline()两次_Python_Python 3.x_Arduino_Pyserial_Readlines - Fatal编程技术网

来自arduino串行监视器的python 3 readline()两次

来自arduino串行监视器的python 3 readline()两次,python,python-3.x,arduino,pyserial,readlines,Python,Python 3.x,Arduino,Pyserial,Readlines,我正在开发一个函数,该函数从arduino串行监视器读取一行数据 线路输出:Licht:870温度:19.01 该函数第一次工作时,但在我再次调用该函数后,它会读取一个空行 这是我的密码: import serial import time class Serializer: def __init__(self, port, baudrate=9600, timeout=2): self.port = serial.Serial(port = port, baudrate=baudra

我正在开发一个函数,该函数从arduino串行监视器读取一行数据 线路输出:Licht:870温度:19.01 该函数第一次工作时,但在我再次调用该函数后,它会读取一个空行

这是我的密码:

import serial
import time

class Serializer:
def __init__(self, port, baudrate=9600, timeout=2):
    self.port = serial.Serial(port = port, baudrate=baudrate,
                              timeout=timeout)

def open(self):
    ''' Open the serial port.'''
    self.port.open()

def close(self):
    ''' Close the serial port.'''
    self.port.close()

def write(self, msg):
    time.sleep(1.6)
    self.port.write(msg.encode())

def recv(self):
    return self.port.readline()
这是我获取温度或勒克斯的代码:

def getLux(self):
    lux = int(self.getTempLight()[1])
    print(lux)

def getTemp(self):
    temp = float(self.getTempLight()[3])
    print(temp)

def getTempLight(self):
    data =self.recv()
    data = str(data)
    list = data.split()
    return list
在调用函数getTemp之后,我想调用函数getLux()从与temp值相同的行中获取lux值

例如,从以下行: Licht:870温度:19.01


我想要870和19.01的值以及函数getTemp和getLux,如果在端口对象上执行
readline()
,它将消耗一行。也就是说,如果执行第二行
readline()
,它将返回arduino发送的下一行,如果没有第二行可用,则返回空字符串


因此,要么您的arduino连续发送值,要么您将读取行存储在
序列化程序
-对象中。

如果您在端口对象上预执行
readline()
,它将消耗一行。也就是说,如果执行第二行
readline()
,它将返回arduino发送的下一行,如果没有第二行可用,则返回空字符串

因此,要么您的arduino连续发送值,要么您将读取行存储在
序列化程序
-对象中