[英]Inconsist delay of ReadFile a serial port device
I need to read data from a serial port device(which sends data per second) on Windows in REALTIME
(<= 5 ms). 我需要从一个串口设备(每秒发送的数据)在Windows中读取数据REALTIME
(<= 5毫秒)。 But the time cost by ReadFile
is unpredictable, which drives me to crazy. 但是ReadFile
花费的时间是无法预测的,这使我发疯。 Some piece of the code can be found at: https://gist.github.com/morris-stock/62b1674b4cda0e9df84d4738e54773f8 可以在以下位置找到一些代码: https : //gist.github.com/morris-stock/62b1674b4cda0e9df84d4738e54773f8
the delay is dumped at https://gist.github.com/morris-stock/62b1674b4cda0e9df84d4738e54773f8#file-serialport_win-cc-L283 延迟转储到https://gist.github.com/morris-stock/62b1674b4cda0e9df84d4738e54773f8#file-serialport_win-cc-L283
Poco::Timestamp now;
if (!ReadFile(_handle, buffer, size, &bytesRead, NULL))
throw Poco::IOException("failed to read from serial port");
Poco::Timestamp::TimeDiff elapsed = now.elapsed();
std::cout << Poco::DateTimeFormatter::format(now, "%Y-%m-%d %H:%M:%S.%i")
<< ", elapsed: " << elapsed << ", data len: " << bytesRead << std::endl << std::flush;
Sometimes ReadFile
costs about 3000 us(which is OK, affected by COMMTIMEOUTS
) to return, but sometimes, it costs 15600 us(NOT affected by COMMTIMEOUTS
). 有时, ReadFile
花费大约3000 us(可以,受COMMTIMEOUTS
影响)才能返回,但是有时,它需要15600 us(不受COMMTIMEOUTS
影响)。
Please let me know if there is anything I can do to make the problem clear. 请让我知道我有什么办法可以解决问题。
PS 聚苯乙烯
COMMTIMEOUTS : COMM超时 :
COMMTIMEOUTS cto;
cto.ReadIntervalTimeout = 1;
cto.ReadTotalTimeoutConstant = 1;
cto.ReadTotalTimeoutMultiplier = 0;
cto.WriteTotalTimeoutConstant = MAXDWORD;
cto.WriteTotalTimeoutMultiplier = 0;
the main reading thread part: 主要阅读线程部分:
https://gist.github.com/morris-stock/62b1674b4cda0e9df84d4738e54773f8#file-serialdevice-cc-L31 https://gist.github.com/morris-stock/62b1674b4cda0e9df84d4738e54773f8#file-serialdevice-cc-L31
baudrate : 9600, it sends about 400 bytes per second(continuously, then no data in the rest of the second). baudrate :9600,它每秒发送约400个字节(连续,然后在其余的秒中没有数据)。
wPacketLength: 64
wPacketVersion: 2
dwServiceMask: 1
dwReserved1: 0
dwMaxTxQueue: 0
dwMaxRxQueue: 0
dwMaxBaud: 268435456
dwProvSubType: 1
dwProvCapabilities: 255
dwSettableParams: 127
dwSettableBaud: 268959743
wSettableData: 15
wSettableStopParity: 7943
dwCurrentTxQueue: 0
dwCurrentRxQueue: 68824
dwProvSpec1: 0
dwProvSpec2: 1128813859
wcProvChar: 0039F16C
2018-01-22 03:35:52.658, elapsed: 15600, data len: 0
2018-01-22 03:35:52.673, elapsed: 15600, data len: 0
2018-01-22 03:35:52.689, elapsed: 15600, data len: 0
2018-01-22 03:35:52.704, elapsed: 15600, data len: 0
2018-01-22 03:35:52.720, elapsed: 15600, data len: 0
2018-01-22 03:35:52.736, elapsed: 15600, data len: 0
2018-01-22 03:35:52.751, elapsed: 15600, data len: 0
In my case, it's the Windows system clock resolution
that matters. 就我而言,最重要的是Windows system clock resolution
。
ClockRes gives me: ClockRes给我:
C:\work\utils\ClockRes>Clockres.exe
Clockres v2.1 - Clock resolution display utility
Copyright (C) 2016 Mark Russinovich
Sysinternals
Maximum timer interval: 15.600 ms
Minimum timer interval: 0.500 ms
Current timer interval: 1.000 ms
and 和
C:\work\utils\ClockRes>Clockres.exe
Clockres v2.1 - Clock resolution display utility
Copyright (C) 2016 Mark Russinovich
Sysinternals
Maximum timer interval: 15.600 ms
Minimum timer interval: 0.500 ms
Current timer interval: 15.600 ms
by calling timeBeginPeriod (1) when my app starts, I can get a more consistent result. 通过在我的应用程序启动时调用timeBeginPeriod (1),我可以获得更一致的结果。
Thanks everyone for your kindly help. 感谢大家的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.