Window Size

When connected to another machine via SSH or Telnet, the current window size, terminal type, and other environment variables are automatically transmitted to the remote device using an out-of-band communications channel. This channel does not exist when using a serial port, however. As a result, you will need to configure these manually on the remote machine for the screen to display correctly.

Many devices assume 80x24 and a terminal type of VT100, but if you’re connected to a Linux (or other Unix) based machine, you can make use of larger window sizes and use more advanced terminal features.

Terminal Type

To set the terminal type, you will need to set the “TERM” environment variable. We recommend setting TERM to “xterm-256color” if this is supported by your device. In bash, you would type the following:

export TERM=xterm-256color

Window Size

Most Unix machines support the “stty” command to set the current window size used for formatting output from programs. If the current window size is 132x48, for example, you would type:

stty cols 132 rows 48

After doing this, screen-based programs such as vi and emacs will now fill the screen.

Serial 1.1.7 and later provide a “Send Window Size” (Command-Y) command that automatically sends this command with the current window size to the remote machine.

If your device is capable, you could also add the following to your .bashrc script on your device to query Serial for the current window size on login:

#!/usr/bin/env python
#
# Set the TTY window size to match that of the terminal
# when logged in over a serial port.
# 
# Source: http://www.decisivetactics.com/files/resize.py
#
# CHANGES
# Copyright 2013 by Akkana Peck -- share and enjoy under the GPL v2 or later.
#   Original URL: http://shallowsky.com/blog/hardware/serial-24-line-terminals.html
# Modified 24 June 2014 by Chris Kent -- use Xterm window size report.
#
#

import os, sys
import fcntl
import struct
import re
import termios
import select

tty = open('/dev/tty', 'r+')

# Ask for an Xterm window size report by sending "ESC [ 18 t"
# In response, we expect to receive "ESC [ 8; nrows; ncols t"
tty.write('\x1b[18t')		
tty.flush()

fd = sys.stdin.fileno()

oldterm = termios.tcgetattr(fd)
newattr = oldterm[:]
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)

oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)

try:
while True:
r, w, e = select.select([fd], [], [])
if r:
output = sys.stdin.read()
break
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)

replycode, rows, cols = map(int, re.findall(r'\d+', output))

# Set TTY window size using TIOCSWINSZ ioctl
fcntl.ioctl(fd, termios.TIOCSWINSZ,
struct.pack("HHHH", rows, cols, 0, 0))

print "Terminal set to", cols, "cols x", rows, "rows"