import { useCallback, useEffect, useState } from 'react' import { Cable, RefreshCw } from 'lucide-react' import Modal from '../common/Modal' import { listSerialPorts } from '../../platform/serial' import type { SerialConfig, SerialFlowControl, SerialParity, SerialPortSummary, } from '../../../shared/types/serial.types' interface SerialConnectDialogProps { isOpen: boolean onClose: () => void onConnect: (config: SerialConfig) => void } const BAUD_RATES = [9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600] export default function SerialConnectDialog({ isOpen, onClose, onConnect, }: SerialConnectDialogProps) { const [ports, setPorts] = useState([]) const [path, setPath] = useState('/dev/tty.debug-console') const [baudRate, setBaudRate] = useState(115200) const [dataBits, setDataBits] = useState<5 | 6 | 7 | 8>(8) const [stopBits, setStopBits] = useState<1 | 2>(1) const [parity, setParity] = useState('none') const [flowControl, setFlowControl] = useState('none') const [loadingPorts, setLoadingPorts] = useState(false) const [error, setError] = useState(null) const refreshPorts = useCallback(async () => { setLoadingPorts(true) setError(null) try { const detected = await listSerialPorts() setPorts(detected) setPath((currentPath) => detected.length > 0 && !detected.some((port) => port.path === currentPath) ? detected[0].path : currentPath ) } catch (reason) { setError(reason instanceof Error ? reason.message : String(reason)) } finally { setLoadingPorts(false) } }, []) useEffect(() => { if (isOpen) void refreshPorts() }, [isOpen, refreshPorts]) const handleConnect = () => { if (!path.trim()) { setError('请输入串口设备路径') return } onConnect({ path: path.trim(), baudRate, dataBits, stopBits, parity, flowControl, }) onClose() } return ( } confirmText="打开串口" size="lg" >
setPath(event.target.value)} placeholder="/dev/tty.debug-console" className="ui-input font-mono" spellCheck={false} /> {ports.length > 0 ? (
{ports.map((port) => ( ))}
) : (

未枚举到设备时可直接输入路径,例如 /dev/tty.debug-console 或 /dev/tty.Bluetooth-Incoming-Port。

)}
{error &&

{error}

}
) }