Search code examples
linuxserial-portvirtual-serial-port

Virtual Serial Port for Linux


I need to test a serial port application on Linux, however, my test machine only has one serial port.

Is there a way to add a virtual serial port to Linux and test my application by emulating a device through a shell or script?

Note: I cannot remap the port, it hard coded on ttys2 and I need to test the application as it is written.


Solution

  • You can use a pty ("pseudo-teletype", where a serial port is a "real teletype") for this. From one end, open /dev/ptyp5, and then attach your program to /dev/ttyp5; ttyp5 will act just like a serial port, but will send/receive everything it does via /dev/ptyp5.

    If you really need it to talk to a file called /dev/ttys2, then simply move your old /dev/ttys2 out of the way and make a symlink from ptyp5 to ttys2.

    Of course you can use some number other than ptyp5. Perhaps pick one with a high number to avoid duplicates, since all your login terminals will also be using ptys.

    Wikipedia has more about ptys: http://en.wikipedia.org/wiki/Pseudo_terminal