How to Enumerating the Serialports automatically which are exist in the system.
hi,
i want to enumerating the Serialports automaticall to a combobox
for example if in my system have com1,com2,com6, it automatically read thes comports and show in the box.
is there any namespace provided by .net for these type of application if so then how i want it in c#.net.
regards RPM!
thanks in Advance
[349 byte] By [
RpM] at [2007-12-23]
i write a sol for the above problem if any body required he may go through the following code bellow it a classi made to read all Hardware configuration of the system. then from this u get u suitable port. every body can use this class
/*call this method on button click*/
private
void load(){
StringBuilder devices=
new StringBuilder("");UInt32 Index=0;
int result=0;cmbDevices.Items.Clear();
while(true){
result=DevInfo.DeviceInfo.EnumerateDevices(Index,"Ports", devices);
Index++;
//increment index for next deviceif(result == -2){
cmbDevices.Items.Add("Incorrect name of Class");
break;}
if(result == -1)break; //no next deviceif(result == 0){
string a = devices.ToString();int x = a.IndexOf("(");b = a.Remove(0,x);
if(b.StartsWith("(COM")){
cmbDevices.Items.Add(b);
}
}
cmbDevices.Refresh();
//refresh listbox}
}
/**Class To read Hardware exist**/
using
System;using
System.Runtime.InteropServices;using
System.Text;namespace
DevClasses{
/// <summary>/// Summary description for Class./// </summary>class ClassInfo{
public const int MAX_SIZE_DEVICE_DESCRIPTION = 1000;public const int CR_SUCCESS = (0x00000000);public const int CR_NO_SUCH_VALUE = (0x00000025);public const int CR_INVALID_DATA = (0x0000001F);private const int DIGCF_PRESENT = (0x00000002);private const int DIOCR_INSTALLER = (0x00000001);// MaximumAllowed access type to Reg.
private const int MAXIMUM_ALLOWED = (0x02000000);[StructLayout(LayoutKind.Sequential)]
private class SP_DEVINFO_DATA{
public int cbSize;public Guid ClassGuid;public int DevInst; // DEVINST handlepublic ulong Reserved;};
[DllImport("cfgmgr32.dll")]
private static extern UInt32CM_Enumerate_Classes(UInt32 ClassIndex,
ref Guid ClassGuid, UInt32 Params);[DllImport("setupapi.dll")]
//private static extern BooleanSetupDiClassNameFromGuidA(
ref Guid ClassGuid,StringBuilder ClassName, //char * ?UInt32 ClassNameSize,
ref UInt32 RequiredSize);[DllImport("setupapi.dll")]
private static extern IntPtrSetupDiGetClassDevsA(
ref Guid ClassGuid, UInt32 Enumerator,IntPtr hwndParent, UInt32 Flags);
[DllImport("setupapi.dll")]
private static extern BooleanSetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);
[DllImport("setupapi.dll")]
private static extern IntPtrSetupDiOpenClassRegKeyExA(
ref Guid ClassGuid, UInt32 samDesired, int Flags, IntPtr MachineName,UInt32 Reserved);
[DllImport("setupapi.dll")]
private static extern BooleanSetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, UInt32 MemberIndex,
SP_DEVINFO_DATA DeviceInfoData);
[DllImport("advapi32.dll")]
private static extern UInt32RegQueryValueA(IntPtr KeyClass,UInt32 SubKey,StringBuilder ClassDescription,
ref UInt32 sizeB);public static int EnumerateClasses(UInt32 ClassIndex, StringBuilder ClassName, StringBuilder ClassDescription, ref bool DevicePresent){
Guid ClassGuid=Guid.Empty;
IntPtr NewDeviceInfoSet;
UInt32 result;
SP_DEVINFO_DATA DeviceInfoData=
new SP_DEVINFO_DATA();bool resNam=false;UInt32 RequiredSize=0;
//enumerate device class with index - ClassIndex
//result - ClassGuid
result = CM_Enumerate_Classes(ClassIndex,
ref ClassGuid,0);DevicePresent=
false;if(result != CR_SUCCESS) //if Error{
return (int)result;}
resNam=SetupDiClassNameFromGuidA(
ref ClassGuid,ClassName,RequiredSize,ref RequiredSize);if(RequiredSize > 0){
ClassName.Capacity=(
int)RequiredSize; //real buffer size we need//get name of device class from class Guid:resNam=SetupDiClassNameFromGuidA(
ref ClassGuid,ClassName,RequiredSize,ref RequiredSize);}
//get device info set for our device classNewDeviceInfoSet=SetupDiGetClassDevsA(
ref ClassGuid,0,
IntPtr.Zero,
DIGCF_PRESENT);
if(NewDeviceInfoSet.ToInt32() == -1){
//Device infoset unavailableDevicePresent=
false;return 0;}
UInt32 numD=0;
//is devices exist for classDeviceInfoData.cbSize=28;
DeviceInfoData.DevInst=0;
DeviceInfoData.ClassGuid=System.Guid.Empty;
DeviceInfoData.Reserved=0;
Boolean res1=SetupDiEnumDeviceInfo(
NewDeviceInfoSet,
numD,
DeviceInfoData);
if(!res1){
//no devices for this classDevicePresent=
false;return 0;}
//Destroy device infosetSetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
//open device class registry keyIntPtr KeyClass=SetupDiOpenClassRegKeyExA(
ref ClassGuid, MAXIMUM_ALLOWED, DIOCR_INSTALLER,IntPtr.Zero,0);if(KeyClass.ToInt32() == -1){
//device class registry key unavailableDevicePresent=
false;return 0;}
//get device descriptionUInt32 sizeB=MAX_SIZE_DEVICE_DESCRIPTION;
ClassDescription.Capacity=MAX_SIZE_DEVICE_DESCRIPTION;
UInt32 res=RegQueryValueA(KeyClass,0,ClassDescription,
ref sizeB);if(res != 0)ClassDescription=new StringBuilder(""); //No device descriptionDevicePresent=
true;return 0;}
}
}
RpM at 2007-8-30 >
