K8090 C# Connection code

Hi all!

I’m a beginner with C#. Actually this is my first C# project and i’m trying to program a little traffic light for my K8090 board. I already had a little help from a friend who is a programmer, but i don’t want to ask him for help over and over again. So i hope someone can help me here.

First of all i’m looking for a code that is automaticly detecting the right USB (COM) port, so i dont have to change the port in C# if i connect to another port. I’m using this code now:

private void Form1_Load(object sender, EventArgs e)
        {
            string[] portNamen = System.IO.Ports.SerialPort.GetPortNames();
            board.Port = "COM9";
            connection = board.Connect();

            if (connection)
            {
                this.Text = "Trafficlight connected to Port 9";
            }

            else
            {
                this.Text = "Trafficlight NOT connected to Port 9";
                System.Windows.Forms.MessageBox.Show("k8090 NOT connected to Port 9");
                pictureBox1.Visible = false;
                pictureBox2.Visible = false;
            }

            

            if (connection)
            {
                board.SwitchRelayOn(1);
                board.SwitchRelayOn(8);
                board.SwitchRelayOn(32);
                board.CommandReceived += OnCommandoRecieving;
                
                
            }
        }

So i hope someone can help me with a better code!

Thx!

The K8090 is an USB CDC device, not a COM port so the device probably won’t be listed with GetPortNames()

Please see this topic:
viewtopic.php?f=3&t=5255&p=20226&hilit=enumerate#p19180

You will need to translate it to C#

Thx for the info!