I am trying to write a program that uses an arduino mega and a FTDI-based USB to RS485 adapter.
I want to make the program user-friendly, thus I don\'t wont the user
This might work for you. I used this to dynamically read port number of Arduino on a system. Here
description.Contains("uino")
is to look for both Arduino and Genuino keyword for both varients of board.
public string detectArduinoPort()
{
ManagementScope mScope = new ManagementScope();
SelectQuery query = new SelectQuery("SELECT * FROM Win32_SerialPort");
ManagementObjectSearcher objectList = new ManagementObjectSearcher(mScope, query);
try
{
foreach (ManagementObject obj in objectList.Get())
{
string description = obj["Description"].ToString();
string deviceId = obj["DeviceID"].ToString();
if (description.Contains("uino"))
{
return deviceId;
}
}
}
catch (Exception)
{
}
return "";
}
Well, after studying several forums, I discovered that the com port associated to the USB/RS485 adapter is listed if I run the query SELECT * FROM Win32_PnPEntity
.
But I really don't understand why the serial port of the arduino is shown by a query and the other port with the other query.... I mean, those are both uSB<->serial adapters!
as @Marcello Romani pointed out FTDI don't fall into the "serial port" category. Even though I am late I came across this problem recently and I fixed it using another query. Instead of searching in WIN32_SerialPort, you can query the Win32_PnPEntity class.One downside of using this class is that is slow to query.
using (var searcher = new ManagementObjectSearcher
("SELECT * FROM Win32_PnPEntity"))
{
string[] portnames = SerialPort.GetPortNames();
var ports = searcher.Get().Cast<ManagementBaseObject>().ToList();
foreach (ManagementBaseObject queryObj in ports)
{
}
}
Basically, this is due to the HID (Human Interface Device)
I think that this is due to the HID provided by the arduino board. Arduino hardware (and thus, the microcontroller) communicates directely with the PC using HID (and so, no need for additional device). And HID can behave as a mouse, keyboard, or simply a serial device (it "simply" declare itself to the PC as a Serial Comport). So somehow, there is some "intelligent" way when communicating with the PC.
On the other hand, USBtoSerial converters and in some Arduino boards, like the Arduino Nano which embed an FTDI (or CH340), these are "obliged" to behave as a normal comport cause there is no intelligence behind (the the one behind the HID).
I don't have a USB<->RS485 adapter, but I suspect they don't fall into the "serial port" category (which seems reserved for RS232 interfaces), which could be the reason why they don't appear among the Win32_SerialPort query results.
To detect the connection of and Arduino board I look into this registry key:
HKLM\HARDWARE\DEVICEMAP\SERIALCOMM
for entries like
\Device\VCP0
(VCP is the prefix to look for).
Maybe you can look into this registry key too, or watch its parent key, DEVICEMAP, and see what happens when you connect the FTDI RS485 adapter. Detecting a change in one of these registry keys contents should be straightforward at that point.
HTH