C # automatically search for serial numbers that match features

Source: Internet
Author: User

Business scenario: when developing a device that requires USB to serial port, the com name may change every connection with the computer. Solution: the easiest way to think about it is to write the serial port number in the configuration file (or let the user select the serial port number on the UI), so the actual operation is still complicated, the following is an article from codeproject.ArticleTo automatically search for specific serial devices based on certain characteristics of the serial device.

For example, if the path of the serial port contains "fdtibus" to determine whether it is the device I want to find, print it out.CodeAs follows:

 using system; using system. management; namespace consoleapplication2 {class program {static void main (string [] ARGs) {try {var searcher = new managementobjectsearcher ("Root \ WMI ", "select * From msserial_portname"); foreach (managementobject queryobj in searcher. get () {If (queryobj ["instancename"]. tostring (). contains ("ftdibus") {console. writeline (queryobj ["portname"] + "is a fdtibus device. ") ;}} console. read ();} catch (managementexception e) {system. diagnostics. debug. writeline ("An error occurred while querying for WMI data:" + E. message) ;}}}

In actual use, you can view the attributes you are concerned about in managementobject through debugging and make corresponding judgments.

Refer:Http://www.codeproject.com/Articles/32330/A-Useful-WMI-Tool-How-To-Find-USB-to-Serial-Adapto

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.