Pipistrello: Identify virtual com port (e.g. COM4) by the device serial number


offroad

Recommended Posts

Hi,

I did a quick demo that Pipistrello boards can be uniquely identified when several are connected to the same host.
The same works probably with Papilio and others, but I didn't test it.
The serial number can be read e.g. with FTDI's FT_PROG utility.Alternatively, Windoze device manager, "View/devices by connection", "USB serial converter A", "Details", "Device instance path".

Adapted from stackexchange.

C#:

using System;
using System.Management;
using System.Linq;
using System.Text.RegularExpressions;

... your class here

    private static string[] matchRegexReturnString(Regex r,string s) {
        Match m = r.Match(s);
        int n = m.Groups.Count - 1; // captures start at index 1
        if(n > 0) {
            string[] result = new string[n];
            for(int ix = 0;ix < n;++ix) {
                result[ix] = m.Groups[ix + 1].Value;
            }
            return result;
        }
        else {
            return null;
        }
    }

    private static string getComPortName(string serialNr) {
        ManagementObjectSearcher searcher2 = new ManagementObjectSearcher("root\\CIMV2","SELECT * FROM Win32_PnPEntity");

        Regex re1 = new Regex(@"FTDIBUS..VID_0403.PID_6010.(\d+)\D");
        Regex re2 = new Regex(@"(COM\d+)");

        foreach(ManagementObject queryObj in searcher2.Get()) {
            string p = queryObj.Path.Path.ToString();
            // search for expected FTDI device
            string[] m = matchRegexReturnString(re1,p);
            if(m != null) {
                // search for supported serial number
                if(m[0] == serialNr) {
                    string uu = queryObj["Caption"].ToString();
                    m = matchRegexReturnString(re2,uu);
                    if(m != null) {
                        return m[0];
                    }
                }
            }
        }
        return null;
    }

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.