Serial port properties NOT binding.

There seems to be some problem (bug ) with the serial port properties binding.
I have : Baudrate, Databits, Handshake, Parity, Portname and StopBits set up as binding, the little binding symbol appears next to the name and also they are all listed under the Application Settings.

Of all the above only the HandShake (Flow control) is properly saved on exit and restored at startup, all other values for the serial port revert to the default state rather than the state on exit.

Other settings for other controls are all saved/restored ok except the above.

Can anyone one shed any light on this please Perhaps someone who uses serial ports can try it out and let me know.

Thank you.



Answer this question

Serial port properties NOT binding.

  • sburton

    Here's how I set up combo-boxes from Application settings and property enumerations:

    ' Initialize Serial Port Combo Box

    For Each strPort In My.Computer.Ports.SerialPortNames

    Me.cbSerialPorts.Items.Add(strPort)

    Next strPort

    iFound = Me.cbSerialPorts.FindString(My.Settings.SerialPort)

    If iFound >= 0 Then

    cbSerialPorts.SelectedIndex = iFound

    Else

    cbSerialPorts.Text = ""

    End If

    ' Initialize Baud Rate Combo Box

    iFound = Me.cbBaudRate.FindString(My.Settings.BaudRate)

    If iFound >= 0 Then

    Me.cbBaudRate.SelectedIndex = iFound

    Else

    Me.cbBaudRate.Text = ""

    End If

    ' Initialize Data Bits Combo Box

    iFound = Me.cbDataBits.FindString(My.Settings.DataBits)

    If iFound >= 0 Then

    Me.cbDataBits.SelectedIndex = iFound

    Else

    Me.cbDataBits.Text = ""

    End If

    ' Initialize Hand Shake Combo Box

    Items = System.Enum.GetNames(GetType(IO.Ports.Handshake))

    For Each Item In Items

    Me.cbHandShake.Items.Add(Item)

    Next

    iFound = Me.cbHandShake.FindString(My.Settings.HandShake)

    If iFound >= 0 Then

    Me.cbHandShake.SelectedIndex = iFound

    Else

    Me.cbHandShake.Text = ""

    End If

    ' Initialize Parity Combo Box

    Items = System.Enum.GetNames(GetType(IO.Ports.Parity))

    For Each Item In Items

    Me.cbParity.Items.Add(Item)

    Next

    iFound = Me.cbParity.FindString(My.Settings.Parity)

    If iFound >= 0 Then

    Me.cbParity.SelectedIndex = iFound

    Else

    Me.cbParity.Text = ""

    End If

    ' Initialize Stop Bits Combo Box

    Items = System.Enum.GetNames(GetType(IO.Ports.StopBits))

    For Each Item In Items

    Me.cbStopBits.Items.Add(Item)

    Next

    iFound = Me.cbStopBits.FindString(My.Settings.StopBits)

    If iFound >= 0 Then

    Me.cbStopBits.SelectedIndex = iFound

    Else

    Me.cbStopBits.Text = ""

    End If


        Ed Tenholder


  • slight

    Some background information on how the property bindings work:

    Under the hood, the windows forms designer is using "normal" databindings in order to bind the value of the property of an object (in this case a SerialPort) to a setting value. Neither the serial port instance nor the setting object know about the other guy - it is the databinding objects responsibility to make sure that if one of the two changes, the other is updated.

    In order to accomplish this, the databinding relies on the fact that the object fires the appropriate events whenever a object changes (either by implementing INotifyPropertyChanged or by exposing a <propertyname>Changed event).

    The settings object is built with this in mind, and implements INotifyPropertyChanged. The System.IO.Ports.SerialPort class, however, do not implement either the INotifyPropertyChanged, nor does it expose any events that tell the databinding when a property has changed, so the databinding doesn't detect this.

    If the windows forms designer detects that it can't set up a "fully functional" bi-directional databinding (which is the case here, as well as if the scope of the setting that you tried to bind to was Application and therefore read-only), it will generate code that initializes the control correctly when you first open the form, but it won't be able to detect any further changes. The binding effectively becomes more of a "read-when-you-first-startup" than a full-fledged bi-directional binding. I'll have a chat with the people who own the UI to set up the property binding and see if we can't make it so that this is apparent to the user when (s)he sets up the binding.

    I honestly have no idea why the Handshake property get saved out - it shouldn't unless you are changing it somewhere else in your code...

    If you are interested in how databindings *really* work, there is more information here: http://www.windowsforms.net/Samples/Go%20To%20Market/Data%20Binding/DataBinding%20FAQ.doc

    So, to round everything up, I wouldn't call this a bug, but I think we should improve the UI to indicate what is (and is not) happening when you bind a setting to a property that doesn't advertise changes in a way that the databindings can understand. I have updated the information in the bug that you opened on http://lab.msdn.microsoft.com/ProductFeedback/viewFeedback.aspx feedbackid=83e7d4fb-8108-4f3d-9c51-5419208de52d with this information.

    Best regards,
    Johan Stenberg



  • Kroeze

    Do you have a way for the user to be able to change say, port number or baud rate at run time If not you may add some buttons to do that, button 1 select com 1, button 2 select com2, etc. and then see if the program remember the last setting before close. I have the usual dropdown menu for the user to be able to select the required settings but they are not remembered by the system after closing and restarting.


  • Eric Knox MSFT

    Thanks again...but now I'm totally confusedTongue Tied. Did the person that wrote the article ACTUALLY used VB2005 I know it's presumptuos of me...but I did not have to write ANY code or know anything of what the article talks about, one simply goes to the property that needs to to be saved (for example the text font for the richeditbox) and clicks on the property binding for the item (the font in this case)and it's all done just like the help for VB2005 says.

    No notify of property change or anything it just magically happens. I'm getting the feeling that a lot of the advice I have been getting on different subjects is for another version of VB because VB2005 seems to make things so effortless on the part of the programmer (especcially a dummy like me). OK I'm out of line ...sorry.

    For example I asked how to generate a single instance of an application from a user's group and I got several lines of code, very helpfull, but obviously the writer didn't know that, with VB2005, all I had to do is to check the single instance box in the project's properties and it all happens.

    Oh well I'll try to digest the above info Big Smile It would be helpfull of course if someone would make a simple application with the serial port, create a form where the user can change the port's settings, make the changes binding and then post the resultant code for us beginners....Big Smile I'll post my application on the download page of my website shortly www.ampertronics.com.au  as it is mostly working now. Perhaps someone can help out with some improvements or if someone needs some of the code I have developed I'll be happy to share it.

  • Cathyjack

    Hi Ed

    I have uploaded my application on the download page of my website at www.ampertronics.com.au if anyone would like to download and try it out you are welcome. It is a imple program that is a cross between Notepad and Hyperterminal and sends thing out to the serial port. Any installation or other problems/suggestions are welcomed. I'll post the same on another thread so that helpful people can see the result of their input Big Smile

  • cgerull

    The reason that the settings aren't saved is that the serial port doesn't tell the settings class when the settings change (it doesn't fire any property changed events) To fix this, I'd manually read the values from the serialport and assign them to the settings before saving my settings...

    Best regards,
    Johan Stenberg

  • Bekas

    Hi Ed

    Thanks for the code snippet, I made a copy of it and will try to add it to my project. I only had the first bit where the com ports available are enumerated.
    So you have a way for the user to change settings, are the last settings before closing remembered by the aplication eg if Com 3 was used last time the aplication ran, it should restart with Com3 selected instead of reverting to say Com1 which may be the default setting for the serial port.

    This is where I'm coming unstuck. If you go to the properties for the serial ports at the top, under Aplication Settings, click on Property binding and select a few of the properties that you want the system to remebember (perhaps all of the ones I described above) and see if it works please or if you get the same results as I do.

    If you already have it all working as far as remembering the settings at close and restart I would apreciate some pointers on how you are doing it Big Smile

  • Mike Ut

    I don't use the property binnding for my serial port, all the settings are stored in the vb code.
    As a test I added a serialport control and used the binding on the baud rate parameter and the value stored remained. I also changed the databits value but did not bind it - the settings for this control were also restored on startup!! (startup in this case means saving the application, closing vb 2005 and reopening vb 2005, and the application)

    If you require further testing or I am not replicating the problem the correct way get in touch.







  • Marco.Casamento

    I do not bind the serialport properties to settings.  I bind the form objects (combo boxes) to application settings.

       Ed Tenholder

  • Kobe_London

    Hi Johan

    Thanks for the reply. So it this a bug (I have reported it as such)
    I don't do any settings/savings at all, I'm letting VB "properties bindings" do all the saving/restoring at closing and startup, at least this is my understanding of how properties binding should work so that I don't have to use a .ini file or the dreadfull registry Smile . Why should a change of settings event occur anyway, shouldn't VB2005 save the "bound  settings" regardless of whether they have changed or not And please keep in mind that 1 out of 6 settings does infact work for the serial port and all other bound settings for the project work perfectly. The "properties bindings" option is a great addition to VB in my opinion (only have used it for 3 weeks Tongue Tied but I'm very impressed)

  • Serial port properties NOT binding.