Created
October 3, 2024 17:59
-
-
Save hlord2000/ccc70b7b8443a6b3dd86b49b7bb54296 to your computer and use it in GitHub Desktop.
Serial Device Manager, defaults to using tio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sdm() { | |
# ANSI color codes | |
local RED=$'\033[0;31m' | |
local GREEN=$'\033[0;32m' | |
local YELLOW=$'\033[1;33m' | |
local BLUE=$'\033[0;34m' | |
local MAGENTA=$'\033[0;35m' | |
local CYAN=$'\033[0;36m' | |
local BOLD=$'\033[1m' | |
local UNDERLINE=$'\033[4m' | |
local NC=$'\033[0m' # No Color | |
# Function to get USB device information | |
get_usb_info() { | |
local device=$1 | |
local info=$(udevadm info -n "$device" 2>/dev/null) | |
# Try different methods to get the product name | |
local product=$(echo "$info" | grep -E 'ID_MODEL=|ID_MODEL_ENC=' | head -n1 | cut -d= -f2 | sed 's/^"//; s/"$//' | xxd -r -p 2>/dev/null | tr -dc '[:print:]' || echo "N/A") | |
# If product is empty or only whitespace, try ID_USB_MODEL_FROM_DATABASE | |
if [[ -z "${product// }" ]]; then | |
product=$(echo "$info" | grep 'ID_USB_MODEL_FROM_DATABASE=' | cut -d= -f2 | sed 's/^"//; s/"$//') | |
fi | |
# If still empty, use ID_VENDOR_FROM_DATABASE + ID_MODEL_FROM_DATABASE | |
if [[ -z "${product// }" ]]; then | |
local vendor=$(echo "$info" | grep 'ID_VENDOR_FROM_DATABASE=' | cut -d= -f2 | sed 's/^"//; s/"$//') | |
local model=$(echo "$info" | grep 'ID_MODEL_FROM_DATABASE=' | cut -d= -f2 | sed 's/^"//; s/"$//') | |
product="${vendor} ${model}" | |
fi | |
# Get serial number | |
local serial=$(echo "$info" | grep 'ID_SERIAL_SHORT=' | cut -d= -f2) | |
# If product or serial is empty, set to N/A | |
[[ -z "${product// }" ]] && product="N/A" | |
[[ -z "${serial// }" ]] && serial="N/A" | |
echo "${product}|${serial}" | |
} | |
# Check if the directory exists | |
if [ ! -d "/dev/serial/by-id" ]; then | |
echo "${RED}${BOLD}The directory /dev/serial/by-id/ does not exist.${NC}" | |
return 1 | |
fi | |
# Get all devices | |
devices=(${(f)"$(ls -A /dev/serial/by-id/)"}) | |
# Check if there are any devices | |
if [ ${#devices[@]} -eq 0 ]; then | |
echo "${YELLOW}${BOLD}No devices found in /dev/serial/by-id/${NC}" | |
return 0 | |
fi | |
# Print menu | |
echo "\n${CYAN}${BOLD}${UNDERLINE}Available Serial Devices:${NC}" | |
echo "${CYAN}${BOLD}===========================${NC}\n" | |
local valid_devices=() | |
for device in "${devices[@]}"; do | |
if [[ -z "$device" ]]; then | |
echo "${YELLOW}Warning: Empty device entry found. Skipping.${NC}" | |
continue | |
fi | |
full_path="/dev/serial/by-id/${device}" | |
if [[ ! -e "$full_path" ]]; then | |
echo "${YELLOW}Warning: Device $device does not exist. Skipping.${NC}" | |
continue | |
fi | |
info=$(get_usb_info "$full_path") | |
IFS='|' read -r product serial <<< "$info" | |
valid_devices+=("$device") | |
i=${#valid_devices[@]} | |
echo "${GREEN}${BOLD}${i})${NC} ${BLUE}${UNDERLINE}${device}${NC}" | |
echo " ${MAGENTA}Product:${NC} ${YELLOW}${product}${NC}" | |
echo " ${MAGENTA}Serial:${NC} ${YELLOW}${serial}${NC}\n" | |
done | |
if [ ${#valid_devices[@]} -eq 0 ]; then | |
echo "${YELLOW}${BOLD}No valid devices found in /dev/serial/by-id/${NC}" | |
return 0 | |
fi | |
# Get user selection | |
while true; do | |
echo -n "${CYAN}${BOLD}Select a device number (${GREEN}1-${#valid_devices[@]}${CYAN}) or '${RED}q${CYAN}' to quit:${NC} " | |
read selection | |
if [[ "$selection" == "q" ]]; then | |
echo "\n${YELLOW}Exiting.${NC}" | |
return 0 | |
elif [[ "$selection" =~ ^[0-9]+$ ]] && [ "$selection" -ge 1 ] && [ "$selection" -le "${#valid_devices[@]}" ]; then | |
break | |
else | |
echo "${RED}Invalid selection. Please try again.${NC}" | |
fi | |
done | |
# Get the selected device | |
selected_device="/dev/serial/by-id/${valid_devices[$selection-1]}" | |
# Connect using tio | |
echo "\n${GREEN}${BOLD}Connecting to ${UNDERLINE}$selected_device${NC}${GREEN}${BOLD} using tio...${NC}" | |
tio "$selected_device" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment