How to process loops from Arduino

I’ve managed to import the data from the Arduino Serial Monitor into python, but when I try and assign certain indices of a list, it gives me an “index out of bounds” error for anything other than 0. The code is as follows:

import serial 
import matplotlib.pyplot as plt 
import pandas as pd
     
ser = serial.Serial('/dev/cu.usbmodem1301', 9600)  # Adjust COM port and baud rate 
     
fig, ax = plt.subplots() 

while True: 
    line = ser.readline().decode('utf-8').rstrip()
    data = line.split(";")
    print(data)
    x_angle = data[0]
    x_accel = data[1]
    y_angle = data[2]
    y_accel = data[3]
    z_angle = data[4]
    z_accel = data[5]

The serial monitor has roughly 200 lines of the sensor data, which looks like this:

-40.41;0.03;29.66;-0.94;66.96;0.34
 78

The top line is the data, and the bottom number acts as a loop clock. How can I process this data in real time, without needing to hit enter for every iteration of the loop? The data list is correct but only shows once, and I get the “list index out of range” error on the x_accel line. Any help is appreciated!

Well, you get this error because the line next to the line with ;-s (the one you have called “a loop clock”) contains only one value.

Try checking, before you read a line into variables x_anglez_accel, that your data has got the right size:

if len(data) == 6:
    x_angle = data[0]
    #and so on...

I don’t understand. As far as I can tell, there is already no reason why you would need to “hit enter for every iteration of the loop”; the code you show would proceed automatically.

You need to press enter when you use the built-in input, because it’s specifically waiting for you to complete a line of text and allowing you to make edits. But if your data comes from ser.readline(), then the only reason it’s waiting is because the connected device isn’t done writing yet.

This has nothing to do with any hypothetical “need to hit enter”.

Think carefully about the logic of the code. You say that you fundamentally have two different kinds of lines in what you read - yes? The data lines, and the “loop clock” lines? So - does your code attempt to treat them differently? Is it aware that there will be “loop clock” lines? (What rule should it use in order to detect those? What should it do when it finds one?) Consider what happens if you have a line that just says 78, and the rest of the code proceeds line by line. What result do you expect to get from line.split(';')? Will it be possible to do data[5] after that? (Or data[1], for that matter?)

Start with the basics, and solve just one thing first. Start your debugger and look at the value of line after each time it reads data to double check the value is what you expect.

Because once you read the “78” you have list with just one item. data = [78] so you get a List index out of range error. Try this as the loop:

while True: 
    line = ser.readline().decode('utf-8').rstrip()
    data = line.split(";")
    print(data)
    if len(data)==6: # If we have 6 values in this line...
        x_angle = data[0]
        x_accel = data[1]
        y_angle = data[2]
        y_accel = data[3]
        z_angle = data[4]
        z_accel = data[5]
    else: # Else we have a single number in the line
        loopclockthing = data