PySerial readline函数偶尔开始读取Midstring?

2024-09-30 20:23:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Python从一个Arduino传感器读取数据,使用python3中的PySerial。我在使用Anaconda,并在其中使用Spyder来测试我的代码,因为Jupyter不能处理我计划使用的一些包。在

大约有一半的时间,我的代码运行得很好,我的输出看起来也很正常(从UTF-8解码出288个逗号分隔的值,如下所示:

135,134,111,111,110,99,111,109,108,107,111,109,110,115,111,109,104,113,109,110,95,110,111,109,110,111,109,108,114,109,110,99,111,108,108,103,111,110,111,121,109,110,108,113,112,111,95,116,116,120,116,124,124,121,127,121,116,112,117,115,114,107,113,112,109,124,111,111,112,114,113,112,79,115,112,112,110,114,114,114,119,118,115,112,119,115,116,109,116,116,114,122,117,114,116,118,115,116,95,116,117,114,145,115,114,114,119,115,114,103,117,114,114,109,115,114,114,118,117,114,103,118,114,112,110,113,115,113,117,115,111,112,115,111,112,107,113,111,111,130,114,109,110,113,111,113,103,111,111,108,135,112,109,111,113,111,112,99,113,110,110,107,110,110,110,118,111,109,104,114,110,110,95,110,110,110,153,112,109,108,113,108,112,99,112,111,108,105,112,110,110,113,109,111,104,113,112,107,95,110,110,109,122,110,111,108,113,110,109,79,111,107,110,103,111,109,109,115,109,108,99,110,111,110,103,111,107,109,121,110,109,108,113,111,110,95,111,109,110,147,110,110,107,113,111,107,99,111,111,110,103,111,110,107,115,110,109,104,110,109,108,95,113,111,109,162,111,108,110,111,107,110,79,111,109,110,105,109,111,110,111,111,109,104,112,110,110,105

有一半的时间我会遇到各种各样的错误,我猜是基于这样一个事实:它似乎是在字符串开始之后启动readline函数的(只是猜测,因为错误的字符串有时以逗号开头)。例如,输出和错误消息如下:

,127,142,140,136,143,137,134,129,137,135,136,127,136,135,134,139,136,134,129,137,134,135,178,135,131

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\WorkCpu\Anaconda3\envs\Python35\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)
  File "C:\Users\WorkCpu\Anaconda3\envs\Python35\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)
  File "C:/Users/WorkCpu/PyQtGraphSensorSpyderCode.py", line 86, in <module>
    main()
  File "C:/Users/WorkCpu/PyQtGraphSensorSpyderCode.py", line 77, in main
    y = np.array(yValues).astype(np.int)
ValueError: invalid literal for int() with base 10: ''

错误输出代码的另一个例子是:

^{pr2}$

所以我想我的问题是:我的诊断正确吗?如果是这样的话,有没有办法强迫PySerial等待开始读取,直到字符串中的起始字符被输出?在

我的代码:

import warnings
import serial
import serial.tools.list_ports
import time
import pyqtgraph as pg
import numpy as np

def main():

#This sets the com port in PySerial to the port with the Genuino as the variable arduino_ports
    arduino_ports = [
        p.device
    for p in serial.tools.list_ports.comports()
    if 'Genuino' in p.description
    ]

    #Set the proper baud rate for your arduino - which is 115200 (value is set in the Ardunio code)
    baud =  115200

    #This prints out the port that was found with the Genuino on it
    ports = list(serial.tools.list_ports.comports())
    for p in ports:
        print ('Sensor is connected to: ', p)


    # --------------------------- Error Handling ---------------------------
    #Tell the user if no Genuino was found
    if not arduino_ports:
        raise IOError("No Arduino found")
    #Tell the user if multiple Genuinos were found
    if len(arduino_ports) > 1:
        warnings.warn('Multiple Arduinos found - using the first')
    # ---------------------------- Error Handling ---------------------------

    ##Run the following two times:
    ##for i in range(2):

    #Assuming there was one Genuino connected (ie. the sensor), create a serial connection to that port
    sensor = serial.Serial(arduino_ports[0], baud)

    #Read one line of data from the sensor
    line = sensor.readline()

    lineDecoded = line.decode("UTF-8") 
    lineDecoded = lineDecoded[:-2] #Index -2 removes newline character and ending comma

    #Print the line of data you read from the sensor as a string

    print(lineDecoded) 


    #Set the X and Y variables:

    #Set X values to static values
    x = range(1,289)


    yValues = lineDecoded.split(",")
    #Set Y to the values from the sensor
.
    y = np.array(yValues).astype(np.int)

    #Plot the data
    pg.plot(x, y)


    #Close the sensor's serial connection
    sensor.close()

main()

编辑:我根据下面的注释测试了输出,在PySerial输出中出现了非常奇怪的行为。以下是我从几次不同运行中得到的结果(完全相同的代码):

['183', '135', '108', '99', '113', '109', '109', '105', '110', '111', '109', '113', '110', '109', '99', '111', '109', '110', '104', '111', '111', '108', '115', '109', '109', '103', '111', '109', '110', '105', '111', '109', '108', '113', '108', '108', '79', '111', '111', '108', '163', '110', '108', '108', '113', '108', '110', '95', '109', '110', '108', '124', '111', '108', '108', '111', '108', '111', '63', '111', '111', '108', '115', '109', '109', '104', '111', '110', '109', '103', '111', '109', '109', '114', '109', '109', '99', '110', '110', '109', '103', '112', '108', '108', '114', '108', '111', '103', '111', '110', '109', '107', '111', '108', '108', '112', '109', '111', '95', '111', '111', '108', '119', '109', '108', '104', '113', '110', '110', '103', '111', '109', '110', '114', '110', '110', '103', '110', '111', '109', '123', '113', '108', '108', '114', '109', '111', '95', '111', '110', '107', '163', '111', '108', '104', '111', '110', '109', '103', '111', '111', '106', '115', '109', '108', '99', '111', '110', '108', '103', '111', '108', '108', '113', '109', '109', '79', '110', '109', '109', '152', '111', '109', '108', '111', '111', '110', '103', '111', '111', '109', '114', '110', '108', '99', '111', '110', '111', '103', '110', '111', '108', '115', '110', '109', '103', '111', '109', '109', '122', '111', '109', '108', '114', '109', '110', '95', '110', '111', '108', '140', '111', '109', '104', '112', '109', '108', '105', '111', '110', '109', '112', '110', '109', '104', '111', '111', '110', '103', '110', '109', '107', '111', '109', '109', '103', '109', '110', '111', '127', '111', '109', '110', '113', '111', '109', '63', '110', '111', '109', '115', '109', '108', '104', '112', '109', '110', '105', '111', '110', '108', '114', '110', '108', '103', '111', '109', '110', '105', '111', '109', '108', '115', '110', '110', '95', '111', '109', '109', '118', '110', '109', '104', '113', '110', '108', '105', '110', '111', '108', '114', '111', '109', '104', '112', '109', '110', '103', '\n']

['130', '135', '209', '207', '209', '201', '205', '193', '202', '199', '198', '218', '207', '193', '188', '193', '189', '190', '183', '195', '190', '185', '193', '188', '182', '192', '187', '190', '183', '217', '187', '186', '184', '191', '203', '207', '214', '223', '236', '234', '281', '343', '448', '626', '824', '976', '988', '989', '981', '987', '992', '980', '979', '992', '984', '982', '1023', '987', '984', '985', '988', '978', '1008', '996', '985', '983', '982', '972', '974', '984', '987', '985', '1002', '988', '982', '992', '990', '985', '992', '987', '984', '988', '985', '980', '983', '988', '984', '984', '992', '984', '978', '1008', '986', '985', '992', '992', '984', '992', '984', '977', '984', '984', '981', '975', '996', '985', '984', '988', '986', '983', '1010', '988', '981', '984', '987', '980', '984', '982', '985', '989', '988', '983', '991', '988', '978', '981', '992', '987', '984', '1023', '972', '979', '980', '982', '982', '992', '989', '983', '990', '987', '982', '983', '994', '977', '977', '992', '980', '987', '1023', '980', '980', '982', '982', '977', '992', '977', '958', '940', '916', '889', '851', '840', '807', '778', '813', '737', '716', '704', '676', '656', '639', '631', '604', '590', '598', '553', '545', '525', '515', '499', '497', '482', '477', '461', '450', '446', '437', '432', '432', '425', '424', '411', '410', '408', '400', '405', '399', '398', '423', '390', '388', '375', '383', '384', '379', '389', '385', '374', '367', '372', '376', '363', '370', '362', '366', '361', '369', '365', '364', '366', '365', '353', '351', '358', '359', '355', '360', '356', '357', '346', '350', '355', '345', '349', '335', '338', '335', '336', '342', '331', '327', '323', '319', '311', '322', '310', '311', '321', '304', '305', '305', '299', '293', '284', '297', '296', '280', '279', '279', '274', '270', '274', '273', '265', '266', '283', '270', '263', '268', '255', '257', '258', '263', '260', '249', '255', '255', '245', '240', '250', '242', '242', '274', '241', '243', '236', '255', '242', '\n']

['0', '169', '121', '118', '116', '122', '120', '120', '111', '120', '119', '117', '123', '119', '118', '95', '119', '119', '119', '139', '120', '118', '\n']

['135', '134', '213', '217', '211', '204', '214', '208', '206', '203', '211', '203', '213', '207', '199', '200', '192', '201', '197', '203', '211', '196', '196', '192', '194', '199', '192', '194', '195', '191', '192', '196', '195', '195', '201', '213', '222', '222', '235', '228', '249', '281', '340', '440', '546', '683', '846', '976', '1023', '990', '984', '984', '985', '981', '992', '989', '985', '990', '983', '960', '905', '859', '812', '772', '788', '778', '791', '832', '865', '900', '952', '981', '977', '983', '989', '990', '986', '993', '988', '982', '996', '982', '980', '1023', '984', '981', '986', '988', '981', '992', '984', '978', '990', '990', '987', '987', '992', '982', '978', '985', '981', '980', '996', '980', '988', '987', '988', '979', '992', '988', '986', '990', '987', '980', '983', '986', '982', '979', '992', '986', '981', '992', '990', '980', '1023', '985', '983', '988', '988', '983', '992', '984', '977', '984', '988', '983', '983', '989', '985', '982', '990', '985', '985', '990', '981', '983', '980', '990', '984', '981', '985', '980', '992', '983', '989', '990', '983', '975', '967', '964', '928', '899', '886', '857', '834', '800', '781', '748', '736', '717', '688', '679', '656', '634', '619', '614', '582', '565', '544', '537', '531', '506', '506', '491', '484', '492', '465', '454', '453', '449', '448', '435', '441', '425', '420', '415', '417', '412', '405', '413', '407', '398', '393', '395', '394', '377', '404', '387', '389', '415', '388', '375', '369', '373', '378', '375', '392', '372', '377', '365', '377', '366', '373', '374', '373', '363', '363', '366', '362', '359', '359', '365', '359', '348', '344', '349', '344', '344', '350', '343', '331', '335', '329', '324', '336', '321', '324', '325', '315', '316', '320', '313', '307', '298', '335', '310', '292', '296', '292', '287', '285', '281', '286', '279', '273', '288', '283', '274', '267', '266', '266', '265', '275', '269', '259', '255', '268', '258', '252', '261', '254', '252', '249', '253', '252', '250', '269', '252', '\n']

我也收到过几次“无效起始字节”错误。arduino只有一个10位的ADC,所以最后的输出没有意义。。。有人见过这样的事吗?在


Tags: thetoinimportforif错误np