//
// (c) Copyright 2006 Novell, Inc. (http://www.novell.com)
//
+// Slightly modified by Konrad M. Kruczynski (added baud rate value checking)
#if NET_2_0
fd = open_serial (portName);
if (fd == -1)
ThrowIOException ();
+
+ TryBaudRate (baudRate);
if (!set_attributes (fd, baudRate, parity, dataBits, stopBits, handshake))
- ThrowIOException (); // Probably Win32Exc for compatibility
+ ThrowIOException (); // Probably Win32Exc for compatibility
read_timeout = readTimeout;
write_timeout = writeTimeout;
throw new IOException (error_message);
}
+
+ [DllImport ("MonoPosixHelper")]
+ static extern bool is_baud_rate_legal (int baud_rate);
+
+ private void TryBaudRate (int baudRate)
+ {
+ if (!is_baud_rate_legal (baudRate))
+ {
+ // this kind of exception to be compatible with MSDN API
+ throw new ArgumentOutOfRangeException ("baudRate",
+ "Given baud rate is not supported on this platform.");
+ }
+ }
}
}
}
gboolean
-set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
+is_baud_rate_legal (int baud_rate)
{
- struct termios newtio;
-
- if (tcgetattr (fd, &newtio) == -1)
- return FALSE;
-
- newtio.c_cflag |= (CLOCAL | CREAD);
- newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN );
- newtio.c_oflag &= ~(OPOST);
- newtio.c_iflag = IGNBRK;
+ return setup_baud_rate (baud_rate) != -1;
+}
- /* setup baudrate */
+int
+setup_baud_rate (int baud_rate)
+{
switch (baud_rate)
{
/*Some values are not defined on OSX and *BSD */
case 50:
case 0:
default:
- baud_rate = B9600;
+ baud_rate = -1;
break;
}
+ return baud_rate;
+}
+
+gboolean
+set_attributes (int fd, int baud_rate, MonoParity parity, int dataBits, MonoStopBits stopBits, MonoHandshake handshake)
+{
+ struct termios newtio;
+
+ if (tcgetattr (fd, &newtio) == -1)
+ return FALSE;
+
+ newtio.c_cflag |= (CLOCAL | CREAD);
+ newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN );
+ newtio.c_oflag &= ~(OPOST);
+ newtio.c_iflag = IGNBRK;
+
+ /* setup baudrate */
+ baud_rate = setup_baud_rate (baud_rate);
/* char lenght */
newtio.c_cflag &= ~CSIZE;