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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="ja">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 6 |
+
<title>Arduino シリアル通信</title>
|
| 7 |
+
<style>
|
| 8 |
+
:root {
|
| 9 |
+
--primary-color: #1e88e5;
|
| 10 |
+
--primary-hover: #1565c0;
|
| 11 |
+
--background-color: #ffffff; /* 背景を白に */
|
| 12 |
+
--text-color: #212121;
|
| 13 |
+
--border-color: #e0e0e0;
|
| 14 |
+
--placeholder-color: #757575;
|
| 15 |
+
--error-color: #d32f2f;
|
| 16 |
+
--success-color: #388e3c;
|
| 17 |
+
--input-background: #fafafa; /* 入力欄とテキストエリアの統一背景 */
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
* {
|
| 21 |
+
box-sizing: border-box;
|
| 22 |
+
margin: 0;
|
| 23 |
+
padding: 0;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
/* paddingに関連するスタイル */
|
| 27 |
+
body {
|
| 28 |
+
padding: 16px;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
.container {
|
| 32 |
+
padding: 32px;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
button {
|
| 36 |
+
padding: 12px;
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
input[type="text"] {
|
| 40 |
+
padding: 12px 16px;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
textarea {
|
| 44 |
+
padding: 16px;
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
@media (max-width: 600px) {
|
| 48 |
+
.container {
|
| 49 |
+
padding: 24px;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
button {
|
| 53 |
+
padding: 12px;
|
| 54 |
+
}
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
/* その他のスタイル */
|
| 58 |
+
body {
|
| 59 |
+
font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
| 60 |
+
background: var(--background-color);
|
| 61 |
+
color: var(--text-color);
|
| 62 |
+
display: flex;
|
| 63 |
+
justify-content: center;
|
| 64 |
+
align-items: flex-start;
|
| 65 |
+
min-height: 100vh;
|
| 66 |
+
line-height: 1.5;
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
.container {
|
| 70 |
+
background: var(--background-color); /* 背景と統一(白) */
|
| 71 |
+
width: 100%;
|
| 72 |
+
max-width: 900px;
|
| 73 |
+
display: grid;
|
| 74 |
+
grid-template-columns: 1fr 2fr;
|
| 75 |
+
gap: 24px;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
.sidebar {
|
| 79 |
+
display: flex;
|
| 80 |
+
flex-direction: column;
|
| 81 |
+
gap: 16px;
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
.main {
|
| 85 |
+
display: flex;
|
| 86 |
+
flex-direction: column;
|
| 87 |
+
gap: 16px;
|
| 88 |
+
position: relative; /* コピーボタンの位置指定用 */
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
h2 {
|
| 92 |
+
text-align: center;
|
| 93 |
+
font-size: 1.8rem;
|
| 94 |
+
margin-bottom: 24px;
|
| 95 |
+
color: var(--text-color);
|
| 96 |
+
font-weight: 600;
|
| 97 |
+
grid-column: 1 / -1;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
.button-group {
|
| 101 |
+
display: flex;
|
| 102 |
+
flex-wrap: wrap;
|
| 103 |
+
gap: 12px;
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
button {
|
| 107 |
+
background: var(--primary-color);
|
| 108 |
+
color: white;
|
| 109 |
+
border: none;
|
| 110 |
+
border-radius: 8px;
|
| 111 |
+
font-size: 1rem;
|
| 112 |
+
font-weight: 500;
|
| 113 |
+
cursor: pointer;
|
| 114 |
+
transition: background-color 0.2s ease, transform 0.1s ease;
|
| 115 |
+
flex: 1;
|
| 116 |
+
min-width: 100px;
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
button:hover {
|
| 120 |
+
background: var(--primary-hover);
|
| 121 |
+
transform: translateY(-1px);
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
button:active {
|
| 125 |
+
transform: translateY(0);
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
button:disabled {
|
| 129 |
+
background: #b0bec5;
|
| 130 |
+
cursor: not-allowed;
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
select, input[type="text"] {
|
| 134 |
+
width: 100%;
|
| 135 |
+
font-size: 1rem;
|
| 136 |
+
border: 1px solid var(--border-color);
|
| 137 |
+
border-radius: 8px;
|
| 138 |
+
margin-bottom: 16px;
|
| 139 |
+
background: var(--input-background);
|
| 140 |
+
padding: 12px 16px;
|
| 141 |
+
transition: border-color 0.2s ease;
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
select:focus, input[type="text"]:focus {
|
| 145 |
+
outline: none;
|
| 146 |
+
border-color: var(--primary-color);
|
| 147 |
+
box-shadow: 0 0 0 3px rgba(30, 136, 229, 0.1);
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
input[type="text"]::placeholder {
|
| 151 |
+
color: var(--placeholder-color);
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
textarea {
|
| 155 |
+
width: 100%;
|
| 156 |
+
height: 300px; /* 受信データの高さ */
|
| 157 |
+
font-size: 0.95rem;
|
| 158 |
+
border: 1px solid var(--border-color);
|
| 159 |
+
border-radius: 8px;
|
| 160 |
+
background: var(--input-background);
|
| 161 |
+
resize: none;
|
| 162 |
+
line-height: 1.5;
|
| 163 |
+
transition: border-color 0.2s ease;
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
textarea:focus {
|
| 167 |
+
outline: none;
|
| 168 |
+
border-color: var(--primary-color);
|
| 169 |
+
box-shadow: 0 0 0 3px rgba(30, 136, 229, 0.1);
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
.copy-button {
|
| 173 |
+
position: absolute;
|
| 174 |
+
top: 10px;
|
| 175 |
+
right: 10px;
|
| 176 |
+
cursor: pointer;
|
| 177 |
+
padding: 4px;
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
.copy-button img {
|
| 181 |
+
width: 16px;
|
| 182 |
+
height: 16px;
|
| 183 |
+
/* 変色(不透明度変化)なし */
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
.status {
|
| 187 |
+
text-align: center;
|
| 188 |
+
margin-bottom: 16px;
|
| 189 |
+
font-size: 0.9rem;
|
| 190 |
+
color: var(--placeholder-color);
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
.status.connected {
|
| 194 |
+
color: var(--success-color);
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
.error {
|
| 198 |
+
color: var(--error-color);
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
.history {
|
| 202 |
+
height: 123px; /* テキスト3行分 */
|
| 203 |
+
border: 1px solid var(--border-color);
|
| 204 |
+
border-radius: 1px;
|
| 205 |
+
padding: 1px;
|
| 206 |
+
background: var(--input-background);
|
| 207 |
+
/* スクロール無効化 */
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
.history-item {
|
| 211 |
+
padding: 8px;
|
| 212 |
+
cursor: pointer;
|
| 213 |
+
border-radius: 4px;
|
| 214 |
+
transition: background-color 0.2s ease;
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
+
.history-item:hover {
|
| 218 |
+
background: rgba(0, 0, 0, 0.05);
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
@media (max-width: 768px) {
|
| 222 |
+
.container {
|
| 223 |
+
grid-template-columns: 1fr;
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
h2 {
|
| 227 |
+
font-size: 1.5rem;
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
.button-group {
|
| 231 |
+
flex-direction: column;
|
| 232 |
+
}
|
| 233 |
+
}
|
| 234 |
+
</style>
|
| 235 |
+
</head>
|
| 236 |
+
<body>
|
| 237 |
+
<div class="container">
|
| 238 |
+
<div class="sidebar">
|
| 239 |
+
<div class="status" id="status">未接続</div>
|
| 240 |
+
<select id="baudRate">
|
| 241 |
+
<option value="9600" selected>9600</option>
|
| 242 |
+
<option value="19200">19200</option>
|
| 243 |
+
<option value="38400">38400</option>
|
| 244 |
+
<option value="57600">57600</option>
|
| 245 |
+
<option value="115200">115200</option>
|
| 246 |
+
</select>
|
| 247 |
+
<div class="button-group">
|
| 248 |
+
<button id="connect">接続</button>
|
| 249 |
+
<button id="disconnect" disabled>切断</button>
|
| 250 |
+
<button id="clear">クリア</button>
|
| 251 |
+
<button id="export">エクスポート</button>
|
| 252 |
+
</div>
|
| 253 |
+
<div class="history" id="history">
|
| 254 |
+
<div class="history-item" style="color: var(--placeholder-color);">送信履歴なし</div>
|
| 255 |
+
</div>
|
| 256 |
+
</div>
|
| 257 |
+
<div class="main">
|
| 258 |
+
<input type="text" id="input" placeholder="Enterで送信" disabled aria-label="コマンド入力">
|
| 259 |
+
<div style="position: relative;">
|
| 260 |
+
<textarea id="output" readonly placeholder="受信データが表示されます..." aria-label="受信データ"></textarea>
|
| 261 |
+
<div class="copy-button" id="copyButton" title="受信データをコピー">
|
| 262 |
+
<img src="copy2.png" alt="コピーアイコン">
|
| 263 |
+
</div>
|
| 264 |
+
</div>
|
| 265 |
+
</div>
|
| 266 |
+
</div>
|
| 267 |
+
|
| 268 |
+
<script>
|
| 269 |
+
// デバッグ用ログ
|
| 270 |
+
console.log('スクリプトの読み込み開始');
|
| 271 |
+
|
| 272 |
+
let port, writer, reader;
|
| 273 |
+
let keepReading = false;
|
| 274 |
+
let isConnected = false;
|
| 275 |
+
let commandHistory = []; // セッション内でのみ保持
|
| 276 |
+
let receiveBuffer = ''; // 受信データバッファ
|
| 277 |
+
|
| 278 |
+
// DOM要素の取得
|
| 279 |
+
const connectButton = document.getElementById('connect');
|
| 280 |
+
const disconnectButton = document.getElementById('disconnect');
|
| 281 |
+
const clearButton = document.getElementById('clear');
|
| 282 |
+
const exportButton = document.getElementById('export');
|
| 283 |
+
const copyButton = document.getElementById('copyButton');
|
| 284 |
+
const inputField = document.getElementById('input');
|
| 285 |
+
const outputArea = document.getElementById('output');
|
| 286 |
+
const statusDisplay = document.getElementById('status');
|
| 287 |
+
const baudRateSelect = document.getElementById('baudRate');
|
| 288 |
+
const historyContainer = document.getElementById('history');
|
| 289 |
+
|
| 290 |
+
// DOM要素の存在確認
|
| 291 |
+
console.log('DOM要素取得:', {
|
| 292 |
+
connectButton, disconnectButton, clearButton, exportButton,
|
| 293 |
+
copyButton, inputField, outputArea, statusDisplay, baudRateSelect, historyContainer
|
| 294 |
+
});
|
| 295 |
+
|
| 296 |
+
// 初期ロード時に履歴を反映
|
| 297 |
+
updateHistoryUI();
|
| 298 |
+
|
| 299 |
+
connectButton.addEventListener('click', async () => {
|
| 300 |
+
console.log('接続ボタンクリック');
|
| 301 |
+
try {
|
| 302 |
+
port = await navigator.serial.requestPort();
|
| 303 |
+
await port.open({ baudRate: parseInt(baudRateSelect.value) });
|
| 304 |
+
|
| 305 |
+
writer = port.writable.getWriter();
|
| 306 |
+
reader = port.readable.getReader();
|
| 307 |
+
keepReading = true;
|
| 308 |
+
isConnected = true;
|
| 309 |
+
|
| 310 |
+
updateUI(true);
|
| 311 |
+
statusDisplay.textContent = `接続済み (ボーレート: ${baudRateSelect.value})`;
|
| 312 |
+
statusDisplay.classList.add('connected');
|
| 313 |
+
statusDisplay.classList.remove('error');
|
| 314 |
+
|
| 315 |
+
const decoder = new TextDecoder();
|
| 316 |
+
|
| 317 |
+
while (keepReading) {
|
| 318 |
+
const { value, done } = await reader.read();
|
| 319 |
+
if (done) break;
|
| 320 |
+
if (value) {
|
| 321 |
+
const text = decoder.decode(value, { stream: true });
|
| 322 |
+
console.log('受信データ(生):', text);
|
| 323 |
+
displayFilteredOutput(text);
|
| 324 |
+
}
|
| 325 |
+
}
|
| 326 |
+
} catch (err) {
|
| 327 |
+
console.error('接続エラー:', err);
|
| 328 |
+
statusDisplay.textContent = `接続エラー: ${err.message}`;
|
| 329 |
+
statusDisplay.classList.add('error');
|
| 330 |
+
statusDisplay.classList.remove('connected');
|
| 331 |
+
updateUI(false);
|
| 332 |
+
}
|
| 333 |
+
});
|
| 334 |
+
|
| 335 |
+
disconnectButton.addEventListener('click', async () => {
|
| 336 |
+
console.log('切断ボタンクリック');
|
| 337 |
+
try {
|
| 338 |
+
keepReading = false;
|
| 339 |
+
if (reader) await reader.releaseLock();
|
| 340 |
+
if (writer) await writer.releaseLock();
|
| 341 |
+
if (port) await port.close();
|
| 342 |
+
isConnected = false;
|
| 343 |
+
updateUI(false);
|
| 344 |
+
statusDisplay.textContent = '未接続';
|
| 345 |
+
statusDisplay.classList.remove('connected', 'error');
|
| 346 |
+
receiveBuffer = ''; // バッファをクリア
|
| 347 |
+
} catch (err) {
|
| 348 |
+
console.error('切断エラー:', err);
|
| 349 |
+
statusDisplay.textContent = `切断エラー: ${err.message}`;
|
| 350 |
+
statusDisplay.classList.add('error');
|
| 351 |
+
}
|
| 352 |
+
});
|
| 353 |
+
|
| 354 |
+
clearButton.addEventListener('click', () => {
|
| 355 |
+
console.log('クリアボタンク���ック');
|
| 356 |
+
outputArea.value = '';
|
| 357 |
+
receiveBuffer = ''; // バッファをクリア
|
| 358 |
+
console.log('受信データクリア成功');
|
| 359 |
+
});
|
| 360 |
+
|
| 361 |
+
exportButton.addEventListener('click', () => {
|
| 362 |
+
console.log('エクスポートボタンクリック');
|
| 363 |
+
try {
|
| 364 |
+
if (outputArea.value.trim().length === 0) {
|
| 365 |
+
statusDisplay.textContent = 'エクスポートするデータがありません';
|
| 366 |
+
statusDisplay.classList.add('error');
|
| 367 |
+
setTimeout(() => {
|
| 368 |
+
if (isConnected) {
|
| 369 |
+
statusDisplay.textContent = `接続済み (ボーレート: ${baudRateSelect.value})`;
|
| 370 |
+
statusDisplay.classList.add('connected');
|
| 371 |
+
statusDisplay.classList.remove('error');
|
| 372 |
+
} else {
|
| 373 |
+
statusDisplay.textContent = '未接続';
|
| 374 |
+
statusDisplay.classList.remove('error');
|
| 375 |
+
}
|
| 376 |
+
}, 2000);
|
| 377 |
+
return;
|
| 378 |
+
}
|
| 379 |
+
const blob = new Blob([outputArea.value], { type: 'text/plain' });
|
| 380 |
+
const url = URL.createObjectURL(blob);
|
| 381 |
+
const a = document.createElement('a');
|
| 382 |
+
a.href = url;
|
| 383 |
+
a.download = `serial_data_${new Date().toISOString()}.txt`;
|
| 384 |
+
a.click();
|
| 385 |
+
URL.revokeObjectURL(url);
|
| 386 |
+
console.log('エクスポート成功');
|
| 387 |
+
} catch (e) {
|
| 388 |
+
console.error('エクスポートエラー:', e);
|
| 389 |
+
statusDisplay.textContent = `エクスポートエラー: ${e.message}`;
|
| 390 |
+
statusDisplay.classList.add('error');
|
| 391 |
+
}
|
| 392 |
+
});
|
| 393 |
+
|
| 394 |
+
copyButton.addEventListener('click', async () => {
|
| 395 |
+
console.log('コピーボタンクリック');
|
| 396 |
+
try {
|
| 397 |
+
await navigator.clipboard.writeText(outputArea.value);
|
| 398 |
+
statusDisplay.textContent = '受信データをコピーしました';
|
| 399 |
+
statusDisplay.classList.add('connected');
|
| 400 |
+
statusDisplay.classList.remove('error');
|
| 401 |
+
console.log('コピー成功');
|
| 402 |
+
setTimeout(() => {
|
| 403 |
+
if (isConnected) {
|
| 404 |
+
statusDisplay.textContent = `接続済み (ボーレート: ${baudRateSelect.value})`;
|
| 405 |
+
} else {
|
| 406 |
+
statusDisplay.textContent = '未接続';
|
| 407 |
+
statusDisplay.classList.remove('connected');
|
| 408 |
+
}
|
| 409 |
+
}, 2000);
|
| 410 |
+
} catch (err) {
|
| 411 |
+
console.error('コピーエラー:', err);
|
| 412 |
+
statusDisplay.textContent = `コピーエラー: ${err.message}`;
|
| 413 |
+
statusDisplay.classList.add('error');
|
| 414 |
+
}
|
| 415 |
+
});
|
| 416 |
+
|
| 417 |
+
inputField.addEventListener('keydown', async (e) => {
|
| 418 |
+
if (e.key === 'Enter') {
|
| 419 |
+
console.log('Enterキー押下:', inputField.value);
|
| 420 |
+
e.preventDefault();
|
| 421 |
+
const text = inputField.value.trim();
|
| 422 |
+
if (text && writer) {
|
| 423 |
+
try {
|
| 424 |
+
const encoder = new TextEncoder();
|
| 425 |
+
await writer.write(encoder.encode(text + '\n'));
|
| 426 |
+
updateCommandHistory(text);
|
| 427 |
+
inputField.value = '';
|
| 428 |
+
} catch (e) {
|
| 429 |
+
console.error('送信エラー:', e);
|
| 430 |
+
statusDisplay.textContent = `送信エラー: ${e.message}`;
|
| 431 |
+
statusDisplay.classList.add('error');
|
| 432 |
+
}
|
| 433 |
+
}
|
| 434 |
+
}
|
| 435 |
+
});
|
| 436 |
+
|
| 437 |
+
historyContainer.addEventListener('click', async (e) => {
|
| 438 |
+
if (e.target.classList.contains('history-item') && isConnected) {
|
| 439 |
+
console.log('履歴アイテムクリック:', e.target.textContent);
|
| 440 |
+
const text = e.target.textContent;
|
| 441 |
+
if (text && writer) {
|
| 442 |
+
try {
|
| 443 |
+
const encoder = new TextEncoder();
|
| 444 |
+
await writer.write(encoder.encode(text + '\n'));
|
| 445 |
+
console.log('履歴から送信成功:', text);
|
| 446 |
+
updateCommandHistory(text); // 履歴を再追加(最新を先頭に)
|
| 447 |
+
} catch (e) {
|
| 448 |
+
console.error('履歴送信エラー:', e);
|
| 449 |
+
statusDisplay.textContent = `送信エラー: ${e.message}`;
|
| 450 |
+
statusDisplay.classList.add('error');
|
| 451 |
+
}
|
| 452 |
+
}
|
| 453 |
+
}
|
| 454 |
+
});
|
| 455 |
+
|
| 456 |
+
function updateCommandHistory(text) {
|
| 457 |
+
if (!commandHistory.includes(text)) {
|
| 458 |
+
commandHistory.unshift(text); // 最新を先頭に追加
|
| 459 |
+
} else {
|
| 460 |
+
// 既存のコマンドを先頭に移動
|
| 461 |
+
commandHistory = commandHistory.filter(cmd => cmd !== text);
|
| 462 |
+
commandHistory.unshift(text);
|
| 463 |
+
}
|
| 464 |
+
// 直近3件に制限
|
| 465 |
+
if (commandHistory.length > 3) {
|
| 466 |
+
commandHistory = commandHistory.slice(0, 3);
|
| 467 |
+
}
|
| 468 |
+
console.log('履歴更新:', commandHistory);
|
| 469 |
+
updateHistoryUI();
|
| 470 |
+
}
|
| 471 |
+
|
| 472 |
+
function displayFilteredOutput(rawText) {
|
| 473 |
+
// 受信データをバッファに追加
|
| 474 |
+
receiveBuffer += rawText;
|
| 475 |
+
console.log('バッファ更新:', receiveBuffer);
|
| 476 |
+
|
| 477 |
+
// バッファを改行で分割
|
| 478 |
+
const lines = receiveBuffer.split(/\r?\n/);
|
| 479 |
+
// 最後の要素が不完全な行(改行で終わっていない)の場合、バッファに残す
|
| 480 |
+
if (!rawText.endsWith('\n')) {
|
| 481 |
+
receiveBuffer = lines.pop() || '';
|
| 482 |
+
} else {
|
| 483 |
+
receiveBuffer = '';
|
| 484 |
+
}
|
| 485 |
+
|
| 486 |
+
// 空行(空白のみ)を除外し、表示
|
| 487 |
+
const filteredLines = lines.filter(line => line.trim().length > 0);
|
| 488 |
+
if (filteredLines.length > 0) {
|
| 489 |
+
if (outputArea.value) {
|
| 490 |
+
outputArea.value += '\n' + filteredLines.join('\n');
|
| 491 |
+
} else {
|
| 492 |
+
outputArea.value += filteredLines.join('\n');
|
| 493 |
+
}
|
| 494 |
+
outputArea.scrollTop = outputArea.scrollHeight;
|
| 495 |
+
}
|
| 496 |
+
console.log('表示データ:', filteredLines);
|
| 497 |
+
}
|
| 498 |
+
|
| 499 |
+
function updateHistoryUI() {
|
| 500 |
+
console.log('履歴UI更新');
|
| 501 |
+
historyContainer.innerHTML = '';
|
| 502 |
+
if (commandHistory.length === 0) {
|
| 503 |
+
historyContainer.innerHTML = '<div class="history-item" style="color: var(--placeholder-color);">送信履歴なし</div>';
|
| 504 |
+
} else {
|
| 505 |
+
commandHistory.forEach(cmd => {
|
| 506 |
+
const div = document.createElement('div');
|
| 507 |
+
div.className = 'history-item';
|
| 508 |
+
div.textContent = cmd;
|
| 509 |
+
historyContainer.appendChild(div);
|
| 510 |
+
});
|
| 511 |
+
}
|
| 512 |
+
}
|
| 513 |
+
|
| 514 |
+
function updateUI(connected) {
|
| 515 |
+
console.log('UI更新:', connected);
|
| 516 |
+
connectButton.disabled = connected;
|
| 517 |
+
disconnectButton.disabled = !connected;
|
| 518 |
+
// clearButtonとexportButtonは常に有効
|
| 519 |
+
inputField.disabled = !connected;
|
| 520 |
+
baudRateSelect.disabled = connected;
|
| 521 |
+
// copyButtonは常に有効
|
| 522 |
+
}
|
| 523 |
+
|
| 524 |
+
window.addEventListener('beforeunload', async () => {
|
| 525 |
+
console.log('ページ終了処理');
|
| 526 |
+
keepReading = false;
|
| 527 |
+
if (reader) await reader.releaseLock();
|
| 528 |
+
if (writer) await writer.releaseLock();
|
| 529 |
+
if (port) await port.close();
|
| 530 |
+
receiveBuffer = ''; // バッファをクリア
|
| 531 |
+
});
|
| 532 |
+
|
| 533 |
+
console.log('スクリプトの読み込み完了');
|
| 534 |
+
</script>
|
| 535 |
+
</body>
|
| 536 |
+
</html>
|