Pylab and pyserial plot in real time
Peter Hansen
peter at engcorp.com
Sun Nov 6 15:08:34 EST 2005
googlinggoogler at 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
More information about the Python-list
mailing list