0

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

Greg
  • 11,654
  • 3
  • 44
  • 50
user3805148
  • 37
  • 1
  • 1
  • 5
  • Why don't you send a "," between two values and then in python use this "delimiter" to separate the values? [link to delimiting with python](http://stackoverflow.com/questions/1059559/python-strings-split-with-multiple-delimiters) p.s. This concept of packet delimiter is useful if you at some point move onto serial communication protocols. – ka05 Aug 04 '14 at 09:41
  • Thanks, I am now able to plot for higher frequencies but if I use the delimiter for lower sampling frequencies I lose my data and I get 0.0 as output while it should be the value I am receiving from serial port.I think this is because at lower frequencies I do not actually require delimiter as my values are already separated but as in my case when I use delimiter it returns 0 value. so is there a way to make this robust for both lower as well as higher frequencies – user3805148 Aug 06 '14 at 04:19

1 Answers1

0

I can think of two ways for doing this reliably:

  • Use a delimiter . All you need is to parse the value correctly:

    line = serial.readline()
    reading = int(line)
    
  • If you don't want to use a delimiter then send a formatted reading from the msp:

    uint8_t buffer[5];
    snprintf(buffer, 4, "%04d", reading);
    uart_print(buffer);
    

    this way you will always get 4 characters per reading, so you can do this in the python code:

    line = serial.read(4)
    reading = int(line)
    

I'd still go for the first alternative, though.

jlhonora
  • 10,179
  • 10
  • 46
  • 70