3

I am finding a difference in the implementation of SerialPort.Open() between Windows and Mono on Ubuntu. Here is my current implementation:

        if (serPort.IsOpen)
        {
            serPort.Close();
        }

        serPort.BaudRate = Convert.ToInt32(baudRateCmbBox.Text);
        serPort.PortName = serPortCmbBox.Text;

        try
        {
            serPort.Open();
        }
        catch
        {
            return false;
        }

        return true;

In Windows, if I have another program with the same serial port open, then an exception is thrown and the function returns false. However, in Mono, the serial port still gets opened, regardless of whether or not another program already has the serial port open. It even reads and writes to the serial port at the same time as the other program.

So the question is, is there a way that will work both in Windows and in Mono that will allow me to check if another program already has the serial port open?

brennen
  • 31
  • 2
  • See: [What is the best practice for locking serial ports and other devices in Linux?](http://stackoverflow.com/questions/30316722/what-is-the-best-practice-for-locking-serial-ports-and-other-devices-in-linux) It seems `flock()` should be used going forward. – Craig McQueen Jan 14 '16 at 00:01

1 Answers1

2

In POSIX this behaviour is due to different file locking semantics: many processes can access the same file. If you'd run your code on Mono on Windows, you would see the same behaviour as Microsoft .Net is showing.

To work in the same way on Linux, open_serial() method in Mono native helper would have to acquire flock() on the opened file descriptor.

skolima
  • 31,963
  • 27
  • 115
  • 151
  • Do you know of any built-in C# functionality that would accomplish this? I'd rather not have to use non-C# code (or other people's code) if I can avoid it. – brennen May 12 '11 at 00:30
  • File a bug report with Mono - although SerialPort is so rarely used that you would probably have to implement the runtime fixes yourself. That's what I wound up doing, anyway. – skolima May 12 '11 at 07:45
  • Actually, using `flock()` won't help. On Linux lock is obtained by creation of special file (usually sth like /var/tmp/LCK..ttyS0). And this is what applications like minicom or gtkterm are using. Mac has its own way. Nevertheless it could be done - and will be if I find enough time and someone with Mac to test. – konrad.kruczynski Jul 03 '11 at 22:40
  • @konrad.kruczynski See: [What is the best practice for locking serial ports and other devices in Linux?](http://stackoverflow.com/questions/30316722/what-is-the-best-practice-for-locking-serial-ports-and-other-devices-in-linux) It seems `flock()` should be used going forward. – Craig McQueen Jan 14 '16 at 00:01
  • @CraigMcQueen Interesting, thank you! For me this is also the cleaner approach so I'm glad the other one is being deprecated now. Nevertheless I haven't done any work on this yet, but who knows - and I even possess an OS X now. – konrad.kruczynski Jan 17 '16 at 09:03