A Serial Terminal For Windows Terminal

Since switching to Windows 11, I have fallen in love with Windows Terminal–the built-from-the-ground-up terminal emulator for Windows that combines everything that I loved about Linux and MacOS with the functionality of tabbed windows and a serious dose of customization. However, because much of my command line work falls into communicating with an external device over serial, I wanted to be able to keep that workflow within Windows Terminal like I’m used to doing already. In other words, I didn’t want to have to resort to using a separate GUI like PuTTY when a command-line tool like Screen or Minicom would do just fine. The problem is that, since the removal of HyperTerminal in Windows Vista, Windows has not included a CLI solution for serial communication (which is kinda weird because Telnet is still buried in Windows 11; if the reasoning was because obsolescence, you’d think that Telnet would also get the axe).

Now, since I have Ubuntu installed, I could just install Screen or Minicom and call it a day, but WSL 2 does not support serial communication. If I wanted to use those tools, I would either need to change the WSL version every time I wanted to use the serial port or install another instance of Linux under WSL 1. I mean, it works, but I really just felt it inefficient to have to fire up Debian (the install is smaller than Ubuntu) just so I could use Minicom.

After a little more digging, I found Makerdiary’s Terminal-S. It’s a simple serial terminal written in Python that can be compiled as a self-contained exe. This little gem was exactly what I was looking for in terms of functionality! It automatically connects to the COM port (or drops a list of available ports) and connects using some default settings for baud rate, stopbit, and parity. I can fire up Windows Terminal, invoke the command in PowerShell, and I’m good to go!

Windows Serial Terminal

But this is Windows Terminal, and I have this lovely menu of drop-down presets that will automatically spawn the environment of my choice with some lovely aesthetic theming. So, I want to be able to just click on a new tab and get that full retro terminal feel without needing to pretend I’m even in Windows! What I need to do is alter the program just a smidge so that instead of looking for flags in the command-line structure (ie -b for setting baud rate), the program will just ask me what settings I want to use (including some defaults for simplicity). To do this, I simply altered the @click.command section to only look for serial ports and moved the connection settings to a series of prompts in the main function, like so:

"""
Terminal for serial port

Requirement:

    + pyserial
    + colorama
    + py-getch
    + click
"""

import os
if os.name == 'nt':
    os.system('title Serial Console')

from collections import deque
import sys
import threading

import colorama
import click
import serial
from serial.tools import list_ports


def run(port, baudrate = 115200, parity='N', stopbits=1):
    try:
        device = serial.Serial(port=port,
                                baudrate=baudrate,
                                bytesize=8,
                                parity=parity,
                                stopbits=stopbits,
                                timeout=0.1)
    except:
        print('--- Failed to open {} ---'.format(port))
        return 0

    print('--- {} is connected. Press Ctrl+] to quit ---'.format(port))
    queue = deque()
    def read_input():
        if os.name == 'nt':
            from msvcrt import getch
        else:
            import tty
            import termios
            stdin_fd = sys.stdin.fileno()
            tty_attr = termios.tcgetattr(stdin_fd)
            tty.setraw(stdin_fd)
            getch = lambda: sys.stdin.read(1).encode()

        while device.is_open:
            ch = getch()
            # print(ch)
            if ch == b'\x1d':                   # 'ctrl + ]' to quit
                break
            if ch == b'\x00' or ch == b'\xe0':  # arrow keys' escape sequences
                ch2 = getch()
                esc_dict = { b'H': b'A', b'P': b'B', b'M': b'C', b'K': b'D', b'G': b'H', b'O': b'F' }
                if ch2 in esc_dict:
                    queue.append(b'\x1b[' + esc_dict[ch2])
                else:
                    queue.append(ch + ch2)
            else:  
                queue.append(ch)

        if os.name != 'nt':
            termios.tcsetattr(stdin_fd, termios.TCSADRAIN, tty_attr)

    colorama.init()

    thread = threading.Thread(target=read_input)
    thread.start()
    while thread.is_alive():
        try:
            length = len(queue)
            if length > 0:
                device.write(b''.join(queue.popleft() for _ in range(length)))

            line = device.readline()
            if line:
                print(line.decode(errors='replace'), end='', flush=True)
        except IOError:
            print('--- {} is disconnected ---'.format(port))
            break

    device.close()
    if thread.is_alive():
        print('--- Press R to reconnect the device, or press Enter to exit ---')
        thread.join()
        if queue and queue[0] in (b'r', b'R'):
            return 1
    return 0

@click.command()
@click.option('-p', '--port', default=None, help='serial port name')
@click.option('-l', is_flag=True, help='list serial ports')

def main(port, l):
    port is None
    if port is None:
        ports = list_ports.comports()
        if not ports:
            print('--- No serial port available ---')
            return
        if len(ports) == 1:
            port = ports[0][0]
        else:
            print('--- Available Ports ----')
            for i, v in enumerate(ports):
                print('---  {}: {} {}'.format(i, v[0], v[2]))
            if l:
                return
            raw = input('--- Select port index: ')
            try:
                n = int(raw)
                port = ports[n][0]
            except:
                return
    baudrate = input("Enter baud rate (Default 115200): ")
    if (len(baudrate) == 0):
        baudrate = 115200
    parity = input("Enter parity [N,E,O,S,M] (Default N): ")
    if (len(parity) == 0):
        parity = "N"
    stopbits = input("Enter stop bit (Default 1): ")
    if (len(stopbits) == 0):
        stopbits = 1    
    while run(port, baudrate, parity, stopbits):
        pass

if __name__ == "__main__":
    main()

Of course, it took me a little bit to deconstruct the program (as well as re-learn Python, which I hadn’t really used in any significant capacity for half a decade at least), but now I have it working exactly how I want it to work, and it feels right! Of course, I had never used Python on Windows before, either, so wrapping my head around PowerShell has been enlightening as well (Yes, I’m late to the PowerShell party, so I’m making up for it). Once I figured out how to use Pyinstaller (after remembering how to set a PATH variable in Windows), then I just have to drop the portable EXE file into a nice, out of the way place in Windows (might as well put it in a folder in C:) and make sure that folder is added to the PATH variable.

Add a folder to PATH variable

Adding a folder or application to the global PATH variable is a pretty simple proposition in Windows. From Windows Explorer, right-click on “This PC” and select “Properties”. This will bring up the settings dialog for System > About. Just under the “Device Specifications” section, click “Advanced System Settings” to bring up the traditional System Properties window. Click on the “Advanced” tab, then the “Environment Variables” button at the bottom of the window.

The “Environment Variables” window is divided into to groups: one for the current user and one for the system. In the system group dialog, find and double-click the line for “Path” to edit it. Click the “New” button to add a new line and type in the path for the folder or application you wish to add to the system PATH. When you’re done, click “OK” to close the window and save, then click “OK” to close the Environment Variables window, and finally click “OK” to close the System Properties window.

Customize Windows Terminal

In Windows Terminal, open the settings dialog and click “Add A New Profile”. Fill in the settings as appropriate. I’ve posted mine as a model:


Retrotacular

Of course, the real draw here is giving the terminal tab a lovely old-school green phosphor look which we can do either using the settings GUI to turn on the “Retro terminal effects” (this setting simulates CRT scan lines and gives a nice anti-alias that simulates the phosphorescent glow of the characters on the monitor). I’m using the Cascadia Mono font at 16pt weight to get the look I’m going for and the “Vintage” cursor style will round everything out nicely. For the color scheme, add the following block of code to the settings JSON file, then enable the new “Green Phosphor” color scheme.

{
            "background": "#000000",
            "black": "#000000",
            "blue": "#00FF00",
            "brightBlack": "#00FF00",
            "brightBlue": "#00FF00",
            "brightCyan": "#00FF00",
            "brightGreen": "#00FF00",
            "brightPurple": "#00FF00",
            "brightRed": "#00FF00",
            "brightWhite": "#00FF00",
            "brightYellow": "#00FF00",
            "cursorColor": "#00FF00",
            "cyan": "#00FF00",
            "foreground": "#00FF00",
            "green": "#00FF00",
            "name": "Green Phosphor",
            "purple": "#00FF00",
            "red": "#00FF00",
            "selectionBackground": "#FFFFFF",
            "white": "#00FF00",
            "yellow": "#00FF00"
        }

From here, everything is ready to go! Connect a serial device, fire up the terminal, and you’re off to the races!