473,404 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Pylab and pyserial plot in real time

Hiya,

I've got a PIC microcontroller reading me humidity data via rs232, this
is in ASCII format. I can view this data easily using hyperterminal or
pyserial and convert it to its value (relative humidty with ord(input))

But what im trying to do is plot the data in real time, ideally with
pylab - as it looks simple to use and simple is the way i want to go!

My code is below, it doesnt show a graph, I was wondering whether
someone could suggest whats wrong?

thank you in advance

David

################################################## ######################

import serial
from pylab import *

ser = serial.Serial(0)
t = arange(0.0, 1.0+0.01, 0.01)

xlabel('time')
ylabel('RH %')
title(' RH sensor data sampled at 1 sec intervals ')
#grid(true)

x = 0

while 1:
s = ser.read()
b = ord(s)
h = []
h.append(b)
x = x + 1
plot(t,h)

ser.close

################################################## ######################

Nov 6 '05 #1
6 17656
go*************@hotmail.com wrote:
I've got a PIC microcontroller reading me humidity data via rs232, this
is in ASCII format.
What do you mean when you say it's in ASCII format? ASCII defines a
convention for representing control and printable characters. Do you
mean that the readings you get are shown in hyperterminal as numbers
such as "101"? Or something else?
I can view this data easily using hyperterminal or
pyserial and convert it to its value (relative humidty with ord(input))
Okay, since you don't show examples, we'll have to assume you know what
you're doing there...
My code is below, it doesnt show a graph, I was wondering whether
someone could suggest whats wrong?
What does it do? What does it do if you put a print statement after the
ord() line? Maybe try "print repr(s), b"...
while 1:
s = ser.read()
b = ord(s)
h = []
h.append(b)
x = x + 1
plot(t,h)

ser.close


Note that the last line is incorrect: it should be ser.close(). Without
the parentheses it just creates a temporary reference to the method,
then discards it. (Of course, this isn't what's stopping your code from
working.)

You haven't provided us much detail, but what happens if you change the
line that reads from the serial port to the following?

s = '5'

This, of course, will partition the problem, showing you whether the
problem is with the serial reading or with the plotting. If it's still
not plotting, you can assume you have missed a critical step in using
pylab and you should probably go back a step and make sure you can run
whatever tutorial or example code is included with pylab. (This is the
first I've heard of pylab, so I can't help you there.)

-Peter
Nov 6 '05 #2
Yea I know the data is correct, all I do is sample my data with an ADC
and then send it to the serial port. using hyper terminal or indeed
pyserial presents the data as a ASCII charecters, the value of these
Charecters is the converted to there equivalent decimal value using the
ord() command.

Basically the code was supposed to get the values from the serial port,
place them in some sort of FIFO buffer with no limits on size and then
plot the values each time a new value is recieved, constantly updating
and presented near enough the graph of the current events.

I can't actually get to the computer with the hardware setup on so i
can't test the ideas you had, however I will do so tommmorw and report
back...

Im pretty certain however that its the plotting aspect of my routine
that doesnt work as I have mucked around with the data fed from the
serial port and its OK, for instance the sensor placed in an oven that
has been heated and dried for hours presnets a value a lot less than
that of the sensor placed in the spout of a boiling kettle - so the
values here are ok for my purposes.

thanks for the clues. wish there was a pylab expert around here as my
example is copied from there tutorial, maybe I need to destory the
window on each iteration or something?

Does anyone know of a module designed for ploting real time data thats
more appropriate for the above mentioned task than pylab??

thanks

David


Peter Hansen wrote:
go*************@hotmail.com wrote:
I've got a PIC microcontroller reading me humidity data via rs232, this
is in ASCII format.


What do you mean when you say it's in ASCII format? ASCII defines a
convention for representing control and printable characters. Do you
mean that the readings you get are shown in hyperterminal as numbers
such as "101"? Or something else?
I can view this data easily using hyperterminal or
pyserial and convert it to its value (relative humidty with ord(input))


Okay, since you don't show examples, we'll have to assume you know what
you're doing there...
My code is below, it doesnt show a graph, I was wondering whether
someone could suggest whats wrong?


What does it do? What does it do if you put a print statement after the
ord() line? Maybe try "print repr(s), b"...
while 1:
s = ser.read()
b = ord(s)
h = []
h.append(b)
x = x + 1
plot(t,h)

ser.close


Note that the last line is incorrect: it should be ser.close(). Without
the parentheses it just creates a temporary reference to the method,
then discards it. (Of course, this isn't what's stopping your code from
working.)

You haven't provided us much detail, but what happens if you change the
line that reads from the serial port to the following?

s = '5'

This, of course, will partition the problem, showing you whether the
problem is with the serial reading or with the plotting. If it's still
not plotting, you can assume you have missed a critical step in using
pylab and you should probably go back a step and make sure you can run
whatever tutorial or example code is included with pylab. (This is the
first I've heard of pylab, so I can't help you there.)

-Peter


Nov 6 '05 #3
go*************@hotmail.com wrote:
Hiya,

I've got a PIC microcontroller reading me humidity data via rs232, this
is in ASCII format. I can view this data easily using hyperterminal or
pyserial and convert it to its value (relative humidty with ord(input))

But what im trying to do is plot the data in real time, ideally with
pylab - as it looks simple to use and simple is the way i want to go!

My code is below, it doesnt show a graph, I was wondering whether
someone could suggest whats wrong?
You have to call pylab.show() for the graph to be drawn. I don't know if it will work incrementally if you call show() in the loop.

Kent

thank you in advance

David

################################################## ######################

import serial
from pylab import *

ser = serial.Serial(0)
t = arange(0.0, 1.0+0.01, 0.01)

xlabel('time')
ylabel('RH %')
title(' RH sensor data sampled at 1 sec intervals ')
#grid(true)

x = 0

while 1:
s = ser.read()
b = ord(s)
h = []
h.append(b)
x = x + 1
plot(t,h)

ser.close

################################################## ######################

Nov 6 '05 #4
go*************@hotmail.com wrote:
Hiya,

I've got a PIC microcontroller reading me humidity data via rs232, this
is in ASCII format. I can view this data easily using hyperterminal or
pyserial and convert it to its value (relative humidty with ord(input))

But what im trying to do is plot the data in real time, ideally with
pylab - as it looks simple to use and simple is the way i want to go!

This might be close to what you are trying to do:

import time
import pylab
# interactive mode on
pylab.ion()
timefig = pylab.figure(1)
timesub = pylab.subplot(111)
dt = 0.1
t = pylab.arange(0.0, 2.0, dt)
h = 1.2*pylab.sin(t)
lines = pylab.plot(t,h)
for i in range(8):
t = t + dt
h = 1.2*pylab.sin(t)
lines[0].set_data(t,h)
timesub.set_xlim((t[0],t[-1]))
pylab.draw()
time.sleep(1.0)

It shows a piece of sine curve, and updates the x-axis with time.
Nov 8 '05 #5
go*************@hotmail.com wrote:
Does anyone know of a module designed for ploting real time data thats
more appropriate for the above mentioned task than pylab??


You could have a look at my plotting package, Veusz, which can be embedded
in other apps. You can update the data in real time, as the windowing runs
in a separate thread.

The main problem is that I have only really tested it on Unix, but I have
reports that it "mostly" works in Windows (I'm looking into supporting this
soon).

http://home.gna.org/veusz/

Alternatively matplotlib may be another solution.

--
Jeremy Sanders
http://www.jeremysanders.net/
Nov 8 '05 #6

Juho Schultz: Thanks for that, havent got time to modify it for my
needs at the moment, but im sure it'll work as i've just tried it

Jeremy Sanders: Cheers for that, i'll check it out.

thanks to everyone else to!

Thanks

David

Nov 8 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: | last post by:
I've trolled the lists, FAQs, and Net as a whole, but can't find anything definitive to answer this. We're looking for real-time graph capability (bar, line, etc), so that we can display...
18
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a real-time c++ project, and we're having a discussion...
3
by: Madhusudan Singh | last post by:
Hi I am still a rookie at python (can do some basic programming with the language), and have been using python gpib and matplotlib to control my instruments and do real time plots. Since I have...
12
by: Russ | last post by:
I'm interested in setting up a web page where live data can be displayed in real-time on the web page. For example: I would like to display a (nice looking) graph of some data value versus time...
1
by: luca72 | last post by:
Hello at all. I need to do a real time plot where on the frame i have this like limit line: import math dati = for freq in range(int(freqiniziale), (int(freqfinale )+ 1)): forza = float(massa)...
0
by: cdeelert | last post by:
Hi friends, I'm using vc++ 2005. I'd like to be able to plot my data in real time and show it on screen. Is there any library I can use? Can I use Matlab functions to do this job? If so, how? So,...
0
by: Johannes Nix | last post by:
Hi, this might be of interest for people who are look for practical information on doing real-time signal processing, possibly using multiple CPUs, and wonder whether it's possible to use...
5
by: 9jaguy | last post by:
Guys, I treally appreciate the way questions are answered on this forum. I hope I get someone to help on this quickly too. :) I am using vb 2005 and getting data directly from two sensors in two...
3
by: Kurt Mueller | last post by:
David, Am 07.10.2008 um 01:25 schrieb Blubaugh, David A.: As others mentioned before, python is not the right tool for "HARD REAL TIME". But: Maybe you can isolate the part of your...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.