Announcement

Collapse
No announcement yet.

Selecting a Serial Port on Settings Page

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Selecting a Serial Port on Settings Page

    Has anyone worked out a good way to select a serial port on the settings page?

    The most user friendly selector would be the Dropdown Select List. The issue I run into though is that it saves the index and not COM4. The list of serial ports is not static as I may plug in a USB serial device that causes the list to change and on the next startup of the plugin to select the wrong port.

    I could display of list of available ports in a label and have the user type it in to a text input. That works reliably, but not friendly.

    I'm tempted to move the selection to the device configuration page where I have more control over the display. That would work for some plugins anyway.

    I played with some hacks. Using the dropdown and then saving the port name in the root device PED may be a good alternative, but the settings page could still show COM3 when the plugin is using COM4, so the selector has to be hidden until a Change button is clicked or something.

    Any suggestions?

    #2
    Not sure if I fully understand, but I had a similar issue with the Dropdown SelectView until I noticed there is an optional parameter "optionKeys".

    Public NotInheritable Class SelectListView
    Public Sub New(id As String, name As String, options As List(Of String), Optional style As ESelectListType = ESelectListType.DropDown, Optional selection As Integer = -1)
    Public Sub New(id As String, name As String, options As List(Of String), optionKeys As List(Of String), Optional style As ESelectListType = ESelectListType.DropDown, Optional selection As Integer = -1)

    I would do something like this:
    Code:
    Dim DDKeys As New List(Of String)
    Dim DDOptions As New List(Of String)
    Dim DDDefault As Integer = 0
    Dim YourDefaultValue As String = "COM4"
    For Each SP As String In My.Computer.Ports.SerialPortNames
      DDKeys.Add(SP.ToString)
      DDOptions.Add(SP.ToString)
      If SP = YourDefaultValue Then DDDefault = DDKeys.Count - 1
    Next
    JUIPage.AddView(New SelectListView("YourPageId", "Name", DDOptions, DDKeys, ESelectListType.DropDown, DDDefault))
    If that doesn't help, give us some code you are working with to understand your issue better.

    Thanks!
    stefxx

    Comment


      #3
      Thanks for responding. Your code is similar to mine except that I am talking specifically about using "settings" pages, so my code looks like:
      Dim sPorts As String() = SerialPort.GetPortNames()
      settingsPage1.WithDropDownSelectList("sp1-newport", "Serial Port", sPorts.ToList, sPorts.ToList)

      Using the parameter "option keys" is useful to be able to parse the actual chosen comm port name in the override function OnSettingChange(), however HS will still save an integer with the index "0" or "1" to the ini file. I'll give a step by step to create an issue with that.

      Connect one USB serial port device to the PC hosting my plugin. Let's say it's an Arduino and I configure it as COM3. There are no hardware serial ports in the PC.

      Now I'll plug in a USB to RS-232 device and configure it as COM4. I start my plugin and go to the Settings Page to select COM4 as the serial port for the plugin. Since the list of ports has COM3 in index 0 and COM4 in index 1, the myplugin.ini file will now have the setting "sp1-newport=1"

      Now I stop the plugin. I unplug the Arduino. I start the plugin again. The list now has only 1 port which is COM4 in index 0. HS will load the setting 1 and try to index it in the list. It will fail.

      Now I plug the Arduino back in and try to start the plugin again. The list is now COM4 in 0 and COM3 in 1 (they show in the order they are added to the PC with Windows 10). The plugin will start on COM3 and will now cause 2 plugins to fail since the Arduino is connected to the wrong plugin.

      As I say above, unless someone sees a way to use a dropdown list in a settings page for a list that is not static, it seems I'll need to bypass HS settings and track them in the plugin. Using the "option keys" would work if the next step is to save "COM4" in the OnSettingChange() function. I could create my own ini file and save it there, or put it in a PED. I'm just wondering if everyone agrees that the Settings Page Dropdown is not useful to save a serial port.

      Comment


        #4
        Yeah, not sure how to help with that. But those limitations made me stop using SettingsPages, and use only FeaturePages.
        stefxx

        Comment

        Working...
        X