functions/public/Get-KlippySerialDevice.ps1

function Get-KlippySerialDevice {
    <#
    .SYNOPSIS
        Gets serial device information from Moonraker peripherals.

    .DESCRIPTION
        Retrieves serial device details using the JSON-RPC method
        machine.peripherals.serial.

    .PARAMETER Id
        The unique identifier of the printer.

    .PARAMETER PrinterName
        The friendly name of the printer.

    .PARAMETER InputObject
        A printer object from pipeline input.

    .PARAMETER Raw
        Return the raw response without normalization.

    .EXAMPLE
        Get-KlippySerialDevice
        Gets serial devices from the default printer.

    .EXAMPLE
        Get-KlippySerialDevice -PrinterName "voronv2"
        Gets serial devices from the specified printer.

    .EXAMPLE
        Get-KlippyPrinter | Get-KlippySerialDevice
        Gets serial devices from all registered printers.

    .OUTPUTS
        PSCustomObject with serial device information.
    #>

    [CmdletBinding(DefaultParameterSetName = 'Default')]
    [OutputType([PSCustomObject])]
    param(
        [Parameter(Mandatory = $true, ParameterSetName = 'ById')]
        [ValidateNotNullOrEmpty()]
        [string]$Id,

        [Parameter(Mandatory = $true, ParameterSetName = 'ByName', Position = 0)]
        [ValidateNotNullOrEmpty()]
        [string]$PrinterName,

        [Parameter(Mandatory = $true, ParameterSetName = 'ByObject', ValueFromPipeline = $true)]
        [PSCustomObject]$InputObject,

        [Parameter()]
        [switch]$Raw
    )

    process {
        # Resolve printer
        $resolveParams = @{}
        switch ($PSCmdlet.ParameterSetName) {
            'ById' { $resolveParams['Id'] = $Id }
            'ByName' { $resolveParams['PrinterName'] = $PrinterName }
            'ByObject' { $resolveParams['InputObject'] = $InputObject }
        }

        $printer = Resolve-KlippyPrinterTarget @resolveParams

        try {
            Write-Verbose "[$($printer.PrinterName)] Fetching serial peripherals..."

            $serial = Invoke-KlippyJsonRpcMethod -Printer $printer -Method "machine.peripherals.serial" -NoNormalize:$Raw

            if ($Raw) {
                return $serial
            }

            $serialDevices = $null
            if ($serial -is [array]) {
                $serialDevices = $serial
            }
            elseif ($serial.SerialDevices) {
                $serialDevices = $serial.SerialDevices
            }
            elseif ($serial.Devices) {
                $serialDevices = $serial.Devices
            }
            elseif ($serial.serial_devices) {
                $serialDevices = $serial.serial_devices
            }

            if (-not $serialDevices -or $serialDevices.Count -eq 0) {
                Write-Verbose "[$($printer.PrinterName)] No serial devices found"
                return
            }

            foreach ($device in $serialDevices) {
                [PSCustomObject]@{
                    PSTypeName     = 'KlippyCLI.SerialDevice'
                    PrinterId      = $printer.Id
                    PrinterName    = $printer.PrinterName
                    DevicePath     = $device.DevicePath ?? $device.device_path ?? $device.Path ?? $device.path
                    DeviceNumber   = $device.DeviceNum ?? $device.device_num ?? $device.DeviceNumber ?? $device.device_number
                    DeviceType     = $device.DeviceType ?? $device.device_type ?? $device.Type ?? $device.type
                    Driver         = $device.Driver ?? $device.driver ?? $device.DriverName ?? $device.driver_name
                    HardwarePath   = $device.HardwarePath ?? $device.hardware_path ?? $device.PathById ?? $device.path_by_id
                    PathByHardware = $device.PathByHardware ?? $device.path_by_hardware
                }
            }
        }
        catch {
            Write-Error "[$($printer.PrinterName)] Failed to get serial peripherals: $_"
        }
    }
}