I try to make arduino a data collect adapter and have it connect to a PC (windows) via usb ports. But when I plugged it into another usb port of the PC or/and use another arduino board, usually the com number shown in the PC changed.
I plan to write my own PC program (python, matlab or labview) to collect the data from this arduino. Is there a way to fix this com number, so everytime when I plug it in, the PC program can correctly identify this device and connect to it.
what I do is a scan of COM ports opening each in turn sending a prompt command, e.g. ?, which the correct target replies with a known string, e.g. Weather_Station.
this C# code searches for a modem attached to a COM port, e.g. send AT command and waits for an OK response
public Form1()
{
InitializeComponent();
// scan for COM ports and add to menu item if found
for(int port = 0; port <= 10; port++)
{
serialPort1.PortName = "COM" + port.ToString();
try
{
serialPort1.Open(); // attempt to open serial port
serialPort1.BaudRate=115200;
textBox1.AppendText("Found " + serialPort1.PortName + "\n");
Console.WriteLine("serial port open OK {0}", serialPort1.PortName);
serialPort1.Write("AT\r\n");
Thread.Sleep(100);
String response = serialPort1.ReadExisting();
Console.WriteLine("response " + response);
if (response.Contains( "OK"))
{
Console.WriteLine("Fastrack XTEND modem found");
}
serialPort1.Close() ; //open OK, close it
ToolStripMenuItem item = new ToolStripMenuItem(serialPort1.PortName); // create a new menu item
item.Click += comPortsStripMenuItem_Click; // add event handler
comPortsStripMenuItem.DropDownItems.Add(item); // add to the menu of COM ports
}
catch ( Exception ex)
{ Console.WriteLine("serial port open fail {0} ", serialPort1.PortName, ex); }
}
Console.WriteLine("COM ports found {0} ", comPortsStripMenuItem.DropDownItems.Count);
textBox1.AppendText(String.Format("{0} COM ports found - select from COM ports menu\r\n\r\n", comPortsStripMenuItem.DropDownItems.Count));
// if no COM ports were found display message
if(comPortsStripMenuItem.DropDownItems.Count == 0 )
textBox1.AppendText("No COM ports found\r\n");
}
Plug the same board into the same usb port each time and it will get the same COM port number.
You can change them in Device Manager -> Ports (COM & LPT) -> [The device] -> Port Settings -> Advanced..., but that applies only to that particular USB to serial chipset (CP210x, CH340, etc) on that particular port. (If you plug an Arduino Nano into a port, then remove it and plug in another Nano, they will both have the same COM port. Windows is relatively dumb when it comes to identifying different generic devices - that's why when you plug a printer/keyboard/mouse/joystick/thumb drive into a different port it does the whole add new hardware crap again - it thinks it's a completely new device. A client's computer had 5 copies of the same printer driver because of this, and he never knew which one to print to.)
I don't know what happened. I thought someone helped me to post it in the right section, but... maybe this oliverjames07207 is another me from one of the parallel universes...