我正在调试一些使用串口的单声道代码。 在某些时候,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类以使其正常运行或以阻塞方式使用串口?

Mono 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
有帮助吗?

解决方案

考虑升级到更新的版本。

该错误已修复此处

其他提示

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

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.");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top