I am sending serial data from uart to pc and trying to plot sine wave(using Python) sent from function generator through ADC12 of MSP430F5438A. I am able to plot the wave for lower sampling frequencies(<120Hz) but when I increase the sampling frequency the digits get concatenated i.e. if two values 2563 , 2879 are sent through uart then python reads them as 25632879. So, I am not able to plot the graph as values are not correct. I am sending the values without new line between them, if I send with new line then the values are not read correctly - python reads them with space in between so then again I get another error: could not convert string to float. I tried data = ser.readline() as well but no luck I am attaching the code below.Please see if anything can be done to solve this problem.
import sys
import serial
import numpy as np
import matplotlib.pyplot as plt
from collections import deque
port = "COM11"
baud = 9600
timeout=1
ser = serial.Serial()
ser.port = port
ser.baudrate = baud
ser.timeout = timeout
a1 = deque([0.0]*100)
#ax = plt.axes(xlim=(0, 100), ylim=(0, 1000))
line, = plt.plot(a1)
plt.ion()
plt.ylim([0,1000])
try:
ser.open()
except:
sys.stderr.write("Error opening serial port %s\n" % (ser.portstr) )
sys.exit(1)
#ser.setRtsCts(0)
while 1:
# Read from serial port, blocking
data = ser.read(1)
# If there is more than 1 byte, read the rest
n = ser.inWaiting()
data = data + ser.read(n)
#sys.stdout.write(data)
print(a1)
a1.appendleft((data))
datatoplot = a1.pop()
line.set_ydata(a1)
plt.draw()
Thanks