在Ubuntu, c++上配置设备ttyUSB0 (Arduino)失败

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

本文关键字:ttyUSB0 Arduino 失败 Ubuntu c++ 配置      更新时间:2023-10-16

我可以打开串行端口,但是我不能正确地配置这个端口为写(/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;
}
错误:

configure device:/dev/ttyUSB0.失败。设备的ioctl设置不合适

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

感谢大家。我自己找到了解决办法:

  1. 由于串行连接上的自动复位在大多数板上默认是激活的,如果你想用最后一个命令直接与你的板通信,而不是终端模拟器(arduino IDE, screen, picocom…),你需要禁用此功能。如果你有莱昂纳多板,你不用担心这个,因为它不会自动复位。如果您使用的是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;
    

我也有arduino UNO,当我通过usb端口插入它时,它连接到/dev/ttyACM0而不是ttyUSB0,当你插入和拔出arduino UNO时,你也应该检查ttyACM0。

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