Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[General] Real time UART data plotting in python

Status
Not open for further replies.

santh92

Junior Member level 1
Joined
Sep 8, 2014
Messages
18
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,487
Hi to all,
I am working on msp430 microcontroller by using that i am able to transfer the sine signal through UART and now want to plot that signal using python. But unfortunately i am unable to get the plot as i expected. Is there anyone can help me to solve this. and please explain me the matplotlib library of python. Thanks in advance

I am attaching my code below :

Code:
 :???:
import sys
import serial
import numpy as np
import matplotlib.pyplot as plt
from collections import deque

port = "COM14"
baud = 460800
timeout=1

ser = serial.Serial()
ser.port = port
ser.baudrate = baud
ser.timeout = timeout

a1 = deque(([0.0])*256)

line, = plt.plot(a1)
plt.ion()
plt.title("Real Time Data Monitoring")
plt.xlabel("Time_Period")
plt.ylabel("Amplitude")
plt.ylim([0,300])
plt.grid()

try:
  ser.open()
except:
  sys.stderr.write("Error opening serial port %s\n" % (ser.portstr) )
  sys.exit(1)

while 1:
     data = ser.read(3)

     print(a1)
     a1.appendleft((data))
     datatoplot = a1.pop()
     line.set_ydata(a1)
     plt.draw()
     plt.pause(0.0001)

:???:
 
Last edited by a moderator:

Hi,

But unfortunately i am unable to get the plot as i expected.
This tells nothing.
--> Read forum rules on how to give informative error descriptions.

***
460800 baud is huge. Is your MSP430 able to handle this?
What clock source and what clock frequency do you use?


Klaus
 

Dear Klaus,
Thanks for your reply and sorry for the inconvenience.

Actually i am able to monitor the data on TERA TERM(Serial port terminal), my requirement is to get the waveform corresponding to my uart data. For that purpose i started using python, but i am unable to get the waveform for my program. I thought some modification should required in python. thanks

ya Msp430 is having able to handle 460800 baudrate with 12Mhz(maximum 20 MHZ) system clock.
 

Still not clear.
What is the issue ?
  • PC program does not get any data?
  • PC program get but plot it incorrectly ( e.g data lost )
  • PC program plot correctly, but not realtime ?
I'm unaware if Python can handle this, but did you consider using interrupt on the PC side ?
Did you make experiments using lower baud rates ?
 

Hi,

With 12MHz or 20MHz clock, how can the MSP generate 460800 baud within the specified tolerance?
I doubt it can.

Klaus
 

Real time data plotting on python

Hello all,
I am facing a problem about Real time uart data plotting on python
Actually i am using msp430f5438a microcontroller, and i am trying to plot sine waveform(which is given by a function generator) on python.

My doubt is => it is possible to plot the higher frequency signal on python by using msp430 microcontroller?
The frequency range is about 5khz

My experiment: Here, I am sending the data from msp430 as a formatted reading which i have mentioned below,
sprintf(str,"%03d",ADC_result);

ADC Configuration is
8-bit configuration, 460800 baud rate,
USCI module clk frequency = 12mhz
ADC12CLK frequency is = 4mhz
My sampling frequency is = 15khz

According to my understanding i am converting each sample of adc to 3-bytes(or character) to transfer through uart.

Wright now my problem is Where i am unable to plot a sine waveform which frequency is more than 10 hz.

If i got solution for this, i will really feel blessed.. Thanks in advance...

i am attaching the python code below:

Code Python - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import sys
import serial
import numpy as np
import matplotlib.pyplot as plt
from collections import deque
 
port = "COM14"
 
baud = 460800
 
timeout=1
 
ser = serial.Serial()
ser.port = port
ser.baudrate = baud
ser.timeout = timeout
 
a1 = deque(([0.0])*1000)
plt.title("Real Time Data Monitoring")
line, = plt.plot(a1)
plt.ion()
plt.xlabel("Time_Period")
plt.ylabel("Amplitude")
plt.ylim([0,300])
plt.grid()
 
try:
ser.open()
except:
sys.stderr.write("Error opening serial port %s\n" % (ser.portstr) )
sys.exit(1)
 
while 1:
data = ser.read(3)
reading = int(data)
print(a1)
a1.appendleft((data))
datatoplot = a1.pop()
line.set_ydata(a1)
plt.draw()
plt.pause(0.0001)

 
Last edited by a moderator:

Did you even read what was suggested/asked before ? Anyway, the way as you did above, doing the reception and plotting inline, running at a preemptive operational system, will not work as expected;
 

Hi,

some issues:
* you don´t have a frame sync (or I didn´t recognize) without it, how do you decide to take the correct bytes?
* with 460800 baud you only have less than 2.5% headroom for transmission speed

What did you do to debug your problem?
Step by step... from ADC....to the diagram screen: What works as desired and what does not work?

Klaus
 

Dear andre_teprom,
1) PC Program does not get any data?
2) PC Program get but plot it incorrectly?
3) PC Program plot correctly, but not real time?

Let me discuss about my problem briefly,

Actually I am using a function generator to give an external analog signal to input to the ADC of MSP430F5438A

After that I tried to monitor the ADC of MSP430 data through UART of MSP430 on my PC, that I have done by using Tera term

Now I am trying to plot the same data (which is receiving from UART) on python (Instead of using tera term because there we cannot plot the data)

My problem is, In python I am able reproduce the signal (the signal which is given from function generator) for some certain frequency range(in my case only 0- 10hz)

But my requirement is to reproduce the signal ranging from (0hz-5khz). If you want to see any waveform for various ranges I am ready to attach it.

My question is, Is it possible to reproduce that maximum frequency range (0hz-5khz) ?? Is there anyway to do it??

I am attaching the sample data format which is received on python from uart, i am trying to plot the data formatted like this. I have attached my python code on my previous post.
deque(['066', '117', '206', '108', '212', '063', '117', '206', '057', '130', '197', '048', '138', '068', '111', '188', '042', '154', '178', '035', '100', '216', '070', '108', '218', '073', '105', '214', '067', '117', '208', '056', '126', '207', '058', '123', '204', '047', '140', '190', '046', '140', '191', '042', '152', '180', '035', '101', '215', '066', '112', '182', '037', '088', '211', '062', '118', '206', '055', '127', '197', '048', '095', '218', '044', '093', '219', '076', '101', '214', '066', '113', '209', '059', '135', '194', '069', '044', '144', '186', '037', '156', '177', '034', '094', '209', '059', '092', '220', '076', '100', '216', '066', '115', '204', '054', '128', '198', '048', '138', '188', '041', '154', '175', '041', '151', '181', '035', '137'])
If you want further clarification I am ready to give you..
Thanks in advance.

- - - Updated - - -

Dear KlausST,
I have explained about my problem in my previous post

Still it is not sufficient i am ready to give you more,

Thanks in advance

- - - Updated - - -

Dear KlausST,

Ya msp430 can handle 460800 baud rate, if you have doubt on that you can refer the manual

**broken link removed** (Table 36-4. Commonly Used Baud Rates, Settings, and Errors, UCOS16 = 0
)
 

You have not yet brought information regarding the operating system in question. If it is in a multitasking system like Windows, it is notorious that from a certain reception rate (not the baudrate, but the interval between packs) there will be an asynchrony between receiving the byte through the serial and plotting on the screen, thus causing a loss of information.

Although you are not buffering the received data, I believe that it is possible to stack them by some system resource. In addition, even though it was running in an RTOS, Python by nature is not a language optimized for multitasking. Note that you are inline receiving and plotting, but ideally it should be done in an interrupt handler, which as far as I know, Python does not support such a feature.
 

Hi,

You don't expect that I go through 1200 pages of documentation.

You confirmed it can generate the baud rate within it's specified tolerance from the given xtal frequency. That's enough for me.

For the MSP as well as the PC the analog signal frequency (0..5kHz) does not matter.

Before you said you transmit 3 bytes per sample, but your shown data show that you additionally transmit delimiter bytes.
If this is the case, then you are beyond possible data rate.

When I said 'can handle the baud rate' I also meant if it has enough processing power to: ADconvert, read, transform data format, add delimiters and send them to UART...whithout missing a byte.

Klaus
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top