Virtual Serial Device in Python?
An answer to this question on Stack Overflow.
Question
I know that I can use e.g. pySerial to talk to serial devices, but what if I don't have a device right now but still need to write a client for it? How can I write a "virtual serial device" in Python and have pySerial talk to it, like I would, say, run a local web server? Maybe I'm just not searching well, but I've been unable to find any information on this topic.
Answer
If you are running Linux you can use the socat command for this, like so:
socat -d -d pty,raw,echo=0 pty,raw,echo=0
When the command runs, it will inform you of which serial ports it has created. On my machine this looks like:
2014/04/23 15:47:49 socat[31711] N PTY is /dev/pts/12
2014/04/23 15:47:49 socat[31711] N PTY is /dev/pts/13
2014/04/23 15:47:49 socat[31711] N starting data transfer loop with FDs [3,3] and [5,5]
Now I can write to /dev/pts/13 and receive on /dev/pts/12, and vice versa.