javax.comm.CommPort enableReceiveThreshold(1)和enableReceiveTimeout的语义
多年来,我已经多次使用RXTX和PureJavaComm。我的应用程序中有以下代码:javax.comm.CommPort enableReceiveThreshold(1)和enableReceiveTimeout的语义,java,serial-port,Java,Serial Port,多年来,我已经多次使用RXTX和PureJavaComm。我的应用程序中有以下代码: SerialPort port = /* code to get serial port instance here */ port.setInputBufferSize(65536); port.enableReceiveThreshold(1); port.enableReceiveTimeout(10); (1个字符的阈值,10毫秒超时)在我得出上述公式之前,我对微调有模
SerialPort port = /* code to get serial port instance here */
port.setInputBufferSize(65536);
port.enableReceiveThreshold(1);
port.enableReceiveTimeout(10);
(1个字符的阈值,10毫秒超时)在我得出上述公式之前,我对微调有模糊的记忆,但我感到羞愧,我没有记下为什么会这样
我的目的是在没有收到数据的情况下,以最小的阻塞延迟响应InputStream.read()
调用,否则返回已经收到的字符数
我不记得为什么/是否需要调用enableCreceiveThreshold(1)
如果已经存在接收超时,则启用接收阈值(1)
和禁用接收阈值()
PureJavaComm在src/jtermios/Windows/jtermimpl.java
中有此Windows代码,其中vmin
是receiveThreshold
,而vtime
是receiveTimeout
,因此如果将vmin=0
与vmin=1
进行比较,结构会得到不同的值(中有一些详细讨论)
啊哈。委员会:
因此,您可以根据此规范选择阻塞行为。叹气。我们不能在SO中使用表标记,这不是很好吗?
COMMTIMEOUTS touts = port.m_Timeouts;
// There are really no write timeouts in classic unix termios
// FIXME test that we can still interrupt the tread
touts.WriteTotalTimeoutConstant = 0;
touts.WriteTotalTimeoutMultiplier = 0;
if (vmin == 0 && vtime == 0) {
// VMIN = 0 and VTIME = 0 => totally non blocking,if data is
// available, return it, ie this is poll operation
touts.ReadIntervalTimeout = MAXDWORD;
touts.ReadTotalTimeoutConstant = 0;
touts.ReadTotalTimeoutMultiplier = 0;
}
if (vmin == 0 && vtime > 0) {
// VMIN = 0 and VTIME > 0 => timed read, return as soon as data is
// available, VTIME = total time
touts.ReadIntervalTimeout = 0;
touts.ReadTotalTimeoutConstant = vtime;
touts.ReadTotalTimeoutMultiplier = 0;
}
if (vmin > 0 && vtime > 0) {
// VMIN > 0 and VTIME > 0 => blocks until VMIN chars has arrived or between chars expired,
// note that this will block if nothing arrives
touts.ReadIntervalTimeout = vtime;
touts.ReadTotalTimeoutConstant = 0;
touts.ReadTotalTimeoutMultiplier = 0;
}
if (vmin > 0 && vtime == 0) {
// VMIN > 0 and VTIME = 0 => blocks until VMIN characters have been
// received
touts.ReadIntervalTimeout = 0;
touts.ReadTotalTimeoutConstant = 0;
touts.ReadTotalTimeoutMultiplier = 0;
}
if (!SetCommTimeouts(port.m_Comm, port.m_Timeouts))
port.fail();