This post originated from an RSS feed registered with Python Buzz
by Phillip Pearson.
Original Post: Colourful console output from Python on Windows
Feed Title: Second p0st
Feed URL: http://www.myelin.co.nz/post/rss.xml
Feed Description: Tech notes and web hackery from the guy that brought you bzero, Python Community Server, the Blogging Ecosystem and the Internet Topic Exchange
Someone just asked me how to display the received bytes from two serial ports in different colours, from a Python script. I'm assuming they're using Windows, which means they want to use the WConio library to change the colour and the PySerial library to talk to the serial port.
So, here we go. Note that I use sys.stdout.write rather than print because I don't want to have either a space (as with print "foo",) or a carriage return (print "foo") after each bit of output.
done = 0
while not done:
for portname, port, colour in ports:
n = port.inWaiting()
if n:
WConio.textattr(colour)
sys.stdout.write(port.read(n))
while WConio.kbhit():
if WConio.getch()[0] == 27:
done = 1