2

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?

1 Answer 1

2

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 <[email protected]>
# 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.

2
  • yes, this worked
    – andijcr
    Oct 28, 2010 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. Mar 5, 2013 at 18:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .