独立模式下的串行端口超时
我的应用程序有一个奇怪的行为。
我打开一个 COM 端口来通过蓝牙与设备进行通信。我执行以下步骤:
- 打开虚拟 COM 端口;
- 将远程蓝牙切换到命令模式;
- 执行少量命令(例如读取远程设备的序列号);
- 将远程蓝牙切换至数据模式;
- 向设备发送数据;
读取一个字节的答案(来自 SerialPort 类的 ReadByte());
设备工作正常,立即回答,当我通过 Visual Studio 在调试模式下运行我的应用程序时,一切都很好。
但是,当我尝试直接运行它(没有附加 Visual Studio 和 adebugger - 但仍然使用“调试”选项进行编译)时,我在第 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 类派生的类中。
I have a strange behaviour im my app.
I open a COM port to comunicate witha a device over Bluetooth. I do the following steps:
- Open the virtual COM port;
- Switch remote bluetooth to command mode;
- Perform few commands (eg. read remote device's serial number);
- Switch remote bluetooth to data amode;
- Send data to device;
Read a byte of answer (ReadByte() from SerialPort class);
The device works fine and answers immediately and everything is fine while I run my app in debug mode via visual studio.
But when I try to run it directly (without visual studio and adebugger attatched - but still compiled with "Debug" option) i get a timeout exception at step 6.
The bug is fully reproducible (no timeouts in Visual Studio, and every time without it).
Does anyone have any idea that could cause such behaviour ?
Here is the code from step 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();
}
}
The method is situated in the class that derives from SerialPort class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您进行调试时,您会给端口驱动程序大量的时间来接收字节。直到您跳过 ReadByte() 调用后,超时计时器才会开始运行,驱动程序可能已经接收到该字节,因此 ReadByte() 会立即返回。当你全速奔跑时,这种情况不会发生。
增加 ReadTimeout 属性的值。还可以考虑改用 DataReceived 事件。
When you are debugging, you are giving the port driver lots of time to receive a byte. The timeout timer doesn't start running until you step over the ReadByte() call, the driver probably already has received the byte so ReadByte() returns immediately. This doesn't happen when you run at full speed.
Increase the value of the ReadTimeout property. Also consider using the DataReceived event instead.
某些虚拟 COM 端口似乎存在错误,在写入后立即超时。您可以通过在写入后插入延迟(尝试 10-20 毫秒)来部分解决此问题,但我还没有找到好的解决方案。
这里是关于错误的讨论以太网-> RS232虚拟COM口
There seems to be a bug in some virtual COM ports with immediate timeouts immediately after a write. You can partially work around it by inserting a delay after the write (try 10-20 milliseconds), but I haven't found a good solution yet.
Here is discussion on the bug in an ethernet-> RS232 virtual com port
超时是
SerialPort
的正常行为。它可以防止您的应用程序停滞。您应该能够修改
ReadTimeout
属性以增加超时所需的时间。http://msdn.microsoft.com/ en-us/library/system.io.ports.serialport.readtimeout.aspx
A timeout is normal behavior for the
SerialPort
. It prevents your application from stalling.You should be able to modify the
ReadTimeout
property to increase the time it takes to timeout.http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.readtimeout.aspx
听起来像是一个时间问题。在调试模式下,程序运行速度比没有附加调试器时慢。一定有时间问题
sounds like a timing problem. In Debug Mode the program rungs slower than without an attached Debugger. There must be a timing issue