Sometimes the com port number of the Velbus connection changes - especially when I pull out the usb plug and plug it in again.
Is this normal behaviour or is there an odd reason for this?
This is especially annoying when using the c# api where I have to specify a ‘hard-coded’ com port number.
Is there a workaround for this? How to find the right port nr using c#?
I connect my pc to Velbus using the usb module.
I’m using Vista Ultimate 32bit.
I found a way to scan for the velbus usb com port and automatically connect to this port:
I added this snippet to the SerialBus class:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSSerial_PortName");
foreach (ManagementObject queryObj in searcher.Get())
{
//this will give back the pid and vid, which is unique for each device.
//The Velbus gives back "VID_10CF&PID_0B1B", so check for this id:
sInstanceName = queryObj"InstanceName"].ToString();
Debug.WriteLine(">>>" + sInstanceName);
if (sInstanceName.IndexOf("VID_10CF&PID_0B1B") > -1)
{
portName = queryObj"PortName"].ToString();
Debug.WriteLine(">>>Velbus com port: " + portName);
}
}
Now you know the com port and could simply pass it to m_serialPort:
m_SerialPort = new SerialPort(portName, baudRate);