我有一个奇怪的行为,我的应用程序。在独立模式下的SerialPort超时
我打开一个COM端口,通过蓝牙与设备通信。我执行以下步骤:
- 打开虚拟COM端口;
- 将遥控蓝牙切换到命令模式;
- 执行一些命令(例如,读取远程设备的序列号);
- 将远程蓝牙切换到数据模式;
- 发送数据到设备;
从串口读取一个字节的答案(ReadByte());
该设备工作正常,立即回答,一切都很好,而我通过Visual Studio在调试模式下运行我的应用程序。
但是,当我尝试直接运行(不Visual Studio和adebugger attatched - 但仍与“调试”选项编译)我会在第6步
超时异常的错误是完全可再现(在Visual Studio中没有超时,每次没有它)。
有没有人有任何想法,可能会导致此类行为?
这里是从第6步
private byte[] ReadResponse() {
try {
int bytes2Read = 6;
do {
this.buffer.Append((byte)ReadByte()); // <- there the timeout occurs
if (this.buffer.Length == 6) { // header receiver
// bytes 2 and 3 contain message length
bytes2Read = this.buffer[2] + (this.buffer[3] << 8);
}
} while (this.buffer.Length < bytes2Read);
return this.buffer.ToArray();
} finally {
this.buffer.Clear();
}
}
的方法,位于从SerialPort类导出类的代码。
顺便说一句 - 串行端口的ReadTimeout属性是2秒所以很长eanough(以毫秒为单位的设备应答)。 – 2010-01-11 13:20:16
仍然,显示在6)中读取的代码。节省很多猜测。 – 2010-01-11 13:26:42