Mono System.IO.Ports SerialPort 类错误处理

发布于 2024-07-18 10:18:23 字数 2334 浏览 7 评论 0原文

我正在调试一些使用串行端口的单声道代码。 在某些时候,mono 用以下代码编写一个表:

   // Send the 1024 byte (256 word) CRC table

    progressBar = new ProgressBar();

    progressBar.Update(0.0,"Sending CRC table...");

    for (int i = 0; i < MyCRC.Length; i++)

    {

        MySP.Write(MyCRC[i].ToString("x8"));

        progressBar.Percent = (((Double)(i+1))/MyCRC.Length);

    }

    progressBar.Update(100.0,"CRC table sent.");

MySP 是一个 SerialPort 实例。 然而,当我使用 strace 跟踪此代码时,我认为结果是系统调用:

16620 write(3, "3ab551ce", 8)           = -1 EAGAIN (Resource temporarily unavailable)
16620 write(3, "\0003ab551c", 8)        = -1 EAGAIN (Resource temporarily unavailable)
16620 write(3, "\0\0003ab551", 8)       = -1 EAGAIN (Resource temporarily unavailable)
16620 write(3, "\0\0\0003ab55", 8)      = -1 EAGAIN (Resource temporarily unavailable)
16620 write(3, "\10\0\0\0003ab5", 8)    = -1 EAGAIN (Resource temporarily unavailable)
16620 write(3, "\0\10\0\0\0003ab", 8)   = -1 EAGAIN (Resource temporarily unavailable)

...

16620 write(3, "\0005\0\230O+\10\0", 8) = -1 EAGAIN (Resource temporarily unavailable)
16620 write(3, "E\0005\0\230O+\10", 8)  = -1 EAGAIN (Resource temporarily unavailable)
16620 write(3, "\0E\0005\0\230O+", 8)   = -1 EAGAIN (Resource temporarily unavailable)
16620 write(3, "\0\0E\0005\0\230O", 8)  = -1 EAGAIN (Resource temporarily unavailable)
16620 write(3, "\0\0\0E\0005\0\230", 8) = -1 EAGAIN (Resource temporarily unavailable)
16620 write(3, "4\0\0\0E\0005\0", 8)    = 8
16620 write(3, "\230O+\10\0\0\0\0", 8)  = 8
16620 write(3, "\0\0\0\0\10\0\0\0", 8)  = -1 EAGAIN (Resource temporarily unavailable)

我的理解是 SerialPort Write 方法不能正确处理 -EAGAIN 情况,并在重新启动写入之前将索引更新为 -1。 因为每次尝试后,原始缓冲区的内容都会移动一个字节。

我的问题是,这是一个已知问题吗?如何修改 SerialPort 类,使其行为正确或以阻塞方式使用串行端口?

SerialPort 类的 Mono 文档 不是很有帮助

附加信息:单声道-V 输出:

Mono JIT compiler version 1.2.6 (tarball)
Copyright (C) 2002-2007 Novell, Inc and Contributors. www.mono-project.com
    TLS:           __thread
    GC:            Included Boehm (with typed GC)
    SIGSEGV:       altstack
    Notifications: epoll
    Architecture:  x86
    Disabled:      none

I am debugging some mono code that is using the serial port.
At some point the mono write a table with the following code :

   // Send the 1024 byte (256 word) CRC table

    progressBar = new ProgressBar();

    progressBar.Update(0.0,"Sending CRC table...");

    for (int i = 0; i < MyCRC.Length; i++)

    {

        MySP.Write(MyCRC[i].ToString("x8"));

        progressBar.Percent = (((Double)(i+1))/MyCRC.Length);

    }

    progressBar.Update(100.0,"CRC table sent.");

MySP is a SerialPort instance.
When I trace this code using strace however, here is what I think are the resulting system calls :

16620 write(3, "3ab551ce", 8)           = -1 EAGAIN (Resource temporarily unavailable)
16620 write(3, "\0003ab551c", 8)        = -1 EAGAIN (Resource temporarily unavailable)
16620 write(3, "\0\0003ab551", 8)       = -1 EAGAIN (Resource temporarily unavailable)
16620 write(3, "\0\0\0003ab55", 8)      = -1 EAGAIN (Resource temporarily unavailable)
16620 write(3, "\10\0\0\0003ab5", 8)    = -1 EAGAIN (Resource temporarily unavailable)
16620 write(3, "\0\10\0\0\0003ab", 8)   = -1 EAGAIN (Resource temporarily unavailable)

...

16620 write(3, "\0005\0\230O+\10\0", 8) = -1 EAGAIN (Resource temporarily unavailable)
16620 write(3, "E\0005\0\230O+\10", 8)  = -1 EAGAIN (Resource temporarily unavailable)
16620 write(3, "\0E\0005\0\230O+", 8)   = -1 EAGAIN (Resource temporarily unavailable)
16620 write(3, "\0\0E\0005\0\230O", 8)  = -1 EAGAIN (Resource temporarily unavailable)
16620 write(3, "\0\0\0E\0005\0\230", 8) = -1 EAGAIN (Resource temporarily unavailable)
16620 write(3, "4\0\0\0E\0005\0", 8)    = 8
16620 write(3, "\230O+\10\0\0\0\0", 8)  = 8
16620 write(3, "\0\0\0\0\10\0\0\0", 8)  = -1 EAGAIN (Resource temporarily unavailable)

My understanding is that the SerialPort Write method dos not handle the -EAGAIN case correctly and updates the index by -1 before restarting the write. because after each try, the content of the original buffer is shifted by one byte.

My question is, is it a known problem, and how can I modify the SerialPort class so that it behaves correctly or use the serial port in a blocking way ?

The Mono documentation for the SerialPort class is not very helpful

Additional Info : mono -V output:

Mono JIT compiler version 1.2.6 (tarball)
Copyright (C) 2002-2007 Novell, Inc and Contributors. www.mono-project.com
    TLS:           __thread
    GC:            Included Boehm (with typed GC)
    SIGSEGV:       altstack
    Notifications: epoll
    Architecture:  x86
    Disabled:      none

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

娇女薄笑 2024-07-25 10:18:23

考虑升级到更新版本。

该错误已此处修复< /a>.

Consider upgrading to a newer version.

The bug was fixed here.

疑心病 2024-07-25 10:18:23

我有一个解决方法,但我不认为这是一个好的解决方案:

progressBar = new ProgressBar();

progressBar.Update(0.0,"Sending CRC table...");

for (int i = 0; i < MyCRC.Length; i++)

{

    MySP.Write(MyCRC[i].ToString("x8"));
    while(WySP.BytesToWrite != 0)
    {
        ;
    }
    progressBar.Percent = (((Double)(i+1))/MyCRC.Length);

}

progressBar.Update(100.0,"CRC table sent.");

I have a workaround, but I don't consider it is a good solution :

progressBar = new ProgressBar();

progressBar.Update(0.0,"Sending CRC table...");

for (int i = 0; i < MyCRC.Length; i++)

{

    MySP.Write(MyCRC[i].ToString("x8"));
    while(WySP.BytesToWrite != 0)
    {
        ;
    }
    progressBar.Percent = (((Double)(i+1))/MyCRC.Length);

}

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