独立模式下的串行端口超时

发布于 2024-08-17 14:53:09 字数 1137 浏览 4 评论 0原文

我的应用程序有一个奇怪的行为。

我打开一个 COM 端口来通过蓝牙与设备进行通信。我执行以下步骤:

  1. 打开虚拟 COM 端口;
  2. 将远程蓝牙切换到命令模式;
  3. 执行少量命令(例如读取远程设备的序列号);
  4. 将远程蓝牙切换至数据模式;
  5. 向设备发送数据;
  6. 读取一个字节的答案(来自 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:

  1. Open the virtual COM port;
  2. Switch remote bluetooth to command mode;
  3. Perform few commands (eg. read remote device's serial number);
  4. Switch remote bluetooth to data amode;
  5. Send data to device;
  6. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我一直都在从未离去 2024-08-24 14:53:09

当您进行调试时,您会给端口驱动程序大量的时间来接收字节。直到您跳过 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.

百合的盛世恋 2024-08-24 14:53:09

某些虚拟 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

少年亿悲伤 2024-08-24 14:53:09

超时是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

少女七分熟 2024-08-24 14:53:09

听起来像是一个时间问题。在调试模式下,程序运行速度比没有附加调试器时慢。一定有时间问题

sounds like a timing problem. In Debug Mode the program rungs slower than without an attached Debugger. There must be a timing issue

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文