简体   繁体   English

为什么花费比第一次打开USB2Serial端口更长的时间

[英]why it is costing longer than the first time to open USB2Serial port

I am testing the time cost of opening a USB2Serial port n Centos 7.4. 我正在测试打开Centos 7.4的USB2Serial端口的时间成本。 But I found it cost about 7ms for the first time. 但是我发现它第一次花费了大约7毫秒。 But it cost much more time in the next opening. 但这在下一次开放中花费了更多时间。 If I increase the sleep time, run again, it costs more time except for the first open. 如果增加睡眠时间,请再次运行,除了第一次打开外,它会花费更多时间。

I am using usb2serial device from FTDI, the kernel driver is ftdi_sio. 我正在使用FTDI的usb2serial设备,内核驱动程序是ftdi_sio。

Here is my code: 这是我的代码:

for (int i = 0; i < 10; i++)
{
    steady_clock::time_point start = steady_clock::now();
    int handle = open("/dev/ttyUSB0", O_RDWR| O_NONBLOCK | O_NDELAY);
    steady_clock::time_point end = steady_clock::now();
    std::cout << "Item " << i <<  ":" << " " << duration_cast<std::chrono::nanoseconds>(end-start).count()/1000000 << " ms" << endl;
    usleep(10000); // us
    close(handle);
}

The result is: 结果是:

Item 0: 6 ms 项目0:6毫秒

Item 1: 76 ms 项目1:76毫秒

Item 2: 75 ms 项目2:75毫秒

Item 3: 75 ms 项目3:75毫秒

Item 4: 75 ms 项目4:75毫秒

Item 5: 76 ms 项目5:76毫秒

Item 6: 75 ms 项目6:75毫秒

Item 7: 75 ms 项目7:75毫秒

Item 8: 75 ms 项目8:75毫秒

Item 9: 74 ms 项目9:74毫秒

I just wonder why the open time becomes longer after the first time. 我只是想知道为什么开放时间在第一次之后会变长。 Maybe need some other operation before close. 关闭前可能需要其他操作。

Anyone has met similar problem ? 有人遇到过类似的问题吗? Or any comments? 或有何评论? Thanks 谢谢

std::chrono::duration_cast returns a std::chrono::duration object . std::chrono::duration_cast返回一个std::chrono::duration 对象

The printf function is an old C compatibility function, and as such knows nothing about C++ objects. printf函数是旧的C兼容性函数,因此对C ++对象一无所知

In short, what you're printing is not really the duration, instead you have undefined behavior! 简而言之,您要打印的不是持续时间,而是行为不确定! Considering you're on Ubuntu, then you're using either GCC or Clang, both compilers which should complain about that. 考虑到您使用的是Ubuntu,那么您使用的是GCC或Clang,这两个编译器都应该对此抱怨。

If you want to print the actual duration "count" then use the count member function, preferably together with std::cout to get type-safe conversions: 如果要打印实际持续时间“ count”,请使用count成员函数,最好与std::cout一起使用以进行类型安全的转换:

std::cout << "Item " << i << ": " << duration_cast<std::chrono::nanoseconds>(e1-s1).count() << " ns\n";

Or if your compiler can handle some of the changes in the upcoming C++20 standard (which defines an operator<< overload for durations): 或者,如果您的编译器可以处理即将到来的C ++ 20标准(其中定义了operator<<持续时间的重载)中的某些更改,则:

std::cout << "Item " << i << ": " << duration_cast<std::chrono::nanoseconds>(e1-s1) << " ns\n";

The big lesson: Don't mix C++ and old C functions. 重要的教训:不要混用C ++和旧的C函数。 They seldom mix very well. 它们很少混合得很好。

You might also want to consider getting a few good books to help you learn C++ properly. 您可能还需要考虑购买一些好书,以帮助您正确学习C ++。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM