|
@@ -231,8 +231,40 @@ function renderModels(models) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+function isVirtualSerialPort(name) {
|
|
|
|
|
+ return /debug-console|Bluetooth|LineFree/i.test(name);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function serialPortScore(port) {
|
|
|
|
|
+ const name = port.port_name || '';
|
|
|
|
|
+ if (/^\/dev\/cu\.(usbmodem|usbserial|wchusbserial|SLAB_USBtoUART)/i.test(name)) return 0;
|
|
|
|
|
+ if (/^\/dev\/cu\./i.test(name) && !isVirtualSerialPort(name)) return 1;
|
|
|
|
|
+ if (isVirtualSerialPort(name)) return 3;
|
|
|
|
|
+ return 2;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function updatePortHint() {
|
|
|
|
|
+ if (!state.ports.length) {
|
|
|
|
|
+ els.portHint.innerHTML =
|
|
|
|
|
+ '没有发现串口。请检查 USB 线是否支持数据传输;macOS 通常显示为 <code>/dev/cu.usbmodem*</code>。CH340/CP210x 可能需要安装驱动。';
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ const current = state.ports.find((port) => port.port_name === state.port);
|
|
|
|
|
+ els.portHint.textContent = current
|
|
|
|
|
+ ? `已发现 ${state.ports.length} 个串口,当前选择 ${current.port_name}${current.product ? `(${current.product})` : ''}。`
|
|
|
|
|
+ : `已发现 ${state.ports.length} 个串口,请选择设备。`;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
function renderPorts() {
|
|
function renderPorts() {
|
|
|
els.portSelect.textContent = '';
|
|
els.portSelect.textContent = '';
|
|
|
|
|
+ state.ports = (Array.isArray(state.ports) ? state.ports : [])
|
|
|
|
|
+ .filter((port) => port && port.port_name)
|
|
|
|
|
+ .sort(
|
|
|
|
|
+ (a, b) =>
|
|
|
|
|
+ serialPortScore(a) - serialPortScore(b) ||
|
|
|
|
|
+ a.port_name.localeCompare(b.port_name),
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
if (!state.ports.length) {
|
|
if (!state.ports.length) {
|
|
|
const option = document.createElement('option');
|
|
const option = document.createElement('option');
|
|
|
option.value = '';
|
|
option.value = '';
|
|
@@ -240,8 +272,7 @@ function renderPorts() {
|
|
|
els.portSelect.append(option);
|
|
els.portSelect.append(option);
|
|
|
state.port = '';
|
|
state.port = '';
|
|
|
els.portCount.textContent = '0 个';
|
|
els.portCount.textContent = '0 个';
|
|
|
- els.portHint.innerHTML =
|
|
|
|
|
- '没有发现串口。请检查 USB 线是否支持数据传输;macOS 通常显示为 <code>/dev/cu.usbmodem*</code>。CH340/CP210x 可能需要安装驱动。';
|
|
|
|
|
|
|
+ updatePortHint();
|
|
|
} else {
|
|
} else {
|
|
|
for (const port of state.ports) {
|
|
for (const port of state.ports) {
|
|
|
const option = document.createElement('option');
|
|
const option = document.createElement('option');
|
|
@@ -252,13 +283,11 @@ function renderPorts() {
|
|
|
option.textContent = bits.join(' · ');
|
|
option.textContent = bits.join(' · ');
|
|
|
els.portSelect.append(option);
|
|
els.portSelect.append(option);
|
|
|
}
|
|
}
|
|
|
- state.port = state.ports[0]?.port_name || '';
|
|
|
|
|
|
|
+ const keep = state.port && state.ports.some((port) => port.port_name === state.port);
|
|
|
|
|
+ state.port = keep ? state.port : state.ports[0].port_name;
|
|
|
els.portSelect.value = state.port;
|
|
els.portSelect.value = state.port;
|
|
|
els.portCount.textContent = `${state.ports.length} 个`;
|
|
els.portCount.textContent = `${state.ports.length} 个`;
|
|
|
- const first = state.ports[0];
|
|
|
|
|
- els.portHint.textContent = first
|
|
|
|
|
- ? `已发现 ${state.ports.length} 个串口,当前选择 ${first.port_name}${first.product ? `(${first.product})` : ''}。`
|
|
|
|
|
- : '';
|
|
|
|
|
|
|
+ updatePortHint();
|
|
|
}
|
|
}
|
|
|
updateFlashButton();
|
|
updateFlashButton();
|
|
|
}
|
|
}
|
|
@@ -464,7 +493,15 @@ function bindEvents() {
|
|
|
$('#btn-refresh-ports').addEventListener('click', () => refreshPorts(false));
|
|
$('#btn-refresh-ports').addEventListener('click', () => refreshPorts(false));
|
|
|
els.portSelect.addEventListener('change', () => {
|
|
els.portSelect.addEventListener('change', () => {
|
|
|
state.port = els.portSelect.value;
|
|
state.port = els.portSelect.value;
|
|
|
|
|
+ updatePortHint();
|
|
|
updateFlashButton();
|
|
updateFlashButton();
|
|
|
|
|
+ const current = state.ports.find((port) => port.port_name === state.port);
|
|
|
|
|
+ if (current) {
|
|
|
|
|
+ log(
|
|
|
|
|
+ `已选择串口:${current.port_name}${current.product ? `(${current.product})` : ''}`,
|
|
|
|
|
+ 'muted',
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
$('#btn-browse').addEventListener('click', async (event) => {
|
|
$('#btn-browse').addEventListener('click', async (event) => {
|