Skip to content

Instantly share code, notes, and snippets.

@smittytone
Last active September 12, 2022 18:48
Show Gist options
  • Save smittytone/15d00976df5b702debdcb3a8ae8f5bae to your computer and use it in GitHub Desktop.
Save smittytone/15d00976df5b702debdcb3a8ae8f5bae to your computer and use it in GitHub Desktop.
Z Shell function for macOS which detects and lists USB-connected serial devices
# Add to your `.zshrc` file or use the function's contents as a script.
#
# With the former, add the function, restart your terminal, connect
# a USB serial device, eg. a Raspberry Pi Pico, and then enter `dlist`
# at the command line.
#
# You should see something like this:
# `/dev/cu.usbserial-01AB8E0B`
#
# If you have only one device attached, you can include the function's
# output in other command line call. For example:
#
# `pyboard -d $(dlist) -f cp prefs.json :prefs.json`
#
# If you have multiple devices attached, `dlist` will list them all:
#
# `1. /dev/cu.usbmodem1101
# 2. /dev/cu.usbserial-01AB8E0B`
#
# You can then specify a single device by number. For example:
#
# `pyboard -d $(dlist 2) -f cp prefs.json :prefs.json`
#
# NOTE Requires macOS
dlist() {
local devices=($(/bin/ls /dev/cu.usb* 2>/dev/null))
if [[ ${#devices} -gt 0 ]]; then
if [[ ${#devices} -eq 1 ]]; then
echo ${devices}
else
local count=1
for device in ${devices}; do
if [[ -n "$1" && "$1" == "$count" ]]; then
echo ${device}
elif [[ -z "$1" ]]; then
echo "${count}. ${device}"
fi
((count+=1))
done
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment