-1

Trying to code up a little serial port communications/control panel in C# with Visual Studio 2022. I'm making it up as a WindowsForm app using .Net Framework 4.8. When launching the code all the other aspects work fine (as far as I can test them without being able to choose and connect to a paired serial port). In the window it creates I'm able to navigate without a problem but the combo box that should have the serial ports listed in it instead remains blank. Debugging and monitoring the value of "ports" also has it responding with either "error CS0103: The name 'ports' does not exist in the current context" or just "null". Another similar program uses the same logic to obtain the ports created with the virtual port emulator software I am using and this works without a worry.

Early Relevant Code Blocks

public partial class Form1 : Form
{
    string dataOUT;
    string sendWith;
    string dataIN;
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        string[] ports = SerialPort.GetPortNames();
        cBoxCOMPORT.Items.AddRange(ports);

        btnOpen.Enabled = true;
        btnClose.Enabled = false;

        chBxDTR.Checked = false;
        serialPort1.DtrEnable = false;
        chBxRTS.Checked = false;
        serialPort1.RtsEnable = false;
        btnSendOut.Enabled = true;
        sendWith = "Both";

        toolStripComboBox1.Text = "Add to Old Data";
        toolStripComboBox2.Text = "Both";
        toolStripComboBox3.Text = "BOTTOM";
    }
~additional non-relevant functions
}

I'm sure the ports are available as the previous code solution can still find them so I don't think it's a problem with drivers or my virtual comm ports and I've check them on HKEY_LOCAL_MACHINE and they are present. The old solution was made around a week ago so it might be using .NET Framework 4.7.2 instead of 4.8. Regardless I have just copied and adjusted snippets of relevant code across and remade the design layout. There are no compilation errors or warnings either and I am for sure including the "using System.IO.Ports;" line.

I was following a guide provided on YouTube (https://www.youtube.com/watch?v=I6uhMIFTF24) and it worked. I did however have to remake it on a fresh solution and for whatever reason now the same lines of code don't obtain any of the ports made available.

Any help or ideas would be appreciated. No clue where to look or what to fiddle with to get it to find them in this solution.

Tim
  • 1
  • 2
  • Check the registry values at `HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM` – shingo Feb 01 '22 at 07:13
  • 1
    If you debug your code, what is in `ports`? – Klaus Gütter Feb 01 '22 at 07:39
  • Tried looking at values in debugger. Says it claims; "error CS0103: The name 'ports' does not exist in the current context" Also checked another variable defined inside this function and it doesn't seem to be getting set either. In the old version both encounter that error code for ports but the other string was fine. Thinking it might be the function not being called now? Doesn't seem to trigger a breakpoint in the debugger at the moment. Will look into that one as well. Also opened HKEY and both virtual serial ports are there, type REG_SZ named \Device\VSerial9_0 and 9_1 respectively. – Tim Feb 02 '22 at 03:54

1 Answers1

0

I think I've found the issue. The Form1 was not triggering the Form1_Load event when launching the program and creating the window Form1 is the main window in the [Design] tab and under its properties the 'Load' box option had no event tied to it. Changed that to be 'Form1_Load' and this now gets that block of code working so it's finding/displaying the COM Ports as well as all the other related set up. Not sure why this wasn't happening by default or if I missed a step creating it. Will add anything else that develops if a similar issue pops up but hopefully that's the end of that.

Tim
  • 1
  • 2