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

<p>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?</p>

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

[{'answer_id': 52232, 'author': 'Howler', 'author_id': 2871, 'author_profile': 'https://Stackoverflow.com/users/2871', 'pm_score': 3, 'selected': False, 'text': '<p>Would you be able to use a USB->RS232 adapter? I have a few, and they just use the FTDI driver. Then, you should be able to rename /dev/ttyUSB0 (or whatever gets created) as /dev/ttyS2 .</p>\n'}, {'answer_id': 52303, 'author': 'Adam Davis', 'author_id': 2915, 'author_profile': 'https://Stackoverflow.com/users/2915', 'pm_score': 3, 'selected': False, 'text': '<p>I can think of three options:</p>\n\n<h2>Implement RFC 2217</h2>\n\n<p><a href="http://www.rfc-archive.org/getrfc.php?rfc=2217" rel="noreferrer">RFC 2217</a> covers a com port to TCP/IP standard that allows a client on one system to emulate a serial port to the local programs, while transparently sending and receiving data and control signals to a server on another system which actually has the serial port.  Here\'s a <a href="http://pcmicro.com/support/rfc2217.html" rel="noreferrer">high-level overview</a>.</p>\n\n<p>What you would do is find or implement a client com port driver that would implement the client side of the system on your PC - appearing to be a real serial port but in reality shuttling everything to a server.  You might be able to get this driver for free from Digi, Lantronix, etc in support of their real standalone serial port servers.</p>\n\n<p>You would then implement the server side of the connection locally in another program - allowing the client to connect and issuing the data and control commands as needed.</p>\n\n<p>It\'s probably non trivial, but the RFC is out there, and you might be able to find an open source project that implements one or both sides of the connection.</p>\n\n<h2>Modify the linux serial port driver</h2>\n\n<p>Alternately, the serial port driver source for Linux is readily available.  Take that, gut the hardware control pieces, and have that one driver run two /dev/ttySx ports, as a simple loopback.  Then connect your real program to the ttyS2 and your simulator to the other ttySx.</p>\n\n<h2>Use two USB&lt;-->Serial cables in a loopback</h2>\n\n<p>But the easiest thing to do right now?  Spend $40 on two serial port USB devices, wire them together (null modem) and actually have two real serial ports - one for the program you\'re testing, one for your simulator.</p>\n\n<p>-Adam</p>\n'}, {'answer_id': 74517, 'author': 'apenwarr', 'author_id': 42219, 'author_profile': 'https://Stackoverflow.com/users/42219', 'pm_score': 7, 'selected': True, 'text': '<p>You can use a pty ("pseudo-teletype", where a serial port is a "real teletype") for this.  From one end, open <code>/dev/ptyp5</code>, and then attach your program to <code>/dev/ttyp5</code>; <code>ttyp5</code> will act just like a serial port, but will send/receive everything it does via /dev/ptyp5.</p>\n\n<p>If you really need it to talk to a file called <code>/dev/ttys2</code>, then simply move your old <code>/dev/ttys2</code> out of the way and make a symlink from <code>ptyp5</code> to <code>ttys2</code>.</p>\n\n<p>Of course you can use some number other than <code>ptyp5</code>.  Perhaps pick one with a high number to avoid duplicates, since all your login terminals will also be using ptys.</p>\n\n<p>Wikipedia has more about ptys: <a href="http://en.wikipedia.org/wiki/Pseudo_terminal" rel="noreferrer">http://en.wikipedia.org/wiki/Pseudo_terminal</a></p>\n'}, {'answer_id': 330518, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>You may want to look at <a href="http://www.tibbo.com/vspdl.php" rel="noreferrer">Tibbo VSPDL</a> for creating a linux virtual serial port using a Kernel driver -- it seems pretty new, and is available for download right now (beta version). Not sure about the license at this point, or whether they want to make it available commercially only in the future.</p>\n\n<p>There are other commercial alternatives, such as <a href="http://www.ttyredirector.com/" rel="noreferrer">http://www.ttyredirector.com/</a>.</p>\n\n<p>In Open Source, <a href="http://lpccomp.bc.ca/remserial/" rel="noreferrer">Remserial</a> (GPL) may also do what you want, using Unix PTY\'s. It transmits the serial data in "raw form" to a network socket; STTY-like setup of terminal parameters must be done when creating the port, changing them later like described in RFC 2217 does not seem to be supported. You should be able to run two remserial instances to create a virtual nullmodem like com0com, except that you\'ll need to set up port speed etc in advance.</p>\n\n<p><a href="http://www.dest-unreach.org/socat/" rel="noreferrer">Socat</a> (also GPL) is like an extended variant of Remserial with many many more options, including a "PTY" method for redirecting the PTY to something else, which can be another instance of Socat. For Unit tets, socat is likely nicer than remserial because you can directly cat files into the PTY. See the <a href="http://www.dest-unreach.org/socat/doc/socat.html#EXAMPLE_ADDRESS_PTY" rel="noreferrer">PTY example</a> on the manpage. A <a href="http://www.dest-unreach.org/socat/contrib/socat-rfc2217.html" rel="noreferrer">patch exists</a> under "contrib" to provide RFC2217 support for negotiating serial line settings.</p>\n'}, {'answer_id': 9446954, 'author': 'Mauro Ciancio', 'author_id': 515483, 'author_profile': 'https://Stackoverflow.com/users/515483', 'pm_score': 3, 'selected': False, 'text': '<p>Using the links posted in the previous answers, I coded a little example in C++ using a Virtual Serial Port. I pushed the code into GitHub: <a href="https://github.com/cymait/virtual-serial-port-example" rel="noreferrer">https://github.com/cymait/virtual-serial-port-example</a> .</p>\n\n<p>The code is pretty self explanatory. First, you create the master process by running ./main master and it will print to stderr the device is using. After that, you invoke ./main slave device, where device is the device printed in the first command.</p>\n\n<p>And that\'s it. You have a bidirectional link between the two process.</p>\n\n<p>Using this example you can test you the application by sending all kind of data, and see if it works correctly.</p>\n\n<p>Also, you can always symlink the device, so you don\'t need to re-compile the application you are testing.</p>\n'}, {'answer_id': 10555885, 'author': 'slonik', 'author_id': 1384839, 'author_profile': 'https://Stackoverflow.com/users/1384839', 'pm_score': 6, 'selected': False, 'text': '<p>Use socat for this:</p>\n\n<p>For example:</p>\n\n<pre><code>socat PTY,link=/dev/ttyS10 PTY,link=/dev/ttyS11\n</code></pre>\n'}, {'answer_id': 11374412, 'author': 'Peter Remmers', 'author_id': 255776, 'author_profile': 'https://Stackoverflow.com/users/255776', 'pm_score': 4, 'selected': False, 'text': '<p>There is also tty0tty <a href="http://sourceforge.net/projects/tty0tty/" rel="nofollow noreferrer">http://sourceforge.net/projects/tty0tty/</a> which is a real null modem emulator for linux.</p>\n<p>It is a simple kernel module - a small source file. I don\'t know why it only got thumbs down on sourceforge, but it works well for me. The best thing about it is that is also emulates the hardware pins (RTC/CTS DSR/DTR). It even implements TIOCMGET/TIOCMSET and TIOCMIWAIT iotcl commands!</p>\n<p>On a recent kernel you may get compilation errors. This is easy to fix. Just insert a few lines at the top of the module/tty0tty.c source (after the includes):</p>\n<pre><code>#ifndef init_MUTEX\n#define init_MUTEX(x) sema_init((x),1)\n#endif\n</code></pre>\n<p>When the module is loaded, it creates 4 pairs of serial ports. The devices are /dev/tnt0 to /dev/tnt7 where tnt0 is connected to tnt1, tnt2 is connected to tnt3, etc.\nYou may need to fix the file permissions to be able to use the devices.</p>\n<p>edit:</p>\n<p>I guess I was a little quick with my enthusiasm. While the driver looks promising, it seems unstable. I don\'t know for sure but I think it crashed a machine in the office I was working on from home. I can\'t check until I\'m back in the office on monday.</p>\n<p>The second thing is that TIOCMIWAIT does not work. The code seems to be copied from some &quot;tiny tty&quot; example code. The handling of TIOCMIWAIT seems in place, but it never wakes up because the corresponding call to wake_up_interruptible() is missing.</p>\n<p>edit:</p>\n<p>The crash in the office really was the driver\'s fault. There was an initialization missing, and the completely untested TIOCMIWAIT code caused a crash of the machine.</p>\n<p>I spent yesterday and today rewriting the driver. There were a lot of issues, but now it works well for me. There\'s still code missing for hardware flow control managed by the driver, but I don\'t need it because I\'ll be managing the pins myself using TIOCMGET/TIOCMSET/TIOCMIWAIT from user mode code.</p>\n<p>If anyone is interested in my version of the code, send me a message and I\'ll send it to you.</p>\n'}, {'answer_id': 19733677, 'author': 'cantoni', 'author_id': 2236741, 'author_profile': 'https://Stackoverflow.com/users/2236741', 'pm_score': 8, 'selected': False, 'text': '<p>Complementing the @slonik\'s answer.</p>\n\n<p>You can test socat to create Virtual Serial Port doing the following procedure (tested on Ubuntu 12.04):</p>\n\n<p><strong>Open a terminal (let\'s call it Terminal 0) and execute it:</strong></p>\n\n<pre><code>socat -d -d pty,raw,echo=0 pty,raw,echo=0\n</code></pre>\n\n<p><strong>The code above returns:</strong></p>\n\n<pre><code>2013/11/01 13:47:27 socat[2506] N PTY is /dev/pts/2\n2013/11/01 13:47:27 socat[2506] N PTY is /dev/pts/3\n2013/11/01 13:47:27 socat[2506] N starting data transfer loop with FDs [3,3] and [5,5]\n</code></pre>\n\n<p><strong>Open another terminal and write (Terminal 1):</strong></p>\n\n<pre><code>cat &lt; /dev/pts/2\n</code></pre>\n\n<p>this command\'s port name can be changed according to the pc. it\'s depends on the previous output.</p>\n\n<pre><code>2013/11/01 13:47:27 socat[2506] N PTY is /dev/pts/**2**\n2013/11/01 13:47:27 socat[2506] N PTY is /dev/pts/**3**\n2013/11/01 13:47:27 socat[2506] N starting data transfer loop with FDs \n</code></pre>\n\n<p>you should use the number available on highlighted area.</p>\n\n<p><strong>Open another terminal and write (Terminal 2):</strong></p>\n\n<pre><code>echo "Test" &gt; /dev/pts/3\n</code></pre>\n\n<p>Now back to Terminal 1 and you\'ll see the string "Test".</p>\n'}, {'answer_id': 70152395, 'author': 'Sadeesh Kalhara', 'author_id': 10341589, 'author_profile': 'https://Stackoverflow.com/users/10341589', 'pm_score': 2, 'selected': False, 'text': '<pre><code>$ socat -d -d pty,link=/tmp/vserial1,raw,echo=0 pty,link=/tmp/vserial2,raw,echo=0\n</code></pre>\n<p>Will generate symlinks of <code>/tmp/vserial1</code> and <code>/tmp/vserial2</code> for generated virtual serial ports in <code>/dev/pts/*</code></p>\n<p><a href="https://unix.stackexchange.com/questions/374916/how-to-use-virtual-serial-port-without-root-privileges?newreg=9b02ebd5b0f7498399a49b1ad51d558d">Resource</a></p>\n'}, {'answer_id': 73639043, 'author': 'Dan2theR', 'author_id': 2251217, 'author_profile': 'https://Stackoverflow.com/users/2251217', 'pm_score': 1, 'selected': False, 'text': '<p>Combining all other amazingly useful answers, I found the below command to be VERY useful for testing on different types of Linux distros where there\'s no  guarantee you\'re <a href="https://unix.stackexchange.com/questions/590302/getting-the-virtual-comport-addr-from-socat-command-with-grep?newreg=03ecb0aa86ea426d847f58952f48aa1b">going to get the same /dev/pts/#\'s every time</a> and/or you need to test multiple psuedo serial devices and connections at once.</p>\n<pre><code>parallel \'i=&quot;{1}&quot;; socat -d -d pty,raw,echo=0,link=$HOME/pty{1} pty,raw,echo=0,link=$HOME/pty$(($i+1))\' ::: $(seq 0 2 3;)\n</code></pre>\n<p>Breaking this down:</p>\n<p><code>parallel</code> runs the same command for each argument supplied to it.\nSo for example if we run it with the <code>--dryrun</code> flag it gives us:</p>\n<pre><code>i=&quot;0&quot;; socat -d -d pty,raw,echo=0,link=$HOME/pty0 pty,raw,echo=0,link=$HOME/pty$(($i+1))\ni=&quot;2&quot;; socat -d -d pty,raw,echo=0,link=$HOME/pty2 pty,raw,echo=0,link=$HOME/pty$(($i+1))\n</code></pre>\n<p>This is due to the <code>$(seq x y z;)</code> at the end, where\nx = start #, y = increment by, and z = end # (or # of devices you need to spawn)</p>\n<p><code>parallel \'i=&quot;{1}&quot;; echo &quot;make psuedo_devices {1} $(($i+1))&quot;\' ::: $(seq 0 2 3;)</code></p>\n<p>Outputs:</p>\n<pre><code>make psuedo_devices 0 1\nmake psuedo_devices 2 3\n</code></pre>\n<p>Gathering all this together the final above command symlinks the proper psuedo devices together regardless of whats in /dev/pts/ to whatever directory supplied to socat via the <code>link</code> flag.</p>\n<p><code>pstree -c -a $PROC_ID</code> gives:</p>\n<pre><code>perl /usr/bin/parallel i=&quot;{1}&quot;; socat -d -d pty,raw,echo=0,link=$HOME/pty{1} pty,raw,echo=0,link=$HOME/pty$(($i+1)) ::: 0 2\n  ├─bash -c i=&quot;0&quot;; socat -d -d pty,raw,echo=0,link=$HOME/pty0 pty,raw,echo=0,link=$HOME/pty$(($i+1))\n  │   └─socat -d -d pty,raw,echo=0,link=/home/user/pty0 pty,raw,echo=0,link=/home/user/pty1\n  └─bash -c i=&quot;2&quot;; socat -d -d pty,raw,echo=0,link=$HOME/pty2 pty,raw,echo=0,link=$HOME/pty$(($i+1))\n      └─socat -d -d pty,raw,echo=0,link=/home/user/pty2 pty,raw,echo=0,link=/home/user/pty3\n</code></pre>\n<p>ls -l $HOME/pty* yield:</p>\n<pre><code>lrwxrwxrwx 1 user user 10 Sep  7 11:46 /home/user/pty0 -&gt; /dev/pts/4\nlrwxrwxrwx 1 user user 10 Sep  7 11:46 /home/user/pty1 -&gt; /dev/pts/6\nlrwxrwxrwx 1 user user 10 Sep  7 11:46 /home/user/pty2 -&gt; /dev/pts/7\nlrwxrwxrwx 1 user user 10 Sep  7 11:46 /home/user/pty3 -&gt; /dev/pts/8\n</code></pre>\n<p>This was all because I was trying running tests against a platform where I needed to generated a lot of mach-serial connections and to test their input/output via containerization (Docker). Hopefully someone finds it useful.</p>\n'}]
['https://Stackoverflow.com/questions/52187', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/445087/']