Get the name of a serial device in a desktop computer in C #. Net

Source: Internet
Author: User

1. Situation:

When you do the project, open the serial port and make some library function calls to manipulate the target board. The serial port uses the USB to the serial port, the board plugs in pulls out, each time uses the different USB port, the debug time does not bother each time to change the COM port, does not want to add a choice in the UI

COM port Combox, so the following method is used.

2. Environment:

Win7 64, VS2010

3. Target:

Gets the device in the end using COM several.

4. Source Codery>

1         /// <summary>2         ///Get the target com num.3         /// </summary>4         /// <returns></returns>5          Public Static intGetcomnum ()6         {7             intComnum =-1;8             string[] Strarr = Getharewareinfo (hardwareenum.win32_pnpentity,"Name");9             foreach(stringSinchStrarr)Ten             { One Debug.WriteLine (s); A  -                 if(S.length >= at&& S.contains ("CH340")) -                 { the                     intStart = S.indexof ("(") +3; -                     intEnd = S.indexof (")"); -Comnum = Convert.ToInt32 (s.substring (start +1, End-start-1)); -                 } +             } -  +             returnComnum; A  at         } -  -         /// <summary> -         ///Get The System devices information with Windows API. -         /// </summary> -         /// <param name= "Hardtype" >Device type.</param> in         /// <param name= "Propkey" >The property of the device.</param> -         /// <returns></returns> to         Private Static string[] Getharewareinfo (Hardwareenum hardtype,stringPropkey) +         { -  thelist<string> STRs =Newlist<string>(); *             Try $             {Panax Notoginseng                 using(ManagementObjectSearcher searcher =NewManagementObjectSearcher ("SELECT * from"+hardtype)) -                 { the                     varHardinfos =Searcher. Get (); +                     foreach(varHardinfoinchHardinfos) A                     { the                         if(Hardinfo.properties[propkey]. Value! =NULL) +                         { -String str =Hardinfo.properties[propkey]. Value.tostring (); $ STRs. ADD (str); $                         } -  -                     } the                 } -                 returnSTRs. ToArray ();Wuyi             } the             Catch -             { Wu                 return NULL; -             } About             finally $             {  -STRs =NULL; -             } -}//End of Func getharewareinfo (). A  +     /// <summary> the     ///Enumerating Win32 APIs -     /// </summary> $      Public enumHardwareenum the     { the         //Hardware theWin32_Processor,//CPU Processor theWin32_physicalmemory,//Physical Memory Strips -Win32_Keyboard,//keyboard inWin32_pointingdevice,//point input device, including mouse.  theWin32_floppydrive,//Floppy Drive theWin32_DiskDrive,//Hard Drive AboutWin32_CDROMDrive,//Optical Drive theWin32_baseboard,//Motherboard theWin32_BIOS,//BIOS Chip theWin32_ParallelPort,//and the Port +Win32_serialport,//Serial Port -Win32_serialportconfiguration,//Serial Configuration theWin32_sounddevice,//multimedia settings, commonly referred to as sound cards. BayiWin32_systemslot,//motherboard Slots (ISA & PCI & AGP) theWin32_usbcontroller,//USB Controller theWin32_NetworkAdapter,//Network Adapter -Win32_NetworkAdapterConfiguration,//Network Adapter Settings -Win32_Printer,//Printer theWin32_PrinterConfiguration,//Printer Settings theWin32_PrintJob,//Printer Tasks theWin32_tcpipprinterport,//Printer Port theWin32_potsmodem,//MODEM -Win32_potsmodemtoserialport,//MODEM Port theWin32_DesktopMonitor,//Display theWin32_displayconfiguration,//video card theWin32_displaycontrollerconfiguration,//Graphics Settings94Win32_VideoController,//graphics details.  theWin32_videosettings,//display mode supported by the graphics card.  the  the         //Operating System98Win32_TimeZone,//Time Zone AboutWin32_SystemDriver,//Driver Program -Win32_DiskPartition,//Disk Partitioning101Win32_LogicalDisk,//Logical Disk102Win32_logicaldisktopartition,//The partition where the logical disk resides and its location. 103Win32_LogicalMemoryConfiguration,//Logical Memory Configuration104Win32_PageFile,//System page File information theWin32_PageFileSetting,//page File Settings106Win32_bootconfiguration,//System Boot Configuration107Win32_ComputerSystem,//Computer Information Brief108Win32_OperatingSystem,//Operating System Information109Win32_startupcommand,//System Auto-Start program theWin32_Service,//System-Installed services111Win32_Group,//System Management Group theWin32_groupuser,//System Group Account113Win32_UserAccount,//User Account theWin32_Process,//System Processes theWin32_thread,//System Threads theWin32_Share,//shared117Win32_NetworkClient,//Installed network Clients118Win32_NetworkProtocol,//Installed network Protocols119Win32_pnpentity,//All device -}

6. Results:

correctly get the corresponding COM slogan, reached the point of any of the mouth program can run the purpose.

Get the name of a serial device in a desktop computer in C #. Net

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.