Jump to content

Pretty output from your serial port


Recommended Posts

  • Member

Back in the good old days™, real programmers used a modem to dial into BBS's and share programs.  A little while later, we used our modems to dial into the University VAX or Unix machines to do our programming assignments.  In those days, we interacted with the servers via text terminals and sometimes with graphics terminals over the serial connections (before we had ethernet in the dorm rooms).  The interactivity included cursor movements, windowed screen scrolling and even COLOR!!!.

I am here to tell you that these wonders are still available today through the magic of the terminal emulation modes built into most every serial terminal program. (that is why they are called terminal emulators). So here is a crash course in using this information.

First: the raw data can be had here: https://en.wikipedia.org/wiki/ANSI_escape_code

There are a number of terminal emulations but the most common ones were VT100 and ANSI.  These were related so most codes do work.

Here is how to use them in your program:

First let us setup some constants

// A selection of ANSI Control Codes
#define RESET   "\33[0m"
#define CLS     "\33[2J"
#define HOME    "\33[1;1H"
#define RED     "\33[31m"
#define GREEN   "\33[32m"
#define BLUE    "\33[34m"
#define WHITE   "\33[37m"
#define INVERSE "\33[7m"
#define NORMAL  "\33[27m"

Next let us use them with some print commands.

printf(RESET GREEN "Hello" BLUE "_" RED "World" CSI_RESET "%d\n",5);

Notice, that C will automatically concatenate strings when you place them all next to each other.

This will issue a RESET to the terminal to ensure all other formatting like double high, flashing or inverse text are all removed.  Then It sets the text color to green, prints Hello, sets it to blue and prints the _ finally red for the World, clears the formatting and prints the integer 5.  That should be enough of an example for you to go wild.

Each control code is about 4-5 bytes long so it does not add much to your program but it sure does make error messages stand out.

Good Luck

Link to comment
Share on other sites

  • 5 weeks later...


This topic is now archived and is closed to further replies.


  • Create New...