Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

1 Answer 1

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".

#!/usr/bin/python
# Spawn pseudo-tty for input testing.
# Copyright 2010, Canonical, Ltd.
# Author: Kees Cook <kees@ubuntu.com>
# License: GPLv3
import os, sys, select

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

print tty

try:
    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 = os.read(fd, 512)
                if fd == parent:
                    sys.stdout.write(chars)
                    sys.stdout.flush()
                else:
                    os.write(parent, chars)
finally:
    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

 
discard

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.