Reputation: 31
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?
Upvotes: 3
Views: 2082
Reputation: 32684
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.
Upvotes: 2