Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am writing an application that communicate via a serial port to an external board. In this phase i need to test the serial protocol, but the external hardware is not yet usable. Is there a way to open a terminal over a (virtual) serial port to my app?

share|improve this question
up vote 2 down vote accepted

Using the "ptmx" interface is probably the best bet. Here is an example program that will attach to /dev/ptmx and trigger the creation of a /dev/pts/N device node that you can attach to from your application.

For details, see "man pty".

# Spawn pseudo-tty for input testing.
# Copyright 2010, Canonical, Ltd.
# Author: Kees Cook <>
# License: GPLv3
import os, sys, select

parent, child = os.openpty()
tty = os.ttyname(child)
os.system('stty cs8 -icanon -echo < %s' % (tty))

print tty

    os.system('stty cs8 -icanon -echo < /dev/stdin')

    poller = select.poll()
    poller.register(parent, select.POLLIN)
    poller.register(sys.stdin, select.POLLIN)

    running = True
    while running:
        events = poller.poll(1000)
        for fd, event in events:
            if (select.POLLIN & event) > 0:
                chars =, 512)
                if fd == parent:
                    os.write(parent, chars)
    os.system('stty sane < /dev/stdin')

When you run this, it will tell you the name of the pts to attach to with your application, and you can simulate the hardware by just typing in the terminal.

share|improve this answer
yes, this worked – andijcr Oct 28 '10 at 7:26
I can simulate data sent from hardware by writing to the terminal but How can i read the data sent to the hardware. It doesn't show in the terminal. – fady mohamed osman Mar 5 '13 at 18:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.