none
Detect and select COM port RRS feed

  • Question

  • Hi,

    My C# app controls two stepper with two different Arduinoes, each with its own COM port. I want the app to connect to the correct COM port automatically.

    1. C# must first find all available COM ports.

    2. Send a string "s" to all available COM ports and only use the two COM ports that respond with "Elevation" and "Azimuth".

    For example .... If C# sends a string "s" to COM3 and and COM3 responds with "Azimuth" then AzimuthPort.PortName = "COM3";. My problem is that I can find all available COM ports, but I can't send them a command because the same COM port cannot be accessed from multiple locations.

    private void detectCOMPort()
            {
                detectPort = new SerialPort();
                // Get a list of available COM ports
                string[] ports = SerialPort.GetPortNames();
                // Display each port name 
                foreach (string port in ports)
                {
                    detectPort.PortName = port;
                    detectPort.Write("s");
    
                    String compare = detectPort.ReadExisting();
    
                    if (compare == "Elevation")
                        myPort.PortName = port;
                    if (compare == "Azimuth")
                        myPort2.PortName = port;
                }
            }
    Any help would be appreciated.





    • Edited by okrus Friday, May 24, 2019 4:21 PM
    Friday, May 24, 2019 4:05 PM

Answers

  • You don't seem to be opening the port !

    detectPort = new SerialPort();
                
    // Get a list of available COM ports
    string[] ports = SerialPort.GetPortNames();
                
    // Display each port name 
    foreach (string port in ports)
    {
    	detectPort.PortName = port;
    	try
    	{
    		detectPort.Open();
    		detectPort.DiscardInBuffer();
    		detectPort.Write("s");
    		
    		//
    		// Allow time for Arduino to respond
    		//
    		Thread.Sleep( 200 );
    		
    		//
    		// Read response.
    		//
        String compare = detectPort.ReadExisting();
    
        if (compare == "Elevation")
            myPort.PortName = port;
        if (compare == "Azimuth")
            myPort2.PortName = port;             
            
    		detectPort.Close();       
    	}
    	catch
    	{
    		//
    		// Port already open in another app ?
    		//
    	}
    }

    Andy

    • Proposed as answer by Castorix31 Saturday, May 25, 2019 12:44 PM
    • Marked as answer by okrus Monday, May 27, 2019 11:34 PM
    Friday, May 24, 2019 6:13 PM

All replies

  • You don't seem to be opening the port !

    detectPort = new SerialPort();
                
    // Get a list of available COM ports
    string[] ports = SerialPort.GetPortNames();
                
    // Display each port name 
    foreach (string port in ports)
    {
    	detectPort.PortName = port;
    	try
    	{
    		detectPort.Open();
    		detectPort.DiscardInBuffer();
    		detectPort.Write("s");
    		
    		//
    		// Allow time for Arduino to respond
    		//
    		Thread.Sleep( 200 );
    		
    		//
    		// Read response.
    		//
        String compare = detectPort.ReadExisting();
    
        if (compare == "Elevation")
            myPort.PortName = port;
        if (compare == "Azimuth")
            myPort2.PortName = port;             
            
    		detectPort.Close();       
    	}
    	catch
    	{
    		//
    		// Port already open in another app ?
    		//
    	}
    }

    Andy

    • Proposed as answer by Castorix31 Saturday, May 25, 2019 12:44 PM
    • Marked as answer by okrus Monday, May 27, 2019 11:34 PM
    Friday, May 24, 2019 6:13 PM
  • Thank you Andy!
    Saturday, May 25, 2019 12:06 PM
  • Hi 

    It seems that your problem has been solved. If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 27, 2019 1:27 AM