gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:00:36 25 4
gpt4 key购买 nike

我可以打开串行端口,但我无法正确配置此端口进行写入 (/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

最佳答案

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

  1. 由于串行连接上的自动重置在大多数开发板上默认处于激活状态,如果您想使用最后一个命令而不是终端仿真器(arduino IDE、屏幕、picocom...)直接与您的开发板通信,则需要禁用此功能。 ).如果你有一 block Leonardo 板,你不必担心这一点,因为它不会自动重置。如果您有 Uno 板,请在 RESET 和 GND 引脚之间连接一个 10 µF 电容器。如果您有另一 block 板,请在 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;

关于c++ - 无法在 Ubuntu、C++ 上配置设备 ttyUSB0 (Arduino),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33373226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com