The Artima Developer Community
Sponsored Link

Python Buzz Forum
Colourful console output from Python on Windows

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Phillip Pearson

Posts: 1083
Nickname: myelin
Registered: Aug, 2003

Phillip Pearson is a Python hacker from New Zealand
Colourful console output from Python on Windows Posted: Nov 20, 2003 9:41 PM
Reply to this message Reply

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
Latest Python Buzz Posts
Latest Python Buzz Posts by Phillip Pearson
Latest Posts From Second p0st

Advertisement
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.

import sys, WConio, serial

SPEED = 19200

ports = [("COM1", serial.Serial(0, SPEED), 0x1f),
         ("COM2", serial.Serial(1, SPEED), 0x4f),
         ]

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

Comment

Read: Colourful console output from Python on Windows

Topic: 7 hours, one line of code Previous Topic   Next Topic Topic: Web Application Patterns: Status Notification

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use