Mono System.IO.Ports SerialPort class error handli

2019-08-11 08:28发布

问题:

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

回答1:

Consider upgrading to a newer version.

The bug was fixed here.



回答2:

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.");