LoginSignup
3
4

More than 5 years have passed since last update.

[メモ] RaspberryPi へ シリアル接続時の画面サイズとか変更(USB-UARTケーブルでPCから)

Last updated at Posted at 2016-05-04

# うまい方法あったら、コメントください.

問題点

Raspberry PiにUSB-UARTで接続すると、コンソールの表示がいまいち。

  • 80x24のサイズ固定 1.png

改善方法

  • 色: export TERM=xterm-256color; source ~/.bashrc
    2.png

  • サイズ: PCのコンソールサイズを変えて、自動で対応はできないが、以下のスクリプトで、現在のサイズに合わせてくれる

    ダウンロード
    wget https://raw.githubusercontent.com/akkana/scripts/master/termsize
    chmod +x termsize
    
    pi@raspberrypi:~ $ ./temsize
    ...
    Reset the terminal to 43 rows 103 cols
    pi@raspberrypi:~ $
    

    3.png

使っている環境

termsize
#!/usr/bin/env python

# Get the current size of the terminal window, and set stty size accordingly.
# A replacement for xterm's resize program, with no X dependency.
# Useful when logged in over a serial line.
# Copyright 2013 by Akkana Peck -- share and enjoy under the GPL v2 or later.

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

tty = open('/dev/tty', 'r+')
tty.write('\033[7\033[r\033[999;999H\033[6n')
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)

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

fcntl.ioctl(fd, termios.TIOCSWINSZ,
            struct.pack("HHHH", rows, cols, 0, 0))

print "\nReset the terminal to", rows, "rows", cols, "cols"
3
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4