1045 lines
44 KiB
C++
1045 lines
44 KiB
C++
#ifndef SERIAL2_TERMINAL_H
|
||
#define SERIAL2_TERMINAL_H
|
||
const char serial2_terminal_html[] PROGMEM = R"rawliteral(
|
||
<!DOCTYPE html>
|
||
<html>
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>Serial2 Terminal - CAN Logger</title>
|
||
<style>
|
||
:root {
|
||
--bg: #0e1117;
|
||
--panel: #161b24;
|
||
--card: #1c2230;
|
||
--border: #2d3748;
|
||
--accent: #43cea2;
|
||
--green: #3fb950;
|
||
--blue: #58a6ff;
|
||
--red: #f85149;
|
||
--yellow: #e3b341;
|
||
--purple: #bc8cff;
|
||
--text: #e6edf3;
|
||
--muted: #8b949e;
|
||
--r: 8px;
|
||
}
|
||
* { margin:0; padding:0; box-sizing:border-box; }
|
||
html, body { min-height:100%; }
|
||
body {
|
||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
||
background: var(--bg); color: var(--text);
|
||
overflow-x: hidden; font-size: 14px;
|
||
}
|
||
|
||
/* Header */
|
||
.header {
|
||
background: linear-gradient(135deg,#1a2744 0%,#1e1a3a 100%);
|
||
padding: 11px 16px; border-bottom: 1px solid var(--border);
|
||
display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
|
||
}
|
||
.header h1 {
|
||
font-size: 1.0em; font-weight: 700; color: var(--accent);
|
||
display: flex; align-items: center; gap: 8px;
|
||
}
|
||
.header p { font-size: 0.78em; color: var(--muted); margin:0; }
|
||
.badge {
|
||
background: rgba(67,206,162,.15); color: var(--accent);
|
||
padding: 2px 8px; border-radius: 12px; font-size: 0.65em;
|
||
font-weight: 600; border: 1px solid rgba(67,206,162,.3);
|
||
white-space: nowrap;
|
||
}
|
||
.header-spacer { flex:1; }
|
||
|
||
/* Nav */
|
||
.nav {
|
||
background: var(--panel); border-bottom: 1px solid var(--border);
|
||
display: flex; overflow-x: auto; -webkit-overflow-scrolling: touch;
|
||
scrollbar-width: none;
|
||
}
|
||
.nav::-webkit-scrollbar { display:none; }
|
||
.nav a {
|
||
display: inline-flex; align-items: center;
|
||
padding: 10px 13px; text-decoration: none; color: var(--muted);
|
||
font-size: 0.78em; font-weight: 500;
|
||
border-bottom: 2px solid transparent; white-space: nowrap;
|
||
transition: color .2s, border-color .2s;
|
||
}
|
||
.nav a:hover { color: var(--text); }
|
||
.nav a.active { color: var(--accent); border-bottom-color: var(--accent); }
|
||
|
||
/* Content */
|
||
.content { padding: 12px; }
|
||
|
||
/* Headings */
|
||
h2 {
|
||
color: var(--accent); margin: 14px 0 10px;
|
||
font-size: 0.82em; font-weight: 700; text-transform: uppercase;
|
||
letter-spacing: .5px; padding-bottom: 6px;
|
||
border-bottom: 1px solid var(--border);
|
||
}
|
||
h3 {
|
||
color: var(--text); font-size: 0.85em; font-weight: 600;
|
||
margin-bottom: 10px; padding-bottom: 6px;
|
||
border-bottom: 1px solid var(--border);
|
||
}
|
||
|
||
/* Buttons */
|
||
.btn, button {
|
||
padding: 6px 13px; border: 1px solid var(--border);
|
||
border-radius: var(--r); background: var(--bg); color: var(--muted);
|
||
font-size: 0.8em; font-weight: 600; cursor: pointer;
|
||
font-family: inherit; transition: all .15s; white-space: nowrap;
|
||
-webkit-tap-highlight-color: transparent; touch-action: manipulation;
|
||
}
|
||
.btn:hover, button:hover { border-color: var(--accent); color: var(--accent); }
|
||
.btn:active, button:active { transform: scale(.97); }
|
||
.btn-primary { border-color:var(--blue); color:var(--blue); }
|
||
.btn-success { border-color:var(--accent); color:var(--accent); }
|
||
.btn-danger { border-color:var(--red); color:var(--red); }
|
||
.btn-warning { border-color:var(--yellow); color:var(--yellow); }
|
||
.btn-secondary{ border-color:var(--muted); color:var(--muted); }
|
||
.btn-primary:hover { background:rgba(88,166,255,.10); }
|
||
.btn-success:hover { background:rgba(67,206,162,.10); }
|
||
.btn-danger:hover { background:rgba(248,81,73,.10); }
|
||
.btn-warning:hover { background:rgba(227,179,65,.10); }
|
||
.btn-secondary:hover{ background:rgba(139,148,158,.10); }
|
||
.btn-small { padding:4px 9px; font-size:.75em; }
|
||
.button-group, .btn-group { display:flex; gap:6px; flex-wrap:wrap; margin-top:10px; }
|
||
|
||
/* Forms */
|
||
label {
|
||
display:block; font-weight:600; color:var(--muted);
|
||
margin-bottom:4px; font-size:.78em;
|
||
text-transform:uppercase; letter-spacing:.3px;
|
||
}
|
||
input[type="text"], input[type="password"], input[type="number"],
|
||
select, textarea {
|
||
width:100%; padding:7px 10px;
|
||
border:1px solid var(--border); border-radius:var(--r);
|
||
font-size:.85em; font-family:inherit;
|
||
background:var(--bg); color:var(--text);
|
||
transition:border-color .2s;
|
||
}
|
||
input:focus, select:focus, textarea:focus {
|
||
outline:none; border-color:var(--accent);
|
||
box-shadow:0 0 0 2px rgba(67,206,162,.10);
|
||
}
|
||
input[type="checkbox"], input[type="radio"] {
|
||
width:15px; height:15px; cursor:pointer; accent-color:var(--accent);
|
||
}
|
||
select option { background:var(--panel); color:var(--text); }
|
||
|
||
/* Panels */
|
||
.section, .settings-section, .send-panel, .stats,
|
||
.control-panel, .auto-trigger-section {
|
||
background:var(--panel); border:1px solid var(--border);
|
||
border-radius:var(--r); padding:13px; margin-bottom:10px;
|
||
}
|
||
.form-group { margin-bottom:12px; }
|
||
.form-group:last-child { margin-bottom:0; }
|
||
.help-text { font-size:.76em; color:var(--muted); margin-top:4px; line-height:1.4; }
|
||
.form-row {
|
||
display:grid;
|
||
grid-template-columns:repeat(auto-fit,minmax(min(100%,190px),1fr));
|
||
gap:10px; margin-bottom:10px;
|
||
}
|
||
.form-row label { text-transform:none; font-size:.78em; }
|
||
.checkbox-group, .checkbox-row {
|
||
display:flex; align-items:center; gap:8px;
|
||
margin-bottom:8px; padding:7px 10px;
|
||
background:var(--card); border-radius:6px; border:1px solid var(--border);
|
||
}
|
||
.checkbox-group label, .checkbox-row label {
|
||
text-transform:none; cursor:pointer; margin-bottom:0;
|
||
color:var(--text); font-size:.85em; letter-spacing:0;
|
||
}
|
||
|
||
/* Alerts */
|
||
.alert {
|
||
padding:9px 13px; border-radius:var(--r);
|
||
margin-bottom:10px; display:none;
|
||
align-items:center; gap:9px;
|
||
font-size:.83em; font-weight:600;
|
||
border:1px solid var(--border); background:var(--panel); color:var(--text);
|
||
}
|
||
.alert.show { display:flex; }
|
||
.alert-success { border-color:rgba(67,206,162,.4); color:var(--accent); background:rgba(67,206,162,.07); }
|
||
.alert-info { border-color:rgba(88,166,255,.4); color:var(--blue); background:rgba(88,166,255,.07); }
|
||
.alert-warning { border-color:rgba(227,179,65,.4); color:var(--yellow); background:rgba(227,179,65,.07); }
|
||
.alert-icon { font-size:1.15em; }
|
||
.alert-text { flex:1; }
|
||
@keyframes slideDown { from{opacity:0;transform:translateY(-6px);}to{opacity:1;transform:translateY(0);} }
|
||
|
||
/* Connection status */
|
||
.connection-status {
|
||
position:fixed; top:10px; right:10px;
|
||
padding:4px 11px; border-radius:20px;
|
||
font-size:.75em; font-weight:600; z-index:1000;
|
||
border:1px solid var(--border); background:var(--panel); color:var(--muted);
|
||
}
|
||
.connection-status.connected { border-color:rgba(67,206,162,.5); color:var(--accent); background:rgba(67,206,162,.08); }
|
||
.connection-status.disconnected { border-color:rgba(248,81,73,.5); color:var(--red); background:rgba(248,81,73,.08); }
|
||
|
||
@media (max-width:480px) { .content{padding:8px;} h2{font-size:.78em;} }
|
||
|
||
/* serial terminal specific */
|
||
.control-panel {
|
||
background:var(--panel); border:1px solid var(--border);
|
||
border-radius:var(--r); padding:13px; margin-bottom:10px;
|
||
}
|
||
.control-group {
|
||
background:var(--card); border:1px solid var(--border);
|
||
border-radius:var(--r); padding:12px;
|
||
display:grid; grid-template-columns:1fr 1fr; gap:16px;
|
||
}
|
||
@media(max-width:600px){ .control-group{ grid-template-columns:1fr; } }
|
||
.control-group.full-width { grid-template-columns:1fr; }
|
||
.control-group h3 { grid-column:1/-1; color:var(--accent); font-size:.85em; border-color:var(--border); }
|
||
.control-section { display:flex; flex-direction:column; }
|
||
.form-row { grid-template-columns:110px 1fr; align-items:center; gap:8px; }
|
||
.form-row label { text-transform:none; color:var(--muted); font-size:.80em; margin-bottom:0; }
|
||
/* Terminal */
|
||
.terminal-container {
|
||
background:#090d13; border:1px solid var(--border); border-radius:var(--r); overflow:hidden;
|
||
}
|
||
.terminal-header {
|
||
background:var(--panel); padding:8px 12px;
|
||
display:flex; justify-content:space-between; align-items:center;
|
||
border-bottom:1px solid var(--border);
|
||
}
|
||
.terminal-title { color:var(--muted); font-size:.78em; font-weight:500; }
|
||
.terminal-controls { display:flex; gap:6px; }
|
||
.terminal-btn {
|
||
padding:3px 10px; border:1px solid var(--border); border-radius:4px;
|
||
font-size:.75em; cursor:pointer; background:var(--bg); color:var(--muted);
|
||
transition:all .15s; font-family:inherit;
|
||
}
|
||
.terminal-btn:hover { border-color:var(--accent); color:var(--accent); }
|
||
.terminal {
|
||
height:380px; overflow-y:auto; padding:12px;
|
||
font-family:'SF Mono','Fira Code','Courier New',monospace;
|
||
font-size:12px; line-height:1.55; color:#c9d1d9; background:#090d13;
|
||
}
|
||
.terminal.teraterm-mode { height:330px; }
|
||
.terminal::-webkit-scrollbar { width:5px; }
|
||
.terminal::-webkit-scrollbar-track { background:#0d1117; }
|
||
.terminal::-webkit-scrollbar-thumb { background:#30363d; border-radius:3px; }
|
||
.terminal::-webkit-scrollbar-thumb:hover { background:#484f58; }
|
||
.terminal-input-area {
|
||
display:none; background:var(--panel); padding:8px 12px;
|
||
border-top:1px solid var(--border);
|
||
}
|
||
.terminal-input-area.active { display:flex; align-items:center; gap:8px; }
|
||
.terminal-input-area .prompt { color:var(--accent); font-family:monospace; font-size:.82em; }
|
||
.terminal-input-area input {
|
||
flex:1; background:var(--bg); border:1px solid var(--border);
|
||
color:var(--text); padding:5px 8px;
|
||
font-family:monospace; font-size:.82em; border-radius:4px;
|
||
}
|
||
.terminal-input-area input:focus { outline:none; border-color:var(--accent); }
|
||
/* Log line colors */
|
||
.line { margin-bottom:3px; font-family:monospace; }
|
||
.timestamp { color:#484f58; margin-right:8px; }
|
||
.rx { color:#79c0ff; }
|
||
.tx { color:#7ee787; }
|
||
/* Modbus */
|
||
.modbus-frame {
|
||
display:inline-block; padding:4px 8px; margin:3px 0;
|
||
background:var(--card); border-radius:4px; border-left:2px solid var(--blue);
|
||
}
|
||
.modbus-addr { color:var(--yellow); font-weight:bold; }
|
||
.modbus-func { color:var(--accent); font-weight:bold; }
|
||
.modbus-data { color:#79c0ff; }
|
||
.modbus-crc { color:#ffa657; }
|
||
.modbus-label { color:var(--muted); font-size:.85em; margin-left:4px; }
|
||
.modbus-error { color:var(--red); }
|
||
/* Send / Stats */
|
||
.send-panel { margin-top:10px; }
|
||
.send-panel h3, .stats h3 { color:var(--accent); border-color:var(--border); }
|
||
.send-controls { display:flex; gap:8px; align-items:center; }
|
||
.send-controls input { flex:1; }
|
||
.stats { margin-top:10px; }
|
||
.stat-grid {
|
||
display:grid;
|
||
grid-template-columns:repeat(auto-fit,minmax(min(100%,120px),1fr));
|
||
gap:8px;
|
||
}
|
||
.stat-item {
|
||
text-align:center; padding:11px; background:var(--card);
|
||
border-radius:var(--r); border:1px solid var(--border);
|
||
}
|
||
.stat-value { font-size:1.4em; font-weight:bold; color:var(--accent); margin-bottom:3px; font-family:monospace; }
|
||
.stat-label { font-size:.70em; color:var(--muted); text-transform:uppercase; letter-spacing:.5px; }
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div class="header">
|
||
<h1>
|
||
📟 Serial2 Terminal
|
||
<span class="badge">GPIO 6/7</span>
|
||
</h1>
|
||
</div>
|
||
|
||
<div class="nav">
|
||
<a href="/">📊 Monitor</a>
|
||
<a href="/transmit">📤 Transmit</a>
|
||
<a href="/graph">📈 Graph</a>
|
||
<a href="/graph-view">📊 Graph View</a>
|
||
<a href="/settings">⚙️ Settings</a>
|
||
<a href="/serial">📟 Serial1</a>
|
||
<a href="/serial2" class="active">📟 Serial2</a>
|
||
</div>
|
||
|
||
<div class="content">
|
||
<div class="control-panel">
|
||
<div class="control-group">
|
||
<div class="control-section">
|
||
<h3>⚙️ Serial Settings</h3>
|
||
<div class="form-row">
|
||
<label>Baud Rate:</label>
|
||
<select id="baudRate">
|
||
<option value="9600">9600</option>
|
||
<option value="19200">19200</option>
|
||
<option value="38400">38400</option>
|
||
<option value="57600">57600</option>
|
||
<option value="115200" selected>115200</option>
|
||
<option value="230400">230400</option>
|
||
<option value="460800">460800</option>
|
||
<option value="921600">921600</option>
|
||
</select>
|
||
</div>
|
||
<div class="form-row">
|
||
<label>Display Format:</label>
|
||
<select id="displayFormat" onchange="updateDisplayMode()">
|
||
<option value="hex">HEX</option>
|
||
<option value="ascii">ASCII</option>
|
||
<option value="both" selected>HEX + ASCII</option>
|
||
<option value="teraterm">TeraTerm Style</option>
|
||
</select>
|
||
</div>
|
||
<div class="checkbox-row">
|
||
<input type="checkbox" id="modbusParseEnabled">
|
||
<label for="modbusParseEnabled">🔍 Modbus RTU 파싱</label>
|
||
</div>
|
||
<div id="teratermOptions" style="display: none; margin-top: 10px;">
|
||
<div class="form-row">
|
||
<label>Line Ending:</label>
|
||
<select id="lineEnding">
|
||
<option value="none">None</option>
|
||
<option value="cr">CR</option>
|
||
<option value="lf">LF</option>
|
||
<option value="crlf" selected>CR+LF</option>
|
||
</select>
|
||
</div>
|
||
<div class="checkbox-row">
|
||
<input type="checkbox" id="localEcho" checked>
|
||
<label for="localEcho">Local Echo</label>
|
||
</div>
|
||
</div>
|
||
<div class="btn-group">
|
||
<button class="btn btn-primary" onclick="applySettings()">Apply Settings</button>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="control-section">
|
||
<h3>📝 Logging Control</h3>
|
||
<div class="form-row">
|
||
<label>Status:</label>
|
||
<span id="logStatus" style="color: #999;">Not Logging</span>
|
||
</div>
|
||
<div class="form-row">
|
||
<label>File:</label>
|
||
<span id="currentFile" style="color: #666; font-size: 12px;">-</span>
|
||
</div>
|
||
<div class="btn-group">
|
||
<button class="btn btn-success" onclick="startLogging()">Start Logging</button>
|
||
<button class="btn btn-danger" onclick="stopLogging()">Stop Logging</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="terminal-container">
|
||
<div class="terminal-header">
|
||
<div class="terminal-title">📡 Serial Monitor (RX/TX)</div>
|
||
<div class="terminal-controls">
|
||
<button class="terminal-btn" onclick="clearTerminal()">Clear</button>
|
||
<button class="terminal-btn" onclick="toggleAutoScroll()">
|
||
<span id="autoScrollText">Auto-scroll: ON</span>
|
||
</button>
|
||
</div>
|
||
</div>
|
||
<div class="terminal" id="terminal"></div>
|
||
<div class="terminal-input-area" id="terminalInput">
|
||
<span class="prompt">></span>
|
||
<input type="text" id="terminalInputField" placeholder="Type here and press Enter..." />
|
||
</div>
|
||
</div>
|
||
|
||
<div class="send-panel">
|
||
<h3>📤 Send Data</h3>
|
||
<div class="send-controls">
|
||
<input type="text" id="sendInput" placeholder="Enter HEX data (e.g., 01 03 00 00 00 0A)" />
|
||
<button class="btn btn-primary" onclick="sendData()">Send</button>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="stats">
|
||
<h3>📊 Statistics</h3>
|
||
<div class="stat-grid">
|
||
<div class="stat-item">
|
||
<div class="stat-value" id="rxCount">0</div>
|
||
<div class="stat-label">RX Bytes</div>
|
||
</div>
|
||
<div class="stat-item">
|
||
<div class="stat-value" id="txCount">0</div>
|
||
<div class="stat-label">TX Bytes</div>
|
||
</div>
|
||
<div class="stat-item">
|
||
<div class="stat-value" id="frameCount">0</div>
|
||
<div class="stat-label">Frames</div>
|
||
</div>
|
||
<div class="stat-item">
|
||
<div class="stat-value" id="modbusCount">0</div>
|
||
<div class="stat-label">Modbus Frames</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<script>
|
||
let ws;
|
||
let autoScroll = true;
|
||
let rxCount = 0;
|
||
let txCount = 0;
|
||
let frameCount = 0;
|
||
let modbusCount = 0;
|
||
let modbusParseEnabled = false;
|
||
let displayFormat = 'both';
|
||
let teratermMode = false;
|
||
let localEcho = true;
|
||
let lineEnding = 'crlf';
|
||
|
||
// localStorage 키
|
||
const STORAGE_KEY = 'serial2_settings';
|
||
|
||
// 설정 저장
|
||
function saveSettings() {
|
||
const settings = {
|
||
baudRate: document.getElementById('baudRate').value,
|
||
displayFormat: document.getElementById('displayFormat').value,
|
||
modbusParseEnabled: document.getElementById('modbusParseEnabled').checked,
|
||
lineEnding: document.getElementById('lineEnding').value,
|
||
localEcho: document.getElementById('localEcho').checked
|
||
};
|
||
localStorage.setItem(STORAGE_KEY, JSON.stringify(settings));
|
||
console.log('Settings saved:', settings);
|
||
}
|
||
|
||
// 설정 로드
|
||
function loadSettings() {
|
||
try {
|
||
const saved = localStorage.getItem(STORAGE_KEY);
|
||
if (saved) {
|
||
const settings = JSON.parse(saved);
|
||
console.log('Settings loaded:', settings);
|
||
|
||
// UI에 적용
|
||
if (settings.baudRate) {
|
||
document.getElementById('baudRate').value = settings.baudRate;
|
||
}
|
||
if (settings.displayFormat) {
|
||
document.getElementById('displayFormat').value = settings.displayFormat;
|
||
displayFormat = settings.displayFormat;
|
||
}
|
||
if (settings.modbusParseEnabled !== undefined) {
|
||
document.getElementById('modbusParseEnabled').checked = settings.modbusParseEnabled;
|
||
modbusParseEnabled = settings.modbusParseEnabled;
|
||
}
|
||
if (settings.lineEnding) {
|
||
document.getElementById('lineEnding').value = settings.lineEnding;
|
||
lineEnding = settings.lineEnding;
|
||
}
|
||
if (settings.localEcho !== undefined) {
|
||
document.getElementById('localEcho').checked = settings.localEcho;
|
||
localEcho = settings.localEcho;
|
||
}
|
||
|
||
// 디스플레이 모드 업데이트
|
||
updateDisplayMode();
|
||
}
|
||
} catch (e) {
|
||
console.error('Failed to load settings:', e);
|
||
}
|
||
}
|
||
|
||
// Modbus Function Code 매핑
|
||
const MODBUS_FUNCTIONS = {
|
||
0x01: 'Read Coils',
|
||
0x02: 'Read Discrete Inputs',
|
||
0x03: 'Read Holding Registers',
|
||
0x04: 'Read Input Registers',
|
||
0x05: 'Write Single Coil',
|
||
0x06: 'Write Single Register',
|
||
0x0F: 'Write Multiple Coils',
|
||
0x10: 'Write Multiple Registers',
|
||
0x14: 'Read File Record',
|
||
0x15: 'Write File Record',
|
||
0x16: 'Mask Write Register',
|
||
0x17: 'Read/Write Multiple Registers',
|
||
0x2B: 'Read Device Identification'
|
||
};
|
||
|
||
function syncTimeFromPhone() {
|
||
const now = new Date();
|
||
const timeData = {
|
||
cmd: 'syncTimeFromPhone',
|
||
year: now.getFullYear(),
|
||
month: now.getMonth() + 1,
|
||
day: now.getDate(),
|
||
hour: now.getHours(),
|
||
minute: now.getMinutes(),
|
||
second: now.getSeconds()
|
||
};
|
||
|
||
if (ws && ws.readyState === WebSocket.OPEN) {
|
||
ws.send(JSON.stringify(timeData));
|
||
console.log('Time sync command sent:', timeData);
|
||
}
|
||
}
|
||
|
||
function initWebSocket() {
|
||
ws = new WebSocket('ws://' + window.location.hostname + ':81/');
|
||
|
||
ws.onopen = function() {
|
||
console.log('WebSocket Connected');
|
||
addToTerminal('System', '🟢 Connected to Serial2 Terminal');
|
||
|
||
// 시간 동기화
|
||
setTimeout(function() {
|
||
syncTimeFromPhone();
|
||
}, 500);
|
||
};
|
||
|
||
ws.onmessage = function(event) {
|
||
try {
|
||
const data = JSON.parse(event.data);
|
||
|
||
if (data.type === 'serial2') {
|
||
handleSerialData(data);
|
||
} else if (data.type === 'serial2Status' || data.type === 'update') {
|
||
updateStatus(data);
|
||
}
|
||
} catch (e) {
|
||
console.error('Parse error:', e);
|
||
}
|
||
};
|
||
|
||
ws.onclose = function() {
|
||
console.log('WebSocket Disconnected');
|
||
addToTerminal('System', '🔴 Disconnected from server. Reconnecting...');
|
||
setTimeout(initWebSocket, 3000);
|
||
};
|
||
|
||
ws.onerror = function(error) {
|
||
console.error('WebSocket error:', error);
|
||
};
|
||
}
|
||
|
||
function handleSerialData(data) {
|
||
if (data.direction === 'RX') {
|
||
rxCount += data.data.length;
|
||
document.getElementById('rxCount').textContent = rxCount;
|
||
} else if (data.direction === 'TX') {
|
||
txCount += data.data.length;
|
||
document.getElementById('txCount').textContent = txCount;
|
||
}
|
||
|
||
frameCount++;
|
||
document.getElementById('frameCount').textContent = frameCount;
|
||
|
||
// TeraTerm 모드가 아닐 때만 Modbus 파싱
|
||
if (!teratermMode && modbusParseEnabled && data.direction === 'RX') {
|
||
const modbusFrame = parseModbus(data.data);
|
||
if (modbusFrame) {
|
||
modbusCount++;
|
||
document.getElementById('modbusCount').textContent = modbusCount;
|
||
displayModbusFrame(data.direction, modbusFrame, data.timestamp);
|
||
return;
|
||
}
|
||
}
|
||
|
||
// 일반 표시
|
||
displaySerialData(data.direction, data.data, data.timestamp);
|
||
}
|
||
|
||
function parseModbus(hexData) {
|
||
// 최소 Modbus RTU 프레임 길이: 주소(1) + 기능(1) + 데이터(최소0) + CRC(2) = 4 bytes
|
||
if (hexData.length < 4) return null;
|
||
|
||
const bytes = hexData.match(/.{2}/g).map(h => parseInt(h, 16));
|
||
|
||
// CRC 검증
|
||
if (!verifyModbusCRC(bytes)) {
|
||
return null;
|
||
}
|
||
|
||
const slaveAddr = bytes[0];
|
||
const funcCode = bytes[1];
|
||
const dataBytes = bytes.slice(2, -2);
|
||
const crc = bytes.slice(-2);
|
||
|
||
// Function Code가 유효한지 확인
|
||
const funcName = MODBUS_FUNCTIONS[funcCode] ||
|
||
(funcCode >= 0x80 ? 'Error Response' : 'Unknown');
|
||
|
||
return {
|
||
slaveAddr: slaveAddr,
|
||
funcCode: funcCode,
|
||
funcName: funcName,
|
||
data: dataBytes,
|
||
crc: crc,
|
||
isError: funcCode >= 0x80
|
||
};
|
||
}
|
||
|
||
function verifyModbusCRC(bytes) {
|
||
if (bytes.length < 4) return false;
|
||
|
||
const data = bytes.slice(0, -2);
|
||
const receivedCRC = (bytes[bytes.length-1] << 8) | bytes[bytes.length-2];
|
||
const calculatedCRC = calculateModbusCRC(data);
|
||
|
||
return receivedCRC === calculatedCRC;
|
||
}
|
||
|
||
function calculateModbusCRC(data) {
|
||
let crc = 0xFFFF;
|
||
|
||
for (let i = 0; i < data.length; i++) {
|
||
crc ^= data[i];
|
||
for (let j = 0; j < 8; j++) {
|
||
if (crc & 0x0001) {
|
||
crc = (crc >> 1) ^ 0xA001;
|
||
} else {
|
||
crc = crc >> 1;
|
||
}
|
||
}
|
||
}
|
||
|
||
return crc;
|
||
}
|
||
|
||
function displayModbusFrame(direction, frame, timestamp) {
|
||
const terminal = document.getElementById('terminal');
|
||
const line = document.createElement('div');
|
||
line.className = 'line';
|
||
|
||
const ts = new Date(timestamp).toLocaleTimeString('en-US', {
|
||
hour12: false,
|
||
hour: '2-digit',
|
||
minute: '2-digit',
|
||
second: '2-digit',
|
||
fractionalSecondDigits: 3
|
||
});
|
||
|
||
let html = `<span class="timestamp">[${ts}]</span>`;
|
||
html += `<span class="${direction.toLowerCase()}">[${direction}]</span> `;
|
||
|
||
html += '<div class="modbus-frame">';
|
||
|
||
// Slave Address
|
||
html += `<span class="modbus-addr">${frame.slaveAddr.toString(16).toUpperCase().padStart(2, '0')}</span>`;
|
||
html += '<span class="modbus-label">(Addr)</span> ';
|
||
|
||
// Function Code
|
||
const funcHex = frame.funcCode.toString(16).toUpperCase().padStart(2, '0');
|
||
if (frame.isError) {
|
||
html += `<span class="modbus-error">${funcHex}</span>`;
|
||
html += `<span class="modbus-label">(ERR: ${frame.funcName})</span> `;
|
||
} else {
|
||
html += `<span class="modbus-func">${funcHex}</span>`;
|
||
html += `<span class="modbus-label">(${frame.funcName})</span> `;
|
||
}
|
||
|
||
// Data
|
||
if (frame.data.length > 0) {
|
||
const dataHex = frame.data.map(b => b.toString(16).toUpperCase().padStart(2, '0')).join(' ');
|
||
html += `<span class="modbus-data">${dataHex}</span>`;
|
||
html += '<span class="modbus-label">(Data)</span> ';
|
||
|
||
// 특정 Function Code에 대한 데이터 해석
|
||
if (frame.funcCode === 0x03 || frame.funcCode === 0x04) {
|
||
// Read Holding/Input Registers 응답
|
||
const byteCount = frame.data[0];
|
||
if (frame.data.length >= byteCount + 1) {
|
||
const registers = [];
|
||
for (let i = 1; i < frame.data.length; i += 2) {
|
||
if (i + 1 < frame.data.length) {
|
||
const regValue = (frame.data[i] << 8) | frame.data[i + 1];
|
||
registers.push(regValue);
|
||
}
|
||
}
|
||
html += `<span class="modbus-label">[Regs: ${registers.join(', ')}]</span> `;
|
||
}
|
||
}
|
||
}
|
||
|
||
// CRC
|
||
const crcHex = frame.crc.map(b => b.toString(16).toUpperCase().padStart(2, '0')).join(' ');
|
||
html += `<span class="modbus-crc">${crcHex}</span>`;
|
||
html += '<span class="modbus-label">(CRC ✓)</span>';
|
||
|
||
html += '</div>';
|
||
|
||
line.innerHTML = html;
|
||
terminal.appendChild(line);
|
||
|
||
if (autoScroll) {
|
||
terminal.scrollTop = terminal.scrollHeight;
|
||
}
|
||
}
|
||
|
||
function displaySerialData(direction, hexData, timestamp) {
|
||
if (teratermMode) {
|
||
displayTeraTermStyle(direction, hexData);
|
||
return;
|
||
}
|
||
|
||
const terminal = document.getElementById('terminal');
|
||
const line = document.createElement('div');
|
||
line.className = 'line';
|
||
|
||
const ts = new Date(timestamp).toLocaleTimeString('en-US', {
|
||
hour12: false,
|
||
hour: '2-digit',
|
||
minute: '2-digit',
|
||
second: '2-digit',
|
||
fractionalSecondDigits: 3
|
||
});
|
||
|
||
let content = '';
|
||
|
||
if (displayFormat === 'hex') {
|
||
content = hexData.match(/.{2}/g).join(' ').toUpperCase();
|
||
} else if (displayFormat === 'ascii') {
|
||
content = hexToAscii(hexData);
|
||
} else { // both
|
||
const hex = hexData.match(/.{2}/g).join(' ').toUpperCase();
|
||
const ascii = hexToAscii(hexData);
|
||
content = `${hex} | ${ascii}`;
|
||
}
|
||
|
||
line.innerHTML = `<span class="timestamp">[${ts}]</span><span class="${direction.toLowerCase()}">[${direction}]</span> ${content}`;
|
||
terminal.appendChild(line);
|
||
|
||
if (autoScroll) {
|
||
terminal.scrollTop = terminal.scrollHeight;
|
||
}
|
||
}
|
||
|
||
function displayTeraTermStyle(direction, hexData) {
|
||
const terminal = document.getElementById('terminal');
|
||
const bytes = hexData.match(/.{2}/g).map(h => parseInt(h, 16));
|
||
|
||
bytes.forEach(byte => {
|
||
const char = (byte >= 32 && byte <= 126) ? String.fromCharCode(byte) : '';
|
||
|
||
if (byte === 0x0D || byte === 0x0A) {
|
||
// 줄바꿈 문자
|
||
const br = document.createElement('br');
|
||
terminal.appendChild(br);
|
||
} else if (char) {
|
||
// 출력 가능한 문자
|
||
const span = document.createElement('span');
|
||
span.className = direction.toLowerCase();
|
||
span.textContent = char;
|
||
terminal.appendChild(span);
|
||
} else {
|
||
// 제어 문자는 [HEX] 형식으로 표시
|
||
const span = document.createElement('span');
|
||
span.className = direction.toLowerCase();
|
||
span.style.color = '#888';
|
||
span.textContent = `[${byte.toString(16).toUpperCase().padStart(2, '0')}]`;
|
||
terminal.appendChild(span);
|
||
}
|
||
});
|
||
|
||
if (autoScroll) {
|
||
terminal.scrollTop = terminal.scrollHeight;
|
||
}
|
||
}
|
||
|
||
function hexToAscii(hex) {
|
||
const bytes = hex.match(/.{2}/g);
|
||
return bytes.map(b => {
|
||
const code = parseInt(b, 16);
|
||
return (code >= 32 && code <= 126) ? String.fromCharCode(code) : '.';
|
||
}).join('');
|
||
}
|
||
|
||
function addToTerminal(type, message) {
|
||
if (teratermMode) {
|
||
// TeraTerm 모드에서는 시스템 메시지를 다르게 표시
|
||
const terminal = document.getElementById('terminal');
|
||
const line = document.createElement('div');
|
||
line.style.color = '#ffa726';
|
||
line.style.marginTop = '5px';
|
||
line.style.marginBottom = '5px';
|
||
line.textContent = `*** ${type}: ${message} ***`;
|
||
terminal.appendChild(line);
|
||
|
||
if (autoScroll) {
|
||
terminal.scrollTop = terminal.scrollHeight;
|
||
}
|
||
return;
|
||
}
|
||
|
||
const terminal = document.getElementById('terminal');
|
||
const line = document.createElement('div');
|
||
line.className = 'line';
|
||
|
||
const now = new Date().toLocaleTimeString('en-US', {
|
||
hour12: false,
|
||
hour: '2-digit',
|
||
minute: '2-digit',
|
||
second: '2-digit'
|
||
});
|
||
|
||
line.innerHTML = `<span class="timestamp">[${now}]</span><span style="color: #ffa726;">[${type}]</span> ${message}`;
|
||
terminal.appendChild(line);
|
||
|
||
if (autoScroll) {
|
||
terminal.scrollTop = terminal.scrollHeight;
|
||
}
|
||
}
|
||
|
||
function updateStatus(data) {
|
||
// Serial2 로깅 상태 업데이트
|
||
if (data.serial2Logging !== undefined) {
|
||
const status = document.getElementById('logStatus');
|
||
if (data.serial2Logging) {
|
||
status.textContent = '🟢 Logging';
|
||
status.style.color = '#38ef7d';
|
||
} else {
|
||
status.textContent = '🔴 Not Logging';
|
||
status.style.color = '#999';
|
||
}
|
||
}
|
||
|
||
// 파일명 업데이트
|
||
if (data.currentSerial2File !== undefined) {
|
||
const fileEl = document.getElementById('currentFile');
|
||
if (data.currentSerial2File) {
|
||
fileEl.textContent = data.currentSerial2File;
|
||
} else {
|
||
fileEl.textContent = '-';
|
||
}
|
||
}
|
||
|
||
// 하위 호환성: logging과 currentFile도 처리
|
||
if (data.logging !== undefined && data.serial2Logging === undefined) {
|
||
const status = document.getElementById('logStatus');
|
||
if (data.logging) {
|
||
status.textContent = '🟢 Logging';
|
||
status.style.color = '#38ef7d';
|
||
} else {
|
||
status.textContent = '🔴 Not Logging';
|
||
status.style.color = '#999';
|
||
}
|
||
}
|
||
|
||
if (data.currentFile && data.currentSerial2File === undefined) {
|
||
document.getElementById('currentFile').textContent = data.currentFile;
|
||
}
|
||
}
|
||
|
||
function updateDisplayMode() {
|
||
const format = document.getElementById('displayFormat').value;
|
||
const teratermOptions = document.getElementById('teratermOptions');
|
||
const terminalInputArea = document.getElementById('terminalInput');
|
||
const terminal = document.getElementById('terminal');
|
||
const sendPanel = document.querySelector('.send-panel');
|
||
|
||
teratermMode = (format === 'teraterm');
|
||
|
||
if (teratermMode) {
|
||
teratermOptions.style.display = 'block';
|
||
terminalInputArea.classList.add('active');
|
||
terminal.classList.add('teraterm-mode');
|
||
if (sendPanel) sendPanel.style.display = 'none';
|
||
} else {
|
||
teratermOptions.style.display = 'none';
|
||
terminalInputArea.classList.remove('active');
|
||
terminal.classList.remove('teraterm-mode');
|
||
if (sendPanel) sendPanel.style.display = 'block';
|
||
}
|
||
}
|
||
|
||
function applySettings() {
|
||
const baudRate = document.getElementById('baudRate').value;
|
||
displayFormat = document.getElementById('displayFormat').value;
|
||
modbusParseEnabled = document.getElementById('modbusParseEnabled').checked;
|
||
|
||
if (teratermMode) {
|
||
lineEnding = document.getElementById('lineEnding').value;
|
||
localEcho = document.getElementById('localEcho').checked;
|
||
}
|
||
|
||
updateDisplayMode();
|
||
|
||
// 설정 저장
|
||
saveSettings();
|
||
|
||
if (ws && ws.readyState === WebSocket.OPEN) {
|
||
ws.send(JSON.stringify({
|
||
cmd: 'setSerial2Baud',
|
||
baud: parseInt(baudRate)
|
||
}));
|
||
|
||
const modeText = teratermMode ? 'TeraTerm' : displayFormat;
|
||
addToTerminal('System', `Settings applied: ${baudRate} baud, Format: ${modeText}, Modbus: ${modbusParseEnabled ? 'ON' : 'OFF'}`);
|
||
}
|
||
}
|
||
|
||
function startLogging() {
|
||
if (ws && ws.readyState === WebSocket.OPEN) {
|
||
ws.send(JSON.stringify({ cmd: 'startSerial2Logging' }));
|
||
addToTerminal('System', 'Starting logging...');
|
||
}
|
||
}
|
||
|
||
function stopLogging() {
|
||
if (ws && ws.readyState === WebSocket.OPEN) {
|
||
ws.send(JSON.stringify({ cmd: 'stopSerial2Logging' }));
|
||
addToTerminal('System', 'Stopping logging...');
|
||
}
|
||
}
|
||
|
||
function sendFromTerminal() {
|
||
const input = document.getElementById('terminalInputField');
|
||
const text = input.value;
|
||
|
||
if (!text) return;
|
||
|
||
// Local Echo
|
||
if (localEcho && teratermMode) {
|
||
const terminal = document.getElementById('terminal');
|
||
const span = document.createElement('span');
|
||
span.className = 'tx';
|
||
span.textContent = text;
|
||
terminal.appendChild(span);
|
||
|
||
// 줄바꿈 추가
|
||
if (lineEnding !== 'none') {
|
||
const br = document.createElement('br');
|
||
terminal.appendChild(br);
|
||
}
|
||
|
||
if (autoScroll) {
|
||
terminal.scrollTop = terminal.scrollHeight;
|
||
}
|
||
}
|
||
|
||
// ASCII to HEX 변환
|
||
let hexData = '';
|
||
for (let i = 0; i < text.length; i++) {
|
||
hexData += text.charCodeAt(i).toString(16).padStart(2, '0');
|
||
}
|
||
|
||
// Line Ending 추가
|
||
switch (lineEnding) {
|
||
case 'cr':
|
||
hexData += '0d';
|
||
break;
|
||
case 'lf':
|
||
hexData += '0a';
|
||
break;
|
||
case 'crlf':
|
||
hexData += '0d0a';
|
||
break;
|
||
}
|
||
|
||
// 전송
|
||
if (ws && ws.readyState === WebSocket.OPEN) {
|
||
ws.send(JSON.stringify({
|
||
cmd: 'sendSerial2',
|
||
data: hexData
|
||
}));
|
||
}
|
||
|
||
input.value = '';
|
||
}
|
||
|
||
function sendData() {
|
||
const input = document.getElementById('sendInput');
|
||
const hexData = input.value.replace(/[^0-9A-Fa-f]/g, '');
|
||
|
||
if (hexData.length === 0) {
|
||
alert('Please enter valid HEX data');
|
||
return;
|
||
}
|
||
|
||
if (hexData.length % 2 !== 0) {
|
||
alert('HEX data must have even number of characters');
|
||
return;
|
||
}
|
||
|
||
if (ws && ws.readyState === WebSocket.OPEN) {
|
||
ws.send(JSON.stringify({
|
||
cmd: 'sendSerial2',
|
||
data: hexData
|
||
}));
|
||
|
||
input.value = '';
|
||
addToTerminal('System', `Sent ${hexData.length / 2} bytes`);
|
||
}
|
||
}
|
||
|
||
function clearTerminal() {
|
||
document.getElementById('terminal').innerHTML = '';
|
||
rxCount = 0;
|
||
txCount = 0;
|
||
frameCount = 0;
|
||
modbusCount = 0;
|
||
document.getElementById('rxCount').textContent = '0';
|
||
document.getElementById('txCount').textContent = '0';
|
||
document.getElementById('frameCount').textContent = '0';
|
||
document.getElementById('modbusCount').textContent = '0';
|
||
}
|
||
|
||
function toggleAutoScroll() {
|
||
autoScroll = !autoScroll;
|
||
document.getElementById('autoScrollText').textContent = 'Auto-scroll: ' + (autoScroll ? 'ON' : 'OFF');
|
||
}
|
||
|
||
// Enter key to send
|
||
document.addEventListener('DOMContentLoaded', function() {
|
||
// 설정 로드
|
||
loadSettings();
|
||
|
||
document.getElementById('sendInput').addEventListener('keypress', function(e) {
|
||
if (e.key === 'Enter') {
|
||
sendData();
|
||
}
|
||
});
|
||
|
||
document.getElementById('terminalInputField').addEventListener('keypress', function(e) {
|
||
if (e.key === 'Enter') {
|
||
sendFromTerminal();
|
||
}
|
||
});
|
||
|
||
// 초기 디스플레이 모드 설정
|
||
updateDisplayMode();
|
||
});
|
||
|
||
// Initialize
|
||
initWebSocket();
|
||
</script>
|
||
</body>
|
||
</html>
|
||
)rawliteral";
|
||
|
||
#endif |