Feb-12-2020, 01:51 AM
Hi,
I'm trying to plot the values from my uC's ADC in real-time. For this I'm using pyserial and Matlibplot. But I try to plot in real-time, I only get 1 value and it doesnt update in the while loop. When I save all the values and then plot them (no real-time plot), it works fine. Any suggestions? I'm trying to simulate an oscilloscope, so the faster the plot, the better. I also tried with 9600 baudrate. The second part of my code deals with the analysis of the data.
I'm trying to plot the values from my uC's ADC in real-time. For this I'm using pyserial and Matlibplot. But I try to plot in real-time, I only get 1 value and it doesnt update in the while loop. When I save all the values and then plot them (no real-time plot), it works fine. Any suggestions? I'm trying to simulate an oscilloscope, so the faster the plot, the better. I also tried with 9600 baudrate. The second part of my code deals with the analysis of the data.
import time
import matplotlib.pyplot as plt
import serial
import numpy as np
######### Convierte data recibida a int
def bytes_to_int(bytes):
result = 0
for b in bytes:
result = result * 256 + int(b)
return result
##### Inicializar puerto serial
ser = serial.Serial(port='COM11', baudrate=38400)
#################
val_vol = []
val_time = []
count = 0
n = 40000 # Numero de muestras
######### Real-Time PLot
while (count <= n):
# ser = serial.Serial(port='COM11', baudrate=38400, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, timeout=0.5)
vol = ser.read()
vol1 = bytes_to_int(vol)
val_vol.append(vol1)
val_time.append(time.clock())
print(vol1)
count += 1
plt.plot(val_vol) # Plot data
plt.show(block=False) # Plot in real time
plt.pause(0.002)
time.sleep(0.002)
#####################################
print(val_vol)
def find_nearest(array, value): # Finds the nearest value to the max and min % set by user
array = np.asarray(array) # Make sure its an array
idx = (np.abs(array - value)).argmin() # Calculate the position of the value
return array[idx] # Returns value
##### Valores Criticos ##############
amplitud = max(val_vol) - min(val_vol)
valor = (amplitud * 0.9) + min(val_vol)
valor_min = (amplitud * 0.1) + min(val_vol)
################################
###### Inicializar arrays ############
res_list = []
res_list_min = []
time_index = []
time_index_min = []
####################################
##### Valores del 90 y 10% ################
index_valor = find_nearest(val_vol, valor)
index_valor_min = find_nearest(val_vol, valor_min)
####################################
##### For loop para encontrar index de valores #####
for i in range(0, len(val_vol)):
if val_vol[i] == index_valor:
res_list.append(i)
if val_vol[i] == index_valor_min:
res_list_min.append(i)
############################################
print("index valor", index_valor)
print("Res_list", res_list)
print("tamanio", len(val_time))
###### For loop para porcentaje mayor ########
for i in range(0, len(res_list)):
time_index1 = val_time[res_list[i]]
time_index.append(time_index1)
###############################################
###### For loop para porcentaje menor ########
for i in range(0, len(res_list_min)):
time_index1_min = val_time[res_list_min[i]]
time_index_min.append(time_index1_min)
###############################################
###### Crear lineas del 90 y 10 % #############
temp_1 = np.ones(len(val_time)) # Temp var for line
lin_max = valor * temp_1
lin_min = valor_min * temp_1
################################################
###### Tiempo critico #############
ts = abs(time_index[0] - time_index_min[0])
tb = abs(time_index[2] - time_index_min[2])
##############################################
##### Plot points ############
x_1 = np.array(time_index[0])
y_1 = index_valor
x_2 = np.array(time_index_min[0])
y_2 = index_valor_min
#########################################
print("time index", time_index)
print("El tiempo de subida es: ", ts, "El tiempo de bajada es: ", tb)
print(x_1, y_1)
####### Plotear data #####################
plt.plot(val_time, val_vol)
plt.plot(val_time, lin_max)
plt.plot(val_time, lin_min)
plt.plot(x_1, y_1, 'ro')
plt.plot(x_2, y_2, 'ro')
plt.show()
###########################################
