简体   繁体   English

Arduino至Python:如何使用ser.readline()将读数导入具有指定起点的列表中?

[英]Arduino to Python: How to import readings using ser.readline() into a list with a specified starting point?

This is quite a specific query so please bear with me. 这是一个非常具体的查询,请耐心等待。

I have 14 ultrasonic sensors hooked to an Arduino sending live readings to the serial monitor (or Pi when I plug it in) . 我有14个超声波传感器连接到Arduino,将实时读数发送到串行监视器(或在插入时插入Pi)。 The readings are sent as follows, with a new line between every 2 digits (except Z). 读数发送方式如下, 每2位数字之间换行 (Z除外)。

Z 62 61 64 63 64 67 98 70 69 71 90 XX 75 XX Z 62 61 64 63 64 67 98 70 69 71 90 XX 75 XX

These measurements are in cm. 这些测量单位为厘米。 "XX" implies the reading is out of the two digit range. “ XX”表示读数超出两位数范围。 Z has been assigned as a starting point as the pi reads the sensors very fast and repetitively, to the point of 80 readings in a second or so. 当pi非常快速且重复地读取传感器时,Z被指定为起点,直到大约一秒钟达到80读数。 So ser.readline() gives multiple samples of the same sensors 所以ser.readline()给出了相同传感器的多个样本

When python reads the readings in ser.readline() it does not have a starting point. 当python在ser.readline()中读取读数时,它没有起点。 It may start at 70, XX or Z. I want to assign it into an accessible list so that: 它可能以70,XX或Z开始。我想将其分配到可访问列表中,以便:

array [0] = Z (always) 数组[0] = Z (始终)

array [1] = 62 (first two digits) 数组[1] = 62 (前两位)

array [2] = 61 (second two digits) 数组[2] = 61 (后两位)

.. ..

array [14] = XX (fourteenth two digits) 数组[14] = XX (第14个两位数字)

This is my code which unfortunately doesn't work as list is out of range: 不幸的是,这是我的代码不起作用,因为列表超出范围:

import serial
ser = serial.Serial('/dev/ttyACM0',115200)

print ("Start")

overallcount=1 #initialise 2 counters
arraycount =1
array = [] #initialise 2 lists
line = []

while True:
    while overallcount<30: #read 30 random readings from Arduino
        ser.readline()      
        print(str(overallcount)) #print reading number
        while arraycount<15:     #Number of readings to fill the array to be made
            for line in ser.readline():
                if line == 'Z':         #If element in ser.readline is "Z"
                    array[0] == line    #Assign first list element as Z (starting point)              
                arraycount=arraycount+1 #Iterate through until 14 sensors are read
            arraycount=1                #reset counter
        overallcount=overallcount+1     #Iterate through 30 random Arduino readings
    overallcount=1                      #iterate random counter

If you could please tell me what I'm doing wrong, or if there is a better method for this I'd really really appreciate it! 如果您能告诉我我在做什么错,或者有更好的方法可以,我将非常感谢!

Thank you 谢谢

How about this? 这个怎么样? Note that your checks overallcount<30 and arraycount<15 should really be overallcount<=30 and arraycount<=15. 请注意,您的检查totalcountcount <30和arraycount <15确实应该是totalcountcount <= 30和arraycount <= 15。

import serial
ser = serial.Serial('/dev/ttyACM0',115200)

readings = [] # Array to store arrays of readings
reading_id = 1 # Id of current reading
random_lines_expected = 30 # NUmber of random lines
num_sensors = 14 # Number of sensors

def read_random():
    for _ in range(random_lines_expected):
        ser.readline() 

read_random() # Read initial random lines
while True:
    print "Reading #", reading_id
    reading = [] # Initialize an array to collect new reading
    while ser.readline().strip() != 'Z': # Keep reading lines until we find 'Z'
        pass
    reading.append('Z') # Add Z to reading array
    for _ in range(num_sensors): # For 14 sensors...
        reading.append(ser.readline().strip()) # Add their value into array
    readings.append(reading) # Add current reading to the array or readings
    reading_id += 1 # Increment reading ID
    #read_random() #Uncomment this if random follows each series of readings

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM