0

I've got Arduino Nano controlling a DC motor connected to my PC and controlled over C#. The only problem is that it works on my computer at the moment, and if I connect it to another PC it won't work unless it uses the same serial port. That's why I want the COM port to "set itself". Is it possible to do? If not, I wanted to make another Form just for entering the number of COM port, but I want to avoid that if possible. Thank you in advance. This is my code:

public partial class Form1 : Form
{
    String s = "0";
    string brojPorta = "COM5";
    int vrijednost = 0;
    System.IO.Ports.SerialPort serialPort1;
    public Form1()
    {
        InitializeComponent();
        System.ComponentModel.IContainer components =
    new System.ComponentModel.Container();
        serialPort1 = new System.IO.Ports.SerialPort(components);
        serialPort1.PortName = brojPorta;
        serialPort1.BaudRate = 9600;
        serialPort1.Open();
    }
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (serialPort1.IsOpen) serialPort1.Close();
    }

    private void Klizac1_Scroll(object sender, ScrollEventArgs e)
    {
        vrijednost = Klizac1.Value;
        s = (vrijednost * 10.24).ToString();
        serialPort1.Write(s + '\n');
        label1.Text = ((vrijednost-50)*2).ToString()+"%";
    }

    private void btn_Zaustavi_Click(object sender, EventArgs e)
    {
        Klizac1.Value = 50;
        label1.Text = "0";
        s = (Klizac1.Value * 10.24).ToString();
        serialPort1.Write(s + '\n');
    }
}
dsolimano
  • 8,870
  • 3
  • 48
  • 63
manac
  • 1
  • 1
  • The ability to talk to the Arduino through the COM port is only for debugging and development. It isn't intended to be portable. But you could 'search' the available COM ports and pick one based on what it looks like. If you really need to create a server for your Arduino, you're going to need to add a user-selection capability, because auto-binding just doesn't always work. Look for example, at the Mission Planner from ArduCopter project. It attempts to auto-bind, but it almost never works. – Jasmine Jun 16 '14 at 22:44
  • If you are writing the code for the Nana include a query command that returns sends back some identifier. When that is done open each port, send the inquiry and check the response. I have used this technique when programming other micro-controllers. The drawback is that COM ports that aren't attached will take some time to timeout. – dbasnett Jun 17 '14 at 12:19

2 Answers2

1

First you would have to enumerate all the ports. See this question: How to find available COM ports?

Then you would have to attempt to connect on each port with a timeout until you find it.

The more prudent scenario is to enumerate the available ports in a drop down list and have the user select the port it's connected to.

Community
  • 1
  • 1
Michael Rice
  • 1,173
  • 5
  • 13
0

There could be some pitfalls here but this example appears to work:

/*Use the WMI to search for the Arduino device on a serial port driver
and assign the serial port to the device*/

ManagementObjectSearcher SerialPortSearcher =
    new ManagementObjectSearcher(
    "root\\CIMV2",
    "SELECT * FROM Win32_SerialPort");

foreach (ManagementObject SerialPortObject in SerialPortSearcher.Get())
{
    if (SerialPortObject["Description"].ToString() == "Arduino Mega 2560")
    {
        SerialPort _serialPort =
            new SerialPort(SerialPortObject["DeviceID"].ToString());
        break;
    }
}
aklijn
  • 1