Creating a null modem virtual serial port pair connection with socat

Wednesday, 25 December 2013, 3.55PM

As an electronics engineer, and even while doing my projects as a student, I have often had to use serial ports for communication between computer and a serial device. So, while testing my code for the device, I have often felt a need for a virtual serial port pair connected in null modem connection.

Initially, I used tty0tty library from this site. But, though I didn’t have any problems with it myself, I read that it had some stability issues and was not being supported by many developers.

Recently, however, I discovered socat.

It is a simple command line tool which has fairly good documentation and is constantly being developed.You can install it through your package manager or by compiling the source code, instructions for both of which are easily available on the internet.

Now, using socat, all you need to do to create a virtual serial port pair is this

$ socat -d -d pty,raw,echo=0 pty,raw,echo=0

You will get an output which is somewhat like this:

2013/12/25 15:44:52 socat[20601] N PTY is /dev/pts/3
2013/12/25 15:44:52 socat[20601] N PTY is /dev/pts/4
2013/12/25 15:44:52 socat[20601] N starting data transfer loop with FDs [3,3] and [5,5]

Now, you have two ports /dev/pts/3 and /dev/pts/4 which have a null modem connection between them. So, everythong you send to one port will be received by the other.

You can test the connection by listening to one port and sending test data on the other. For this, open a new terminal and type

$ cat < /dev/pts/4

This will listen to port /dev/pts/4. Now, to send some data from port 3, open another terminal and type

$ echo “send to 3″ > /dev/pts/3

That message should appear at the terminal which is listening to port 4.

I have tested this connection using pyserial module in python, and so far I have no complaints.

This is how it looked like in my window:

Image

About these ads
This entry was posted in programming and tagged , , , , , . Bookmark the permalink.

2 Responses to Creating a null modem virtual serial port pair connection with socat

  1. srinath says:

    Hi

    I want to send serial data from Simulink to a virtual com port created by socat and read it into a program. How would I do that?

    • rabinnankhwa says:

      Hi srinath,
      From your question, I am assuming that you are using windows. I haven’t used socat in windows and though I think it is possible to do so, it’s not as much fun. Instead you could use some software to create a virtual serial port pair in windows and use it for your communication. The software I use is “Virtual Serial Port Driver” from Eltima, but you could also use “com0com” which is a free and open source software.
      In case, you are using matlab in linux, just do “socat -d -d pty,raw,echo=0 pty,raw,echo=0″. Its so much easier…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s