我上周发布了一个类似的问题,但我的代码中出现了一些错误,我认为我现在已修复了 .

我有一台设备通过USB(cdc-acm)以10 fps的帧速率发送80x80 16位图像 .

为了获得这些图像,我编写了一些我在Ubuntu笔记本电脑上测试的代码,以及Raspberry Pi 3模型B.

代码在两个设备上工作正常(意味着我得到正确的图像),但在Raspberry Pi上,帧率非常低!我最多只能达到5 fps .

从串行端口读取图像的代码部分在主程序的分离线程中运行 . 与此同时,覆盆子Pi(运行Raspbian Desktop)的“CPU使用率监视器”向我显示,只使用了大约20%(有时高达31%)的CPU,因此应该仍然有一些处理能力可供使用 . 引擎盖,对吗?

有谁知道问题可以从哪里来?

因为它可能来自我在代码中处理串行通信部分的方式,下面的代码显示了我是如何做到的 . 请注意,正如我在之前的帖子中建议的那样,我尝试使用不同的波特率,它根本不会改变任何东西 .

/// INITIALISATION

// Try to open the serial port
int m_file_descriptor = open("/dev/ttyACM0", O_RDWR | O_NOCTTY /*| O_NDELAY*/);
// Failed to open
if (m_file_descriptor == -1)
{
    std::cerr << "Unable to open serial port" << std::endl;
}
// If success, configure serial like expected
else
{ 
    fcntl(m_file_descriptor, F_SETFL, 0);

    struct termios settings;
    tcgetattr(m_file_descriptor, &settings);  // Get current default settings
    cfmakeraw(&settings);  // Raw serial communication (custom bytes sending and parsing)
    //cfsetspeed(&settings, B115200); // Setting baud rates doesn't change anything

    // Now clean and set params
    tcflush( m_file_descriptor, TCIFLUSH );
    tcsetattr( m_file_descriptor, TCSANOW, &settings );
}


/// Getting Data : main loop of the serial com thread

unsigned char read_buffer[128];

while (m_isListening)
{
    int rd = read(m_file_descriptor, read_buffer, 128); // Blocking read

    // Octets were read
    if (rd > 0)
    {
        /// Processing read_buffer basically copy the pixels' octets into a class member image data buffer
    }
    else if (rd == 0)
    {
        std::cout << "No bytes read" << std::endl;
    }
    else
    {
        printf("%d %s \n", errno, strerror(errno));
    }
}

EDIT :

基于评论,它可能来自于Raspberry Pi上的USB速度限制为USB 1.1速度 .

在树莓派上,当我插入设备时, dmesg 命令返回的第一行是:

usb 1-1.4: new full-speed USB device number 6 using dwc_otg

而在Ubuntu笔记本电脑上它是:

usb 1-6: new full-speed USB device number 5 using xhci_hcd

我对 dwc_otgxhci_hcd 了解不多,但我想问题就在这里......任何人都有想法?

EDIT 2 :

显然,USB速度问题(特别是对非常见设备的速度重新分配)在Raspberry Pi上很常见 . 我找到了一个似乎使我的设备以10 fps工作的解决方法:

我添加了: dwc_otg.speed=1 到文件 /boot/cmdline.txt . 根据我在互联网上发现的情况,这将限制USB速度达到全速12Mbps(USB 1.1) .

这增加了我的设备的帧率,但问题是,使用线路,插入Raspberry的鼠标和键盘不再响应 .