title: Fake serial communication under Linux
body: <p>I have an application where I want to simulate the connection between a device and a "modem". The device will be connected to a serial port and will talk to the software modem through that.</p>
  
  <p>For testing purposes I want to be able to use a mock software device to test send and receive data.</p>
  
  <p>Example Python code</p>
  
  <pre><code>device = Device()
  modem  = Modem()
  device.connect(modem)
  
  device.write("Hello")
  modem_reply = device.read()
  </code></pre>
  
  <p>Now, in my final app I will just pass /dev/ttyS1 or COM1 or whatever for the application to use.
  But how can I do this in software? I am running <strong>Linux</strong> and application is written in <strong>Python</strong>.</p>
  
  <p>I have tried making a FIFO (<code>mkfifo ~/my_fifo</code>) and that does work, but then I'll need one FIFO for writing and one for reading. What I want is to open <code>~/my_fake_serial_port</code> and read and write to that.</p>
  
  <p>I have also lpayed with the <code>pty</code>module, but can't get that to work either. I can get a master and slave file descriptor from <code>pty.openpty()</code> but trying to read or write to them only causes <code>IOError Bad File Descriptor</code> error message.</p>
  
  <h2>Update</h2>
  
  <p>Comments pointed me to the SO question <a href="https://stackoverflow.com/questions/2175440/are-there-some-program-like-com2com-in-linux">Are there some program like COM0COM in linux?</a> which uses <code>socat</code> to setup a virtual serial connection.
  I used it like this:</p>
  
  <p><code>socat PTY,link=$HOME/COM1 PTY,link=$HOME/COM2</code></p>
  
  <p>To the rest of you, thank you for giving me valuable information.
  I chose to accept <em>Vinay Sajips</em>'s answer since that is the solution which I went for before the socat suggestion showed up. It seems to work well enough.</p>