448,563 Members | 1,280 Online Need help? Post your question and get tips & solutions from a community of 448,563 IT Pros & Developers. It's quick & easy.

# Pylab and pyserial plot in real time

 P: n/a 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 Replies

 P: n/a 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

 P: n/a 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

 P: n/a 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.set_data(t,h) timesub.set_xlim((t,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

 P: n/a 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

 P: n/a 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 discussion thread is closed

Replies have been disabled for this discussion. 