Files
ch340-webusb/examples/minimal.html
T
jiangchen 6c5e647c8a
test / test (push) Canceled after 0s
feat: v1.0.0 收尾 — 五芯片 DTR/RTS、类型声明、示例、CI
- CP210x / FTDI / PL2303 补齐 setSignals()(编码与各自初始化序列一致)
- 新增 index.d.ts(TypeScript 类型声明)、examples/minimal.html(最小接入示例)
- 新增 .gitea/workflows/ci.yml(push 时跑离线单测)、.gitattributes(强制 LF)、CHANGELOG.md
- package.json 补 repository / homepage / bugs / types / engines
- 单测 16 项全部通过:node test/driver.test.mjs
2026-09-23 00:41:29 +08:00

54 lines
2.0 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ch340-webusb 最小示例</title>
<style>
body { font:14px/1.6 system-ui,sans-serif; margin:0 auto; max-width:640px; padding:16px; }
button { padding:10px 14px; font-size:14px; }
pre { background:#0b0d12; color:#d8dee9; padding:10px; border-radius:8px; height:220px; overflow:auto; }
</style>
</head>
<body>
<h3>ch340-webusb 最小示例</h3>
<p>
<button id="connect">连接 CH340</button>
<button id="close" disabled>断开</button>
<input id="text" value="hello" size="12">
<button id="send" disabled>发送</button>
</p>
<pre id="log"></pre>
<script src="../ch340-webusb.js"></script>
<script>
// 1. 选设备(必须由用户点击触发)
document.getElementById('connect').onclick = async () => {
const port = await TamaCh340.requestPort({ baudRate: 115200 });
// 2. 打开:内部完成 CH340 芯片初始化(波特率 / LCR / DTR-RTS)
await port.open({ baudRate: 115200 });
const writer = port.writable.getWriter();
const reader = port.readable.getReader();
(async () => { // 3. 收
for (;;) {
const { value, done } = await reader.read();
if (done) break;
log('RX ' + Array.from(value).map(b => b.toString(16).padStart(2, '0')).join(' '));
}
})();
document.getElementById('send').onclick = () => // 4. 发
writer.write(new TextEncoder().encode(document.getElementById('text').value + '\r\n'));
document.getElementById('send').disabled = false;
document.getElementById('connect').disabled = true;
document.getElementById('close').disabled = false;
log('已连接 ' + JSON.stringify(port.getInfo()) + ' chip=' + port.chip);
};
document.getElementById('close').onclick = async () => { location.reload(); };
function log(s) { const el = document.getElementById('log'); el.textContent += s + '\n'; el.scrollTop = el.scrollHeight; }
</script>
</body>
</html>