Using a Script to Simulate a Serial Device for VirtualBox

Recently I needed to simulate a serial device (a GPS, to be precise) for a Windows application I'm modifying for a client.

I don't use Windows on a regular basis, but I do have a license for it, and an original Windows XP CD from back in the day, so I loaded Windows in a VirtualBox virtual machine for the odd occasion I might need Windows, like when I build the Windows version of OpenLP 2.0.

Fortunately for me, because I'm running Linux on my system, and I have Windows running in VirtualBox, it's actually quite easy to simulate a serial device.

VirtualBox allows you to specify where to connect the guest's serial device on the host, and provides a few options. I selected "pipe" which is a standard Un*x socket, and I set VirtualBox to create the pipe if it didn't exist. I set the value of the pipe to be /tmp/vbox-socket.

Next I hauled out an extremely handy little program called socat. I had to install this, but that was a simple case of sudo aptitude install socat. Once I had installed socat, I set up a connection from a pseudo serial device (man pty can tell you more about that) to the Un*x socket:

@:~$ socat UNIX-CONNECT:/tmp/vbox-socket PTY,link=/tmp/vbox-pty

That will create a pseudo serial device called /tmp/vbox-pty and connect the Un*x socket to the pseudo serial device. Now, to connect up your script to the PTY device, simply use the standard Linux redirection:

python > /tmp/vbox-pty