无法在 Ubuntu、C++ 上配置设备 ttyUSB0 (Arduino)

Failed to configure device ttyUSB0 (Arduino) on Ubuntu, C++

我可以打开串行端口,但我无法正确配置此端口进行写入(/dev/ttyUSB0)。

一段C++代码:

int
Platform::initConnection( const char* devicePath, int baudRate )
{
        int fd = 0;
        int ret = 0;

        struct termios terminalOptions;         // POSIX structure for configurating terminal devices

        fd = open( devicePath, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );
        //fd = open( devicePath, O_RDWR | O_NOCTTY );
        if (fd == -1)
        {
                this->setFail();
                this->setErrorStr( "Failed to open: " + (std::string)devicePath + ". " + (std::string)strerror(errno) );

                return -1;
        }

        memset( &terminalOptions, 0, sizeof( struct termios ) );        // Cleaning up the structure
        cfmakeraw(&terminalOptions);                                    //

        cfsetspeed(&terminalOptions, baudRate);

        /*terminalOptions.c_cflag = CLOCAL;       // If CLOCAL is set, the line behaves as if DCD is always asserted.
                                                // It is used when your device is local

        terminalOptions.c_cflag |= CS8;         // Character size mask

        terminalOptions.c_cc[VMIN] = 24;         // 1 second timeout
        terminalOptions.c_cc[VTIME] = 0;       // */

        terminalOptions.c_cflag &= ~CRTSCTS;    
        terminalOptions.c_cflag |= (CLOCAL | CREAD);                   
        terminalOptions.c_iflag |= (IGNPAR | IGNCR);                  
        terminalOptions.c_iflag &= ~(IXON | IXOFF | IXANY);          
        terminalOptions.c_oflag &= ~OPOST;

        terminalOptions.c_cflag &= ~CSIZE;            
        terminalOptions.c_cflag |= CS8;              
        terminalOptions.c_cflag &= ~PARENB;         
        terminalOptions.c_iflag &= ~INPCK;         
        terminalOptions.c_iflag &= ~(ICRNL|IGNCR);
        terminalOptions.c_cflag &= ~CSTOPB;      
        terminalOptions.c_iflag |= INPCK;       
        terminalOptions.c_cc[VTIME] = 0.001;  //  1s=10   0.1s=1 *
        terminalOptions.c_cc[VMIN] = 0;


        ret = ioctl( fd, TIOCSETA, &terminalOptions );  // Configuring the device
        if (ret == -1)
        {
                this->setFail();
                this->setErrorStr( "Failed to configure device: " + (std::string)devicePath + ". " + (std::string)strerror(errno) );

                return -1;
        }

        return fd;
}

错误:

Failed to configure device: /dev/ttyUSB0. Inappropriate ioctl for device

Arduino UNO 使用芯片组 CH340。

我不知道如何解决这个问题。我希望得到你的帮助。谢谢!

更新: 从 dmesg

登录
[11840.346071] usb 2-1.2: new full-speed USB device number 5 using ehci-pci
[11840.439832] usb 2-1.2: New USB device found, idVendor=1a86, idProduct=7523
[11840.439844] usb 2-1.2: New USB device strings: Mfr=0, Product=2, SerialNumber=0
[11840.439850] usb 2-1.2: Product: USB2.0-Serial
[11840.440472] ch341 2-1.2:1.0: ch341-uart converter detected
[11840.442452] usb 2-1.2: ch341-uart converter now attached to ttyUSB0

我也有 arduino UNO,当我通过 USB 端口插入它时,它连接到 /dev/ttyACM0 而不是 ttyUSB0,您还应该在插拔 arduino UNO 时检查 ttyACM0。

如果你没有安装arduino端口驱动也是如此

感谢大家。我自己找到了解决方案:

  1. 由于串行连接上的自动重置在大多数开发板上默认处于激活状态,如果您想使用最后一个命令而不是终端仿真器(arduino IDE,直接与您的开发板通信,则需要禁用此功能屏幕、picocom...)。如果你有一块 Leonardo 板,你不必担心这一点,因为它不会自动重置。如果您有 Uno 板,请在 RESET 和 GND 引脚之间连接一个 10 µF 电容器。如果您有另一块板,请在 RESET 和 5V 引脚之间连接一个 120 欧姆的电阻。有关详细信息,请参阅 http://playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection
  2. С更改代码

    memset( &terminalOptions, 0, sizeof( struct termios ) );
    tcgetattr(fd, &terminalOptions);        //change
    cfmakeraw(&terminalOptions);
    cfsetspeed(&terminalOptions, baudRate);
    terminalOptions.c_cflag = CLOCAL;                                            
    terminalOptions.c_cflag |= CS8;        
    terminalOptions.c_cc[VMIN] = 0;         
    terminalOptions.c_cc[VTIME] = 10;      
    terminalOptions.c_cflag = CLOCAL;                                             
    terminalOptions.c_cflag &= ~HUPCL;       //change (disable hang-up-on-close to avoid reset)
    
    ret = tcsetattr(fd, TCSANOW, &terminalOptions);  //change
    if (ret == -1)
    {
            this->setFail();
            this->setErrorStr( "Failed to configure device: " + (std::string)devicePath + ". " + (std::string)strerror(errno) );
    
            return -1;
    }
    
    return fd;