Custom baud rates on SerialPort = ArgumentOutOfRangeException

intvsteveintvsteve USMember ✭✭

Working on an application that needs to run at 2000000 bps. Works perfectly in Windows. However, on Mac, this fails with an ArgumentOutOfRangeException.

This is a real stinker of a problem. It would seem that in the Mono sources, SerialPortStream.cs calls out to is_baud_rate_legal(baud_rate).

Looking at that source, and the discussion for Bug 8207 over at bugzilla, it would seem that this might be fixed. Alas, it is not.

Any tips on how to get this working? Is it necessary to p/invoke ioctl somehow? Has anyone here done this?

Thanks!

Posts

  • intvsteveintvsteve USMember ✭✭

    Found a workaround as follows:
    a) Open the port using some safe, dummy baud rate (e.g. 9600)
    b) Use reflection to dig out the file descriptor in the SerialPortStream (UGH!)
    c) p/invoke to ioctl to set the desired baud rate:

        const uint IOSSIOSPEED = 0x80000000 | ((sizeof(uint) & 0x1fff) << 16) | ((uint)'T' << 8) | 2;
    
        [DllImport("/System/Library/Frameworks/IOKit.framework/IOKit")]
        extern public static int ioctl(int fileNumber, uint request, ref int baudRate);
    
        public static int SetBaudRate(int fileNumber, int baudRate)
        {
            var customBaudRate = baudRate;
            var result = ioctl(fileNumber, IOSSIOSPEED, ref customBaudRate);
            return result;
        }
    

    Preliminary results are encouraging.

    Of course, I'll be really happy to find out if this has been addressed in Mono. :D

Sign In or Register to comment.