# Any way to get the serial port name from USB info?

**URL:** <https://forum.xojo.com/t/any-way-to-get-the-serial-port-name-from-usb-info/77834>\
**Category:** Extras\
**Created:** [October 26, 2023, 10:08am UTC](https://forum.xojo.com/t/any-way-to-get-the-serial-port-name-from-usb-info/77834 "2023-10-26T10:08:56Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ulrich\_Bogun](https://forum.xojo.com/user_avatar/forum.xojo.com/ulrich_bogun/32/1864_2.png) [@Ulrich\_Bogun](https://forum.xojo.com/u/Ulrich_Bogun)\
**Post date:** [October 26, 2023, 10:08am UTC](https://forum.xojo.com/t/any-way-to-get-the-serial-port-name-from-usb-info/77834/1 "2023-10-26T10:08:56Z")

</div>

I tried several methods to watch USB ports for changes – i.e. MacUSBNotificationMBS, libUSB running as a port watcher. Both make me get VendorID and ProductID which is necessary for me to determine the attached device‘s type.  
Both do show me a product name, which in this case is “USB Serial”.  
The Mac port name, anyway, can be something like “usbserial-113210” or similar.  
I tried to figure out if there is any resemblance between usb properties (address, bus etc.) and serial name but could not find any.

Therefore: Is it possible to translate usb info into Mac pro name?  
Or is there any way to read pID and vID from a serial port?  
Maybe with MBS, @Christian_Schmitz ?

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [October 26, 2023, 10:55am UTC](https://forum.xojo.com/t/any-way-to-get-the-serial-port-name-from-usb-info/77834/2 "2023-10-26T10:55:26Z")

</div>

WMI classes may help.

See blog post where we used it for USB Sticks  
[https://www.mbsplugins.de/archive/2012-02-24/Detecting\_drive\_letter\_for\_an\_/monkeybreadsoftware\_blog\_xojo](https://www.mbsplugins.de/archive/2012-02-24/Detecting_drive_letter_for_an_/monkeybreadsoftware_blog_xojo)

---

<div class="post-metadata">

**Author:** ![Ulrich\_Bogun](https://forum.xojo.com/user_avatar/forum.xojo.com/ulrich_bogun/32/1864_2.png) [@Ulrich\_Bogun](https://forum.xojo.com/u/Ulrich_Bogun)\
**Post date:** [October 26, 2023, 11:57am UTC](https://forum.xojo.com/t/any-way-to-get-the-serial-port-name-from-usb-info/77834/3 "2023-10-26T11:57:32Z")

</div>

Thank you, Christian. Any idea to have a similar feature for macOS?  
I tried FTDI/D2XX access too but sadly the mentioned devices, although some of them using an FTDI chip internally, are to listed here.

---

<div class="post-metadata">

**Author:** ![Jean-Yves\_Pochez](https://forum.xojo.com/user_avatar/forum.xojo.com/jean-yves_pochez/32/21769_2.png) [@Jean-Yves\_Pochez](https://forum.xojo.com/u/Jean-Yves_Pochez)\
**Post date:** [October 26, 2023, 12:39pm UTC](https://forum.xojo.com/t/any-way-to-get-the-serial-port-name-from-usb-info/77834/4 "2023-10-26T12:39:38Z")

</div>

> To do this, open a Terminal window, type `ls /dev/tty.usb*`, and press enter. You should see two entries of the form `tty.usbmodem<number>` (e.g. `/dev/tty.usbmodem00022331`). These entries represent virtual serial ports created by the programmer.

from

> <https://stackoverflow.com/questions/14074413/serial-port-names-on-mac-os-x>

also possible  
`ls -al /dev`  
and check for port names beginning with `cu.`

---

<div class="post-metadata">

**Author:** ![Ulrich\_Bogun](https://forum.xojo.com/user_avatar/forum.xojo.com/ulrich_bogun/32/1864_2.png) [@Ulrich\_Bogun](https://forum.xojo.com/u/Ulrich_Bogun)\
**Post date:** [October 27, 2023, 10:50am UTC](https://forum.xojo.com/t/any-way-to-get-the-serial-port-name-from-usb-info/77834/5 "2023-10-27T10:50:10Z")

</div>

Thank you, and a big step forward for my issue!  
Any ideas if there is a console app to ask pID and vID from a virtual serial port too?

---

<div class="post-metadata">

**Author:** ![Jean-Yves\_Pochez](https://forum.xojo.com/user_avatar/forum.xojo.com/jean-yves_pochez/32/21769_2.png) [@Jean-Yves\_Pochez](https://forum.xojo.com/u/Jean-Yves_Pochez)\
**Post date:** [October 27, 2023, 12:36pm UTC](https://forum.xojo.com/t/any-way-to-get-the-serial-port-name-from-usb-info/77834/6 "2023-10-27T12:36:31Z")

</div>

you should get anything possible from  
`system_profiler SPUSBDataType`

---

<div class="post-metadata">

**Author:** ![Ulrich\_Bogun](https://forum.xojo.com/user_avatar/forum.xojo.com/ulrich_bogun/32/1864_2.png) [@Ulrich\_Bogun](https://forum.xojo.com/u/Ulrich_Bogun)\
**Post date:** [October 27, 2023, 1:06pm UTC](https://forum.xojo.com/t/any-way-to-get-the-serial-port-name-from-usb-info/77834/7 "2023-10-27T13:06:43Z")

</div>

Excellent! Thank you!

---

<div class="post-metadata">

**Author:** ![Julia\_Truchsess](https://forum.xojo.com/user_avatar/forum.xojo.com/julia_truchsess/32/499_2.png) [@Julia\_Truchsess](https://forum.xojo.com/u/Julia_Truchsess)\
**Post date:** [October 27, 2023, 10:32pm UTC](https://forum.xojo.com/t/any-way-to-get-the-serial-port-name-from-usb-info/77834/8 "2023-10-27T22:32:15Z")

</div>

> [@Jean-Yves\_Pochez](#):
>
> you should get anything possible from  
> `system_profiler SPUSBDataType`

How would one go about querying System Profiler data programmatically?

---

<div class="post-metadata">

**Author:** ![Jean-Yves\_Pochez](https://forum.xojo.com/user_avatar/forum.xojo.com/jean-yves_pochez/32/21769_2.png) [@Jean-Yves\_Pochez](https://forum.xojo.com/u/Jean-Yves_Pochez)\
**Post date:** [October 28, 2023, 9:20am UTC](https://forum.xojo.com/t/any-way-to-get-the-serial-port-name-from-usb-info/77834/9 "2023-10-28T09:20:20Z")

</div>

it could be easier with the -json or the -xml option to parse the result.

```auto
system_profiler -json SPUSBDataType

```

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [October 28, 2023, 9:59am UTC](https://forum.xojo.com/t/any-way-to-get-the-serial-port-name-from-usb-info/77834/10 "2023-10-28T09:59:21Z")

</div>

Using Shell class to run command line tool and parse result.

---

<div class="post-metadata">

**Author:** ![Julia\_Truchsess](https://forum.xojo.com/user_avatar/forum.xojo.com/julia_truchsess/32/499_2.png) [@Julia\_Truchsess](https://forum.xojo.com/u/Julia_Truchsess)\
**Post date:** [October 28, 2023, 5:48pm UTC](https://forum.xojo.com/t/any-way-to-get-the-serial-port-name-from-usb-info/77834/11 "2023-10-28T17:48:18Z")

</div>

Bingo.

```
"_items" : [
        {
          "_name" : "FT232R USB UART",
          "bcd_device" : "6.00",
          "bus_power" : "500",
          "bus_power_used" : "90",
          "device_speed" : "full_speed",
          "extra_current_used" : "0",
          "location_id" : "0x01140000 / 3",
          "manufacturer" : "FTDI",
          "product_id" : "0x6001",
          "serial_num" : "AB89UL00",
          "vendor_id" : "0x0403 (Future Technology Devices International Limited)"
        },

```

@Roger_Meier

---

<div class="post-metadata">

**Author:** ![Roger\_Meier](https://forum.xojo.com/user_avatar/forum.xojo.com/roger_meier/32/260_2.png) [@Roger\_Meier](https://forum.xojo.com/u/Roger_Meier)\
**Post date:** [October 30, 2023, 5:46pm UTC](https://forum.xojo.com/t/any-way-to-get-the-serial-port-name-from-usb-info/77834/12 "2023-10-30T17:46:27Z")

</div>

> [@Jean-Yves\_Pochez](#):
>
> `system_profiler -json SPUSBDataType`

Nice! Now to correlate the output of `system_profiler -json SPUSBDataType` with `SerialDevice.Name` 🙂.

---

<div class="post-metadata">

**Author:** ![Ulrich\_Bogun](https://forum.xojo.com/user_avatar/forum.xojo.com/ulrich_bogun/32/1864_2.png) [@Ulrich\_Bogun](https://forum.xojo.com/u/Ulrich_Bogun)\
**Post date:** [October 31, 2023, 11:26am UTC](https://forum.xojo.com/t/any-way-to-get-the-serial-port-name-from-usb-info/77834/13 "2023-10-31T11:26:13Z")

</div>

> [@Roger\_Meier](#):
>
> Nice! Now to correlate the output of `system_profiler -json SPUSBDataType` with `SerialDevice.Name` 🙂.

In my case, where name is reported as USB Serial, I found the serial port name to be usbserial-[locationID.Left(locationID.netlength -1)]  
With net length I mean the hex value of locationID only, without slash and USB Address (“/ 3” in Julia’s example). So with Julia’s example my serial port name would be “usbserial-0114000” probably. (One trailing 0 removed).

---

<div class="post-metadata">

**Author:** ![Roger\_Meier](https://forum.xojo.com/user_avatar/forum.xojo.com/roger_meier/32/260_2.png) [@Roger\_Meier](https://forum.xojo.com/u/Roger_Meier)\
**Post date:** [October 31, 2023, 4:48pm UTC](https://forum.xojo.com/t/any-way-to-get-the-serial-port-name-from-usb-info/77834/14 "2023-10-31T16:48:00Z")

</div>

Yes, for SOME of them this is true. Others, however, use the serial number instead of the location ID (and some add an index number in case of an adapter with multiple ports). Some may also use the string “usbmodem” rather than “usbserial-”.

So, the approach to look up the description and manufacturer for a given port name is to strip those strings from the port name, like this

```auto
var ID as string = PortName
ID = ID.Replace("wchusbserial", "")
ID = ID.Replace("usbmodem", "")
ID = ID.Replace("usbserial-", "")

```

and then to iterate through the JSON structure to find the item that contains the remaining string in either the 'serial\_num" or “location\_id” strings.

```auto
if item.HasKey("serial_num") or item.hasKey("location_id") then
  var serNo as string = if(item.HasKey("serial_num"), item.Value("serial_num"), "There is no serial number")
  var locID as string = item.Value("location_id")
  var locIDs() as string = locID.Split(" ")
  locID = locIDs(0).Replace("0x", "")
  if ID.IndexOf(serNo) >= 0 or ID.IndexOf(locID) >= 0 then
    var name as string = item.Value("_name")
    var manufacturer as string
    if item.HasKey("manufacturer") then
      manufacturer = item.Value("manufacturer")
    end if
    return new Pair(name, manufacturer)
  else 
    return nil
  end if
end if

```

I’m sure this is not the sexiest implementation, but this seems to work ok, at least for the plethora of USB/Serial devices I have at my disposal.

---

<div class="post-metadata">

**Author:** ![Ulrich\_Bogun](https://forum.xojo.com/user_avatar/forum.xojo.com/ulrich_bogun/32/1864_2.png) [@Ulrich\_Bogun](https://forum.xojo.com/u/Ulrich_Bogun)\
**Post date:** [November 2, 2023, 12:06pm UTC](https://forum.xojo.com/t/any-way-to-get-the-serial-port-name-from-usb-info/77834/15 "2023-11-02T12:06:00Z")

</div>

Just found a weird bug on macOS Ventura on an Intel iMac – Sonoma & M1 works great, which seems to be related to Xojo’s shell class.  
On the iMac, `system_profiler -json SPUSBDataType` produces some errors first. I can see them when I run the command in terminal too. But followed by the JSON as requested which I can see in Debugger in Shell.Result:

```auto
2023-11-02 12:55:05.141 system_profiler[2812:58324] SPUSBDevice: IOCreatePlugInInterfaceForService failed 0xe00002be
2023-11-02 12:55:05.142 system_profiler[2812:58324] SPUSBDevice: IOCreatePlugInInterfaceForService failed 0xe00002be
{
  "SPUSBDataType" : [
    {
      "_items" : [
        {
          "_items" : [
            {
              "_name" : "Bluetooth USB Host Controller",
              "bcd_device" : "1.50",
              "location_id" : "0x14630000",
              "manufacturer" : "Apple Inc.",
              "product_id" : "0x828d",
              "vendor_id" : "apple_vendor_id"
            }
          ], 
(//and so on)
}}}

```

when I read Shell.Result into a string where I want to remove the failure lines, the string is truncated to 256 characters:

```auto
2023-11-02 12:55:05.141 system_profiler[2812:58324] SPUSBDevice: IOCreatePlugInInterfaceForService failed 0xe00002be
2023-11-02 12:55:05.142 system_profiler[2812:58324] SPUSBDevice: IOCreatePlugInInterfaceForService failed 0xe00002be
{
  "SPUSBDataType" :

```

Someone able to confirm this?

---

<div class="post-metadata">

**Author:** ![Ian\_Kennedy](https://forum.xojo.com/user_avatar/forum.xojo.com/ian_kennedy/32/6860_2.png) [@Ian\_Kennedy](https://forum.xojo.com/u/Ian_Kennedy)\
**Post date:** [November 2, 2023, 12:22pm UTC](https://forum.xojo.com/t/any-way-to-get-the-serial-port-name-from-usb-info/77834/16 "2023-11-02T12:22:02Z")

</div>

I just tried this code in 2023r4 beta and it returns lots of lines (far greater than 256 chars). **Ah, sorry, I’m on Sanoma. so I don’t think that helps.**

```auto
Var she As New shell
she.Execute "system_profiler -json SPUSBDataType"
MessageBox she.result

```

What mode are you using and did you try shell.ReadAll?

I just tried it on an old Intel Mini running 12.7.1 (Monterey) and it also worked fine. Not sure if that helps.

---

<div class="post-metadata">

**Author:** ![Ulrich\_Bogun](https://forum.xojo.com/user_avatar/forum.xojo.com/ulrich_bogun/32/1864_2.png) [@Ulrich\_Bogun](https://forum.xojo.com/u/Ulrich_Bogun)\
**Post date:** [November 2, 2023, 1:00pm UTC](https://forum.xojo.com/t/any-way-to-get-the-serial-port-name-from-usb-info/77834/17 "2023-11-02T13:00:50Z")

</div>

Thank you. I could in between find that the error does not appear with Xojo2023r2. The profiler errors persist of course, but neither Result nor ReadAll are truncated.

It’s a synchronous shell and I am debugging remotely on my old iMac. Will try 2023r3 and r3.1 for when the error occurs. I can even see the full text in my copied string flath briefly when I switch to debugger but the rest appears after a fraction of a second. And error exists for me and a customer in build too.

---

<div class="post-metadata">

**Author:** ![Ulrich\_Bogun](https://forum.xojo.com/user_avatar/forum.xojo.com/ulrich_bogun/32/1864_2.png) [@Ulrich\_Bogun](https://forum.xojo.com/u/Ulrich_Bogun)\
**Post date:** [November 2, 2023, 1:36pm UTC](https://forum.xojo.com/t/any-way-to-get-the-serial-port-name-from-usb-info/77834/18 "2023-11-02T13:36:16Z")

</div>

hmm … Count this under “oddities”.  
I created a demo project, and no truncation error anymore. Differences were that in my original project, the shell containing method is triggered by a macUSBNotification event, and a different order of checking ExitCode and Result. I copied the code order to my main project and now do not see that bug anymore. Weird, but at least working, it seems.

---

<div class="post-metadata">

**Author:** ![Ian\_Kennedy](https://forum.xojo.com/user_avatar/forum.xojo.com/ian_kennedy/32/6860_2.png) [@Ian\_Kennedy](https://forum.xojo.com/u/Ian_Kennedy)\
**Post date:** [November 2, 2023, 1:47pm UTC](https://forum.xojo.com/t/any-way-to-get-the-serial-port-name-from-usb-info/77834/19 "2023-11-02T13:47:50Z")

</div>

odd indeed. If you can still reproduce the original issue, it would be worth posting a bug report.
