Arduino到Python:如何使用ser.readline()将读数导入到具有指定起点的列表中?
这是一个非常具体的问题,所以请容忍我 我有14个超声波传感器连接到Arduino上,向串行监视器发送实时读数(插入时为Pi)。读数按如下方式发送,每2位之间有一条新行(Z除外) Z 62 61 64 63 64 67 98 70 69 71 90 XX 75 XX 这些测量单位为厘米。“XX”表示读数超出两位数范围。Z被指定为起始点,因为pi以非常快的速度重复读取传感器,大约在一秒钟内读取80个读数。因此,ser.readline()给出了相同传感器的多个样本 当python读取ser.readline()中的读数时,它没有起点。它可以从70、XX或Z开始。我想将其分配到一个可访问列表中,以便: 数组[0]=Z(始终) 数组[1]=62(前两位) 数组[2]=61(第二个两位数) 数组[14]=XX(第十四个两位数) 这是我的代码,不幸的是它无法工作,因为列表超出了范围:Arduino到Python:如何使用ser.readline()将读数导入到具有指定起点的列表中?,python,arrays,raspberry-pi,uart,usart,Python,Arrays,Raspberry Pi,Uart,Usart,这是一个非常具体的问题,所以请容忍我 我有14个超声波传感器连接到Arduino上,向串行监视器发送实时读数(插入时为Pi)。读数按如下方式发送,每2位之间有一条新行(Z除外) Z 62 61 64 63 64 67 98 70 69 71 90 XX 75 XX 这些测量单位为厘米。“XX”表示读数超出两位数范围。Z被指定为起始点,因为pi以非常快的速度重复读取传感器,大约在一秒钟内读取80个读数。因此,ser.readline()给出了相同传感器的多个样本 当python读取ser.read
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
导入序列号
ser=serial.serial('/dev/ttyACM0',115200)
打印(“开始”)
总计数=1#初始化2个计数器
arraycount=1
数组=[]#初始化2个列表
行=[]
尽管如此:
总计数这个怎么样?请注意,您的支票总数非常感谢!它工作没有错误,我已经通过打印阅读测试了它们。祝你有一个美好的一天,先生/妈妈那太好了,因为我还没有试过运行它。另外,你能接受这个答案吗?谢谢,祝你的项目好运!
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