A function with coding in VB10 to set a com port for K8090

I’m currently creating software for controlling the relays on a K8090 board, but not necessarly going to run it from the laptop used for the software development and testing, how can I code it to be able to select a com port from a drop down list that the board has been connected to?

Your question is how you can get a list of all K8090’s connected to your computer? In Visual Basic .NET 2010? For Windows?

Imports System.Management ' Don't forget to add a reference!

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) _
        Handles MyBase.Load

        Dim scope As ManagementScope = New ManagementScope("root\CIMV2")
        scope.Options.EnablePrivileges = True

        Dim query As ObjectQuery = New ObjectQuery(
            "SELECT * FROM Win32_SerialPort WHERE PNPDeviceID LIKE '%VID_10CF&PID_8090%'")

        Dim searcher As ManagementObjectSearcher = New ManagementObjectSearcher(scope, query)
        ComboBox1.Items.Clear()
        For Each obj As ManagementObject In searcher.Get()
            ComboBox1.Items.Add(obj("DeviceID").ToString())
        Next

        If ComboBox1.Items.Count >= 1 Then
            ComboBox1.SelectedIndex = 0
        End If
    End Sub

End Class

Thanks for your reply, sorry to ask many questions but I’m new to VB, so I would have to add a combo box to my form?, and you say don’t forget to add a refrence, could you explain a little more for me?

Also my code goes so far as

K8090.Port = “COM7”
If K8090.Connect() Then
K8090.SwitchRelayOff(2)
K8090.SwitchRelayOff(4)
K8090.SwitchRelayOff(8)
System.Threading.Thread.Sleep(500)
K8090.SwitchRelayOn(1)
K8090.Disconnect()
End If

How can I get the k8090.Port = what is selected in the combo box?

Look forward to you reply

[quote=“ian.spurgeon”]Thanks for your reply, sorry to ask many questions but I’m new to VB, so I would have to add a combo box to my form?[/quote]Yes, a ComboBox called ComboBox1

[quote=“ian.spurgeon”]and you say don’t forget to add a refrence, could you explain a little more for me?[/quote]Project -> Options -> References -> Add… -> Browse -> (the K8090 dll for .NET)

[quote=“ian.spurgeon”]Also my code goes so far as

K8090.Port = “COM7”
If K8090.Connect() Then
K8090.SwitchRelayOff(2)
K8090.SwitchRelayOff(4)
K8090.SwitchRelayOff(8)
System.Threading.Thread.Sleep(500)
K8090.SwitchRelayOn(1)
K8090.Disconnect()
End If

How can I get the k8090.Port = what is selected in the combo box?[/quote]K8090.Port = ComboBox1.Text

Try to google or buy/download a good book about VB.NET, it will help you with these basic questions

Many thanks for your help

I have added the reference, added a combobox to my form, but it brings up about 4 errors saying somthing like ManagementObject is not defined, can you help on this?

Also add a reference to System.Management