title: Unable to open /dev/ttyUSB0 while debugging with Visual Studio Code on Linux
body: <p>I have noticed that attempts to open /dev/ttyUSB0 will fail when I am running my code with the VSCode debugger. </p>
  
  <p>The same code will work correctly when I run the program in gdb on the command line.</p>
  
  <p>main.cpp:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fcntl.h&gt;
  
  int main(int argc, char** argv){
      int reg_fd = open("other_file.txt", O_RDWR);
      int dev_fd = open("/dev/ttyUSB0", O_RDWR); 
      std::cout &lt;&lt; "reg_fd: " &lt;&lt; reg_fd &lt;&lt; std::endl;
      std::cout &lt;&lt; "dev_fd: " &lt;&lt; dev_fd &lt;&lt; std::endl;
      return 0;
  }
  </code></pre>
  
  <p>compiled with:</p>
  
  <pre><code>g++ -g ./main.cpp
  </code></pre>
  
  <p>When I debug with gbd from command line it works fine:</p>
  
  <pre><code>$gdb ./a.out
  GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
  Copyright (C) 2018 Free Software Foundation, Inc.
  License GPLv3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;
  This is free software: you are free to change and redistribute it.
  There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
  and "show warranty" for details.
  This GDB was configured as "x86_64-linux-gnu".
  Type "show configuration" for configuration details.
  For bug reporting instructions, please see:
  &lt;http://www.gnu.org/software/gdb/bugs/&gt;.
  Find the GDB manual and other documentation resources online at:
  &lt;http://www.gnu.org/software/gdb/documentation/&gt;.
  For help, type "help".
  Type "apropos word" to search for commands related to "word"...
  Reading symbols from ./a.out...done.
  (gdb) run
  Starting program: /home/bob/stackoverflowQ/a.out 
  reg_fd: 3
  dev_fd: 4
  [Inferior 1 (process 20129) exited normally]
  (gdb) 
  </code></pre>
  
  <p>When run inside VSCode debugger I get:</p>
  
  <pre><code>reg_fd: 3
  dev_fd: -1
  </code></pre>
  
  <p>Here is my launch.json:</p>
  
  <pre><code>{
      // Use IntelliSense to learn about possible attributes.
      // Hover to view descriptions of existing attributes.
      // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
      "version": "0.2.0",
      "configurations": [
          {
              "name": "(gdb) Launch",
              "type": "cppdbg",
              "request": "launch",
              "program": "${workspaceFolder}/a.out",
              "args": [],
              "stopAtEntry": false,
              "cwd": "${workspaceFolder}",
              "environment": [],
              "externalConsole": true,
              "MIMode": "gdb",
              "setupCommands": [
                  {
                      "description": "Enable pretty-printing for gdb",
                      "text": "-enable-pretty-printing",
                      "ignoreFailures": true
                  }
              ]
          }
      ]
  }
  </code></pre>
  
  <p>If I replace "/dev/ttyUSB0" with "/dev/urandom" then both the gdb commandline and VSCode debugger work fine. </p>
  
  <p>This makes me think the problem might be related to some kind of environment variable or environment setup that I need to specify in the launch file. </p>
  
  <p>Does anyone know how I can get this working in VSCode? </p>
  
  <p>Thanks.</p>
  
  <p>Edits: </p>
  
  <ol>
  <li>In case anyone finds this and the question remains unanswered. I have found that it is possible to start my program on the command line and then have the VSCode C++ debugger attach to the running process. When the debugging session is established in this way the opening of /dev/ttyUSB0 was successful.</li>
  <li>For clarification, I'm running this on an Ubuntu 18.04 machine with Visual Studio Code on native Linux.</li>
  <li>My user is part of the dialout group.</li>
  </ol>