Changing port parameters on the fly

  • H.B.

    H.B. - 2006-01-05

    Currently it's not possible to change eg. your baud rate on the fly (read: if the port is open)without closing and reopening the port. I'm not sure wether this is a feature or a bug, but I found a possible solution I want to discuss with you.

    If you search for the BaudRate property of the SerialPort class (SerialPort.cs), you'll find the following definition:

    public int    BaudRate
    if( IsOpen ) _baudRate = _streamCtrl.BaudRate;
                    return _baudRate;
    if( Array.BinarySearch(Consts.ValidBaudRates, value) < 0 )
        throw new ArgumentException( "Invalid Baud Rate", "value");

    if( IsOpen ) _streamCtrl.BaudRate = value;
        _baudRate = value;
    } private int _baudRate;

    So if your port is open the set method of _streamCtrl gets called, wich is of type ISerialStreamCtrl.
    In the class WinStream, you'll find the region "ISerialStreamCtrl Members", which gives us the implementation of the ISerialStreamCtrl properties. Among others, here you'll find the concerning implementation of the BaudRate property which is used at the end.

    The set method is defined as follows:

    set { _port.Settings.BaudRate = (OpenNETCF.IO.Serial.BaudRates) value; }

    _port.Settings is of type BasicPortSettings, which is a property of the basic Port class, defined in ..\Win32CE\Port.cs.

    The "problem" here concerning our on the fly change is that with _port.Settings.BaudRate the *get* method of the Settings property instead of the *set* method gets called.
    Therefore, the UpdateSettings() method for Settings never gets called (see *set* method of <BasicPortSettings Settings> in file Port.cs).

    So, if we replace the set method of BaudRate in WinStream.cs with:

    OpenNETCF.IO.Serial.BasicPortSettings tempPortSettings = _port.DetailedSettings.BasicSettings;
    tempPortSettings.BaudRate = (OpenNETCF.IO.Serial.BaudRates) value;
    _port.Settings = tempPortSettings;

    , UpdateSettings() gets called and the port is reconfigured to the new parameter.

    This applies to other settings you want to change on the fly accordingly.
    You may have problems following me here, the best you can do is to step through with your debugger starting with a call to the set method of SerialPort.BaudRate until you get to the *get* method of Port.Settings and hopefully you'll see what I mean. :)

    I tried some scenarios with the change and it worked flawlessly, would be great to read your opinion and suggestions, maybe I missed something and there's a better way to achieve on the fly changes.

    • Mr ChriZ

      Mr ChriZ - 2006-01-05

      That looks great Gonzo!
      I don't know if theres anyway we can get these
      changes updated given the fact that the authors dissapeared, but it would be nice to get them in there!

      I'm seriously busy at the moment so don't have time to look in to it.  This is already a very useful library however so it would be really good if they could be implemented.
      Any how gotta go
      I've got 5 hours travelling in front of me :-S


Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks