title: Why does SerialPort.Open() fail?
body: <p>I'm trying to use a serial port to talk to a USB device, via /dev/ttyUSB0.</p>
  
  <p>So far, I've used modprobe to get it to show up as USB0, chmod to allow read and write without being root, and I've got a C library (called something like Gant) which actually works. </p>
  
  <p>However, I want to integrate it with an existing application, originally developed in C# for .Net, but (hopefully) soon to be running on Linux using mono.</p>
  
  <p>The C code uses <code>open("/dev/ttyUSB0")</code> to get a file descriptor, and then <code>read()</code> and <code>write()</code>to talk directly to the USB device.</p>
  
  <p>The equivalent in C#, as far as I can see, would be something like</p>
  
  <pre><code>SerialPort sp = new SerialPort("/dev/ttyUSB0");
  sp.Open();
  </code></pre>
  
  <p>but that gives an <code>IOException: Invalid argument at System.IO.Ports.SerialPortStream.SetSignal (SerialSignal signal, Boolean value) [0x00000] in &lt;filename unknown&gt;:0</code> </p>
  
  <p>I've tried different sets of arguments for <code>new SerialPort()</code>, and setting values subsequently with stuff like <code>sp.BaudRate = 115200</code> but as I don't know what's actually causing the error I'd have to get pretty lucky to sort it by just trying different combinations.</p>
  
  <p>From digging around in mono's source code a bit it looks like the function being called which generates the error is <code>set_signal (fd, signal, value)</code></p>
  
  <p>set_signal is an external, declared like this:</p>
  
  <pre><code>[DllImport ("MonoPosixHelper", SetLastError = true)]
  static extern int set_signal (int fd, SerialSignal signal, bool value);
  </code></pre>
  
  <p>As I understand it, fd is a file descriptor like in the C code, SerialSignal is an enum:</p>
  
  <pre><code>enum SerialSignal {
      None = 0,
      Cd = 1, // Carrier detect
      Cts = 2, // Clear to send
      Dsr = 4, // Data set ready
      Dtr = 8, // Data terminal ready
      Rts = 16 // Request to send
  }
  </code></pre>
  
  <p>and the boolean <code>value</code> is whether or not the particular SerialSignal gets set or not.</p>
  
  <p>It seems to be called twice, once to set Dtr, and once for Rts.</p>
  
  <p>So after all that my question is still why is this failing? Might there be device specific settings for DTR and RTS that I've not got right? Would it work to completely bypass the mono code, and use some C externals (which is what I think the mono source does anyway, once it gets low level enough). Could I just use some C code from within a C# mono project?</p>
  
  <p>Since I have working C code I think that last one might prove the easiest route, but even so I'd like to understand what's wrong here.</p>