Fake Serial Communication Under Linux

Virtual Serial Port for Linux

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

Fake serial communication under Linux

It's probably best to use pyserial to communicate with the serial port, and you can just create a mock version of the serial.Serial class which implements read, readline, write and any other methods you need.

How to create a dummy pipe pseudo-serial device on Linux?

Credit to meuh for his tip-off.

socat UDP:127.0.0.1:5001,bind=127.0.0.1:5000 \
PTY,link=/dev/ttyS0,raw,echo=0,waitslave

This listens on UDP port 5000 on the loopback network interface. All data received is sent to the virtual serial device at /dev/ttyS0. All data received on the virtual serial device is sent to UDP address 127.0.0.1:5001.

The IP address can be remote.

The command must be run as root, as must the process connecting to the serial port. To avoid this use a different file path, e.g. /tmp/ttyS99.

Apparently the file path specified must not already exist. However my PC has /dev/ttyS0 all the way to /dev/ttyS31 despite not having any serial ports, and using /dev/ttyS0 works fine. I suppose if I actually had a real serial port this wouldn't work.

Emulate serial port

Yes, you can use socat to simulate a serial port.

You need to use socat's PTY address type:

PTY: Generates a pseudo terminal (pty) and uses its master side. Another
process may open the pty's slave side using it like a serial line or
terminal.

The simplest option is:

socat PTY,link=./virtual-tty,raw,echo=0 -

Have the application you are testing opens virtual-tty. Output from your
application will print to the console. Text you type will be sent to your
application.

As noted above, the PTY address type creates a
peudo-terminal. The link
option creates a soft-link between the pseudo-terminal and the given file.
You can choose any filename you wish. Without the soft-link you would need to
open the device and it is difficult to determine the correct one. raw
puts the pseudo-terminal in raw mode. You usually want this as you don't want
any of the special terminal handling options. echo=0 disables echo mode.

If you have (or create) an application that simulates the code executing on
the Arduino, you can connect it via socat as well. If your simulator
comunicates via stdin/stdout, then use the following command:

socat PTY,link=./virtual-tty,raw,echo=0 EXEC:simulator-command

The above connects the stdin/stdout of simulator-command to the
pseudo-terminal.

If your simulator communicates via a serial port as well, then use the PTY
command twice:

socat PTY,link=./arduino-sim,raw,echo=0 PTY,link=./virtual-tty,raw,echo=0

Have your simulator open arduino-sim.

Create a virtual serial port connection over TCP

Try socat. Possible scenario:

socat  pty,link=/dev/virtualcom0,raw  tcp:192.168.254.254:8080&

socat creates TCP connection to 192.168.254.254:8080, so that everything, that will be written to /dev/virtualcom0 will be forwarded to 192.168.254.254:8080 and vice versa.

Another approach would be to use RFC2217 via ser2net on Linux sever side and RFC2217 driver on Windows side (for example http://www.hw-group.com/products/hw_vsp/index_en.html single port version). You can also try to get http://pyserial.sourceforge.net/ to work with ser2net.

How create a virtual io device in Linux that proxies data to real device?

I ended up using socat

Examples can be found here: socat examples

You socat back to back on both the machines. One listens on a tcp port and forwards data to local virtual port or pty. The socat on other box uses real device as input and forwards any data to tcp port.



Related Topics



Leave a reply



Submit