Click here to Skip to main content
14,669,071 members
Home » Discussions » C#
   

C#

 
GeneralRe: Integers to Float IEEE 754 Pin
Luc Pattyn1-Jul-20 9:29
sitebuilderLuc Pattyn1-Jul-20 9:29 
QuestionHow can I find out what "USB Serial Device" is called in all other languages? Pin
arnold_w30-Jun-20 11:14
Memberarnold_w30-Jun-20 11:14 
AnswerRe: How can I find out what "USB Serial Device" is called in all other languages? Pin
Dave Kreskowiak30-Jun-20 11:18
mveDave Kreskowiak30-Jun-20 11:18 
GeneralRe: How can I find out what "USB Serial Device" is called in all other languages? Pin
arnold_w30-Jun-20 11:36
Memberarnold_w30-Jun-20 11:36 
GeneralRe: How can I find out what "USB Serial Device" is called in all other languages? Pin
Dave Kreskowiak30-Jun-20 11:59
mveDave Kreskowiak30-Jun-20 11:59 
GeneralRe: How can I find out what "USB Serial Device" is called in all other languages? Pin
Luc Pattyn30-Jun-20 11:48
sitebuilderLuc Pattyn30-Jun-20 11:48 
GeneralRe: How can I find out what "USB Serial Device" is called in all other languages? Pin
Dave Kreskowiak30-Jun-20 11:59
mveDave Kreskowiak30-Jun-20 11:59 
AnswerRe: How can I find out what "USB Serial Device" is called in all other languages? Pin
Luc Pattyn30-Jun-20 12:35
sitebuilderLuc Pattyn30-Jun-20 12:35 
Hi,

1.
SerialPort does enumerate all serial ports as it should, there is no problem, except it does not know about the underlying hardware, so for instance you can't differentiate motherboard vs USB ports.

2.
I trust USB is called USB in all languages that use latin character sets, so you could use String.Contains on the friendlyname.

3.
I have my own class enumerating all serial ports, its underlying technology is somewhat similar to the class you referred; here it is, use at your leisure. It returns a list of struct ComInfo which holds the friendlyname as well as the location (for USB serial ports this is a concatenation of port numbers, pointing to the USB port actually used).
BTW: you will have to remove all lines that hold an "env", these are used for my debugging only.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace LP_Core {
	public class LP_SerialPortInfo {
		private static List<ComInfo> serialPorts=new List<ComInfo>();

		private const UInt32 DIGCF_PRESENT=0x00000002;
		private const UInt32 DIGCF_DEVICEINTERFACE=0x00000010;
		private const UInt32 DICS_FLAG_GLOBAL=0x00000001;
		private const UInt32 DIREG_DEV=0x00000001;
		private const UInt32 KEY_QUERY_VALUE=0x0001;
		private const string GUID_DEVINTERFACE_COMPORT="86E0D1E0-8089-11D0-9CE4-08003E301F73";
		[StructLayout(LayoutKind.Sequential)]
		private struct SP_DEVINFO_DATA {
			public Int32 cbSize;
			public Guid ClassGuid;
			public Int32 DevInst;
			public UIntPtr Reserved;
		};

		public enum ComPortCharacteristic {
			Description=0x00000000,
			FriendlyName=0x0000000C,
			HardwareID=0x00000001,
			Location=0x0000000D
		}

		[DllImport("setupapi.dll")]
		private static extern Int32 SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);

		[DllImport("setupapi.dll")]
		private static extern bool SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, Int32 MemberIndex, ref  SP_DEVINFO_DATA DeviceInterfaceData);

		[DllImport("setupapi.dll", CharSet=CharSet.Auto, SetLastError=true)]
		private static extern bool SetupDiGetDeviceRegistryProperty(IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData,
			uint property, out UInt32 propertyRegDataType, StringBuilder propertyBuffer, uint propertyBufferSize, out UInt32 requiredSize);

		[DllImport("setupapi.dll", SetLastError=true)]
		private static extern IntPtr SetupDiGetClassDevs(ref Guid gClass, UInt32 iEnumerator, IntPtr hParent, UInt32 nFlags);

		[DllImport("Setupapi", CharSet=CharSet.Auto, SetLastError=true)]
		private static extern IntPtr SetupDiOpenDevRegKey(IntPtr hDeviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData, uint scope,
			uint hwProfile, uint parameterRegistryValueKind, uint samDesired);

		[DllImport("advapi32.dll", CharSet=CharSet.Unicode, EntryPoint="RegQueryValueExW", SetLastError=true)]
		private static extern int RegQueryValueEx(IntPtr hKey, string lpValueName, int lpReserved, out uint lpType,
			StringBuilder lpData, ref uint lpcbData);

		[DllImport("advapi32.dll", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
		private static extern int RegCloseKey(IntPtr hKey);

		[DllImport("kernel32.dll")]
		private static extern Int32 GetLastError();

		public struct ComInfo {
			public string Name;
			public string Description;
			public string FriendlyName;
			public string Location;
			public string HardwareID;
		}

		public static List<ComInfo> GetComPorts() {
			serialPorts.Clear();
			Guid guidComPorts=new Guid(GUID_DEVINTERFACE_COMPORT);
			IntPtr hDeviceInfoSet=SetupDiGetClassDevs(
				ref guidComPorts, 0, IntPtr.Zero, DIGCF_PRESENT|DIGCF_DEVICEINTERFACE);
			if (hDeviceInfoSet!=IntPtr.Zero) {
				try {
					List<ComInfo> devices=new List<ComInfo>();
					Int32 iMemberIndex=0;
					while (true) {
						SP_DEVINFO_DATA deviceInfoData=new SP_DEVINFO_DATA();
						deviceInfoData.cbSize=Marshal.SizeOf(typeof(SP_DEVINFO_DATA));
						bool success=SetupDiEnumDeviceInfo(hDeviceInfoSet, iMemberIndex, ref deviceInfoData);
						if (!success) {
							// No more devices in the device information set
							break;
						}

						ComInfo comInfo=new ComInfo();
						comInfo.Name=getName(hDeviceInfoSet, deviceInfoData);
						comInfo.Description=getString((UInt32)ComPortCharacteristic.Description, hDeviceInfoSet, deviceInfoData);
						comInfo.FriendlyName=getString((UInt32)ComPortCharacteristic.FriendlyName, hDeviceInfoSet, deviceInfoData);
						comInfo.HardwareID=getString((UInt32)ComPortCharacteristic.HardwareID, hDeviceInfoSet, deviceInfoData);
						comInfo.Location=getString((UInt32)ComPortCharacteristic.Location, hDeviceInfoSet, deviceInfoData);
						env.log("found: "+comInfo.Name+" = "+comInfo.Description+";FN="+comInfo.FriendlyName+
							"; HID="+comInfo.HardwareID+"; LOC="+comInfo.Location);
						serialPorts.Add(comInfo);
						iMemberIndex++;
					}
				} finally {
					SetupDiDestroyDeviceInfoList(hDeviceInfoSet);
				}
			}
			return serialPorts;
		}

		private static string getName(IntPtr pDevInfoSet, SP_DEVINFO_DATA deviceInfoData) {
			string name = "???";
			IntPtr hDeviceRegistryKey = SetupDiOpenDevRegKey(pDevInfoSet, ref deviceInfoData,
				DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE);
			if (hDeviceRegistryKey!=IntPtr.Zero) {
				StringBuilder buf = new StringBuilder(256);
				try {
					uint lpRegKeyType;
					uint length = (uint)buf.Capacity;
					int result = RegQueryValueEx(hDeviceRegistryKey, "PortName", 0, out lpRegKeyType, buf, ref length);
					if (result!=0) {
						throw new Exception("Can not read registry value PortName for device "+deviceInfoData.ClassGuid);
					}
				} finally {
					RegCloseKey(hDeviceRegistryKey);
				}
				name=buf.ToString();
			}
			return name;
		}

		private static string getString(UInt32 SPDRP, IntPtr hDeviceInfoSet, SP_DEVINFO_DATA deviceInfoData) {
			StringBuilder buf = new StringBuilder(256);
			uint propRegDataType;
			uint length = (uint)buf.Capacity;
			bool success = SetupDiGetDeviceRegistryProperty(hDeviceInfoSet, ref deviceInfoData, SPDRP,
				out propRegDataType, buf, length, out length);
			if (!success) {
				throw new Exception("Can not read registry value PortName for device "+deviceInfoData.ClassGuid);
			}
			return buf.ToString();
		}
	}
}


Smile | :)
Luc Pattyn [My Articles] Nil Volentibus Arduum

GeneralRe: How can I find out what "USB Serial Device" is called in all other languages? Pin
arnold_w30-Jun-20 19:52
Memberarnold_w30-Jun-20 19:52 
GeneralRe: How can I find out what "USB Serial Device" is called in all other languages? Pin
Member 79891221-Jul-20 0:09
MemberMember 79891221-Jul-20 0:09 
GeneralRe: How can I find out what "USB Serial Device" is called in all other languages? Pin
arnold_w1-Jul-20 0:15
Memberarnold_w1-Jul-20 0:15 
GeneralRe: How can I find out what "USB Serial Device" is called in all other languages? Pin
Luc Pattyn1-Jul-20 1:08
sitebuilderLuc Pattyn1-Jul-20 1:08 
GeneralRe: How can I find out what "USB Serial Device" is called in all other languages? Pin
arnold_w1-Jul-20 1:58
Memberarnold_w1-Jul-20 1:58 
GeneralRe: How can I find out what "USB Serial Device" is called in all other languages? Pin
Luc Pattyn1-Jul-20 2:08
sitebuilderLuc Pattyn1-Jul-20 2:08 
GeneralRe: How can I find out what "USB Serial Device" is called in all other languages? Pin
arnold_w1-Jul-20 2:12
Memberarnold_w1-Jul-20 2:12 
QuestionVS2017 publish project to .exe without manifest files Pin
Member 1487747430-Jun-20 10:35
MemberMember 1487747430-Jun-20 10:35 
AnswerRe: VS2017 publish project to .exe without manifest files Pin
Dave Kreskowiak30-Jun-20 11:15
mveDave Kreskowiak30-Jun-20 11:15 
QuestionOptimize Linq To SQL Query Pin
Kevin Marois30-Jun-20 8:21
professionalKevin Marois30-Jun-20 8:21 
QuestionC# Pin
Member 1258896327-Jun-20 5:30
MemberMember 1258896327-Jun-20 5:30 
AnswerRe: C# Generate a password dictionary to log into API Pin
OriginalGriff27-Jun-20 5:49
mveOriginalGriff27-Jun-20 5:49 
GeneralRe: C# Generate a password dictionary to log into API Pin
Member 1258896327-Jun-20 5:59
MemberMember 1258896327-Jun-20 5:59 
GeneralRe: C# Generate a password dictionary to log into API Pin
OriginalGriff27-Jun-20 6:00
mveOriginalGriff27-Jun-20 6:00 
GeneralRe: C# Generate a password dictionary to log into API Pin
Dave Kreskowiak27-Jun-20 7:10
mveDave Kreskowiak27-Jun-20 7:10 
QuestionBest way to transform list and get top result from custom sort order? Pin
linalinea26-Jun-20 9:44
Memberlinalinea26-Jun-20 9:44 
AnswerRe: Best way to transform list and get top result from custom sort order? Pin
OriginalGriff26-Jun-20 11:28
mveOriginalGriff26-Jun-20 11:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.