Reading data from Arduino into Python

I'm trying to read the data from Arduino to python. The data is read by two sensors so i wanted the be contusloyesly reading at the same time i want to take 1000 samples every time and put them into the function (preposs).

> arduino_port = port='COM4'
> baud = 115200 #arduino uno runs at 9600 baud
> ser = serial.Serial(arduino_port, baud)
> #display the data to the terminal
> sensor_data = [] #store data
> 
> sensor_data.append(readings)
> samples = 4999 #how many samples to collect
> line = 0 #start at 0 because our header is 0 (not real data)
> 
> 
> while line <= samples:
>     getData=ser.readline()
>     dataString = getData.decode('utf-8')
>     data=dataString[0:][:-2]
> 
>     readings = data.split(",")
> 
>     sensor_data.append(readings)
> 
>     line = line+1
> df = pd.DataFrame(sensor_data, columns=["y-axis", "z-axis"])
> data12 = df.astype("float32")
> 
> X=preposs(data12, 250, 50)
> print(data12)

This picture shows 5000 samples i manged to print but, it printed after it collected the sample. whereas,i wanted it to be continually printing and i take 1000 sample and put it in the function with the same data frame
image

it's not really a Python forum... so you might not find the help you want here

can you post with code tags so that at least it's possible to see the indentation of the python code?

I dont understand what you mean by (indentation of the python code)

Python code structure (loops, conditional statements ...) is defined by indentation instead of brackets. So, your python code example right now has no structure and we do not know what you intend it to do.

arduino_port = port='COM4'
baud = 115200 #arduino uno runs at 9600 baud
ser = serial.Serial(arduino_port, baud)
#display the data to the terminal
sensor_data = [] #store data

sensor_data.append(readings)
samples = 4999 #how many samples to collect
line = 0 #start at 0 because our header is 0 (not real data)


while line <= samples:
    getData=ser.readline()
    dataString = getData.decode('utf-8')
    data=dataString[0:][:-2]

    readings = data.split(",")

    sensor_data.append(readings)

    line = line+1
df = pd.DataFrame(sensor_data, columns=["y-axis", "z-axis"])
data12 = df.astype("float32")

#X=preposs(data12, 250, 50)
    
    
#predictions = EMG_CNcN.predict(X,batch_size=32)
#predicted_value = np.argmax(predictions[0])

#print(predictions)
#print(predicted_value)
print(data12.dtypes)

read How to get the best out of this forum and see mention of code tags

i have already adjust it

thanks

so the question is about printing the data as you receive it?

I want the code to be running forever and every time i take 1000 sample and print it

change samples to 1000, add an encompassing while True: and reset line to 0 once you collected and processed your 1000 samples

Ya i know that i need to change the sample to 1000 the code i gave i gust put 5000 for testing. but the thing is the code is take 5000 and print it and the code stop running

that's why you need the encompassing while

something like this (typed here)

while True:
	while line <= samples:
		getData=ser.readline()
		dataString = getData.decode('utf-8')
		data=dataString[0:][:-2]
		readings = data.split(",")
		sensor_data.append(readings)
		line = line+1

	df = pd.DataFrame(sensor_data, columns=["y-axis", "z-axis"])
	data12 = df.astype("float32")
	print(data12.dtypes)

	line = 0 
	sensor_data.clear()

Thank you sir so much it worked perfectly now. But what is the purpose of putting

line = 0 
sensor_data.clear()

Glad it worked

Those 2 lines reset the index and the array where you are collecting the data

I read this genuine thing about this topic .The hardware consists of an open-source hardware board designed around an ATmega328 microcontroller, with a wide range of input and output pins. The software consists of a development environment that runs on your computer, used to write and upload code to the microcontroller board.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.