在Linux上使用Visual Studio Code进行调试时,无法打开/ dev / ttyUSB0

时间:2018-08-26 14:47:04

标签: c++ linux visual-studio-code tty

我注意到,当我使用VSCode调试器运行代码时,尝试打开/ dev / ttyUSB0的尝试将失败。

当我在命令行中在gdb中运行程序时,相同的代码将正常工作。

main.cpp:

#include <iostream>
#include <fcntl.h>

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 << "reg_fd: " << reg_fd << std::endl;
    std::cout << "dev_fd: " << dev_fd << std::endl;
    return 0;
}

编译为:

g++ -g ./main.cpp

当我从命令行使用gbd调试时,它工作正常:

$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 <http://gnu.org/licenses/gpl.html>
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:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
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) 

在VSCode调试器中运行时,我得到:

reg_fd: 3
dev_fd: -1

这是我的launch.json:

{
    // 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
                }
            ]
        }
    ]
}

如果将“ / dev / ttyUSB0”替换为“ / dev / urandom”,则gdb命令行和VSCode调试器都可以正常工作。

这使我认为问题可能与我需要在启动文件中指定的某种环境变量或环境设置有关。

有人知道我如何才能在VSCode中使用它吗?

谢谢。

编辑:

  1. 万一有人发现这个问题,而这个问题仍然没有答案。我发现可以在命令行上启动程序,然后将VSCode C ++调试器附加到正在运行的进程中。以这种方式建立调试会话后,/ dev / ttyUSB0的打开成功。
  2. 为澄清起见,我正在本机Linux上使用Visual Studio Code的Ubuntu 18.04计算机上运行此程序。
  3. 我的用户属于拨出组。

0 个答案:

没有答案