1299 lines
44 KiB
C++
1299 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>
|
||
* {
|
||
margin: 0;
|
||
padding: 0;
|
||
box-sizing: border-box;
|
||
}
|
||
|
||
body {
|
||
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||
min-height: 100vh;
|
||
padding: 20px;
|
||
}
|
||
|
||
.container {
|
||
max-width: 1200px;
|
||
margin: 0 auto;
|
||
background: rgba(255, 255, 255, 0.95);
|
||
border-radius: 15px;
|
||
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
|
||
overflow: hidden;
|
||
}
|
||
|
||
.header {
|
||
background: linear-gradient(135deg, #667eea, #764ba2);
|
||
color: white;
|
||
padding: 20px 30px;
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
box-shadow: 0 2px 10px rgba(0,0,0,0.2);
|
||
}
|
||
|
||
.header h1 {
|
||
font-size: 1.8em;
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 10px;
|
||
}
|
||
|
||
.badge {
|
||
background: rgba(255, 255, 255, 0.2);
|
||
padding: 5px 12px;
|
||
border-radius: 20px;
|
||
font-size: 0.5em;
|
||
font-weight: normal;
|
||
}
|
||
|
||
/* ========== 통일된 네비게이션 ========== */
|
||
.nav {
|
||
background: #f8f9fa;
|
||
padding: 0;
|
||
border-bottom: 2px solid #e0e0e0;
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
justify-content: center;
|
||
align-items: center;
|
||
}
|
||
|
||
.nav a {
|
||
display: inline-flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
padding: 15px 20px;
|
||
text-decoration: none;
|
||
color: #666;
|
||
font-weight: 500;
|
||
font-size: 0.95em;
|
||
border-bottom: 3px solid transparent;
|
||
transition: all 0.3s;
|
||
white-space: nowrap;
|
||
min-width: 120px;
|
||
}
|
||
|
||
.nav a:hover {
|
||
background: #e9ecef;
|
||
color: #667eea;
|
||
transform: translateY(-2px);
|
||
}
|
||
|
||
.nav a.active {
|
||
color: #764ba2;
|
||
border-bottom-color: #764ba2;
|
||
background: white;
|
||
font-weight: 600;
|
||
}
|
||
|
||
.content {
|
||
padding: 30px;
|
||
}
|
||
|
||
.control-panel {
|
||
background: #f8f9fa;
|
||
padding: 20px;
|
||
border-radius: 10px;
|
||
margin-bottom: 20px;
|
||
display: grid;
|
||
grid-template-columns: 1fr;
|
||
gap: 20px;
|
||
}
|
||
|
||
.control-group {
|
||
background: white;
|
||
padding: 15px;
|
||
border-radius: 8px;
|
||
border: 1px solid #e0e0e0;
|
||
display: grid;
|
||
grid-template-columns: 1fr 1fr;
|
||
gap: 20px;
|
||
}
|
||
|
||
.control-group.full-width {
|
||
grid-template-columns: 1fr;
|
||
}
|
||
|
||
.control-section {
|
||
display: flex;
|
||
flex-direction: column;
|
||
}
|
||
|
||
.control-group h3 {
|
||
color: #333;
|
||
margin-bottom: 15px;
|
||
font-size: 1.1em;
|
||
border-bottom: 2px solid #667eea;
|
||
padding-bottom: 8px;
|
||
}
|
||
|
||
.form-row {
|
||
display: grid;
|
||
grid-template-columns: 120px 1fr;
|
||
gap: 10px;
|
||
margin-bottom: 12px;
|
||
align-items: center;
|
||
}
|
||
|
||
.form-row label {
|
||
color: #555;
|
||
font-weight: 500;
|
||
}
|
||
|
||
.checkbox-row {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 10px;
|
||
margin-top: 12px;
|
||
padding: 10px;
|
||
background: #f8f9fa;
|
||
border-radius: 5px;
|
||
}
|
||
|
||
.checkbox-row input[type="checkbox"] {
|
||
width: 18px;
|
||
height: 18px;
|
||
cursor: pointer;
|
||
}
|
||
|
||
.checkbox-row label {
|
||
color: #555;
|
||
font-weight: 500;
|
||
cursor: pointer;
|
||
}
|
||
|
||
select, input[type="text"], input[type="number"] {
|
||
padding: 8px 12px;
|
||
border: 1px solid #ddd;
|
||
border-radius: 5px;
|
||
font-size: 14px;
|
||
width: 100%;
|
||
}
|
||
|
||
select:focus, input:focus {
|
||
outline: none;
|
||
border-color: #667eea;
|
||
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
|
||
}
|
||
|
||
.btn-group {
|
||
display: flex;
|
||
gap: 10px;
|
||
margin-top: 15px;
|
||
}
|
||
|
||
.btn {
|
||
flex: 1;
|
||
padding: 12px;
|
||
border: none;
|
||
border-radius: 8px;
|
||
font-size: 14px;
|
||
font-weight: 600;
|
||
cursor: pointer;
|
||
transition: all 0.3s;
|
||
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
||
}
|
||
|
||
.btn:hover {
|
||
transform: translateY(-2px);
|
||
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
|
||
}
|
||
|
||
.btn-primary {
|
||
background: linear-gradient(135deg, #667eea, #764ba2);
|
||
color: white;
|
||
}
|
||
|
||
.btn-success {
|
||
background: linear-gradient(135deg, #11998e, #38ef7d);
|
||
color: white;
|
||
}
|
||
|
||
.btn-danger {
|
||
background: linear-gradient(135deg, #eb3349, #f45c43);
|
||
color: white;
|
||
}
|
||
|
||
.btn-secondary {
|
||
background: #6c757d;
|
||
color: white;
|
||
}
|
||
|
||
.terminal-container {
|
||
background: #1e1e1e;
|
||
border-radius: 10px;
|
||
overflow: hidden;
|
||
box-shadow: 0 5px 20px rgba(0,0,0,0.3);
|
||
}
|
||
|
||
.terminal-header {
|
||
background: #2d2d2d;
|
||
padding: 10px 15px;
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
border-bottom: 1px solid #444;
|
||
}
|
||
|
||
.terminal-title {
|
||
color: #ccc;
|
||
font-size: 13px;
|
||
font-weight: 500;
|
||
}
|
||
|
||
.terminal-controls {
|
||
display: flex;
|
||
gap: 8px;
|
||
}
|
||
|
||
.terminal-btn {
|
||
padding: 4px 12px;
|
||
border: none;
|
||
border-radius: 4px;
|
||
font-size: 12px;
|
||
cursor: pointer;
|
||
background: #444;
|
||
color: #ccc;
|
||
transition: all 0.2s;
|
||
}
|
||
|
||
.terminal-btn:hover {
|
||
background: #555;
|
||
}
|
||
|
||
.terminal {
|
||
height: 400px;
|
||
overflow-y: auto;
|
||
padding: 15px;
|
||
font-family: 'Courier New', monospace;
|
||
font-size: 13px;
|
||
line-height: 1.5;
|
||
color: #f0f0f0;
|
||
}
|
||
|
||
.terminal.teraterm-mode {
|
||
height: 350px;
|
||
padding-bottom: 5px;
|
||
}
|
||
|
||
.terminal-input-area {
|
||
display: none;
|
||
background: #2d2d2d;
|
||
padding: 10px 15px;
|
||
border-top: 1px solid #444;
|
||
}
|
||
|
||
.terminal-input-area.active {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 10px;
|
||
}
|
||
|
||
.terminal-input-area .prompt {
|
||
color: #4fc3f7;
|
||
font-family: 'Courier New', monospace;
|
||
font-size: 13px;
|
||
}
|
||
|
||
.terminal-input-area input {
|
||
flex: 1;
|
||
background: #1e1e1e;
|
||
border: 1px solid #444;
|
||
color: #f0f0f0;
|
||
padding: 6px 10px;
|
||
font-family: 'Courier New', monospace;
|
||
font-size: 13px;
|
||
border-radius: 3px;
|
||
}
|
||
|
||
.terminal-input-area input:focus {
|
||
outline: none;
|
||
border-color: #667eea;
|
||
box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
|
||
}
|
||
|
||
.terminal::-webkit-scrollbar {
|
||
width: 10px;
|
||
}
|
||
|
||
.terminal::-webkit-scrollbar-track {
|
||
background: #2d2d2d;
|
||
}
|
||
|
||
.terminal::-webkit-scrollbar-thumb {
|
||
background: #555;
|
||
border-radius: 5px;
|
||
}
|
||
|
||
.terminal::-webkit-scrollbar-thumb:hover {
|
||
background: #666;
|
||
}
|
||
|
||
.line {
|
||
margin-bottom: 5px;
|
||
font-family: 'Courier New', monospace;
|
||
}
|
||
|
||
.timestamp {
|
||
color: #888;
|
||
margin-right: 10px;
|
||
}
|
||
|
||
.rx {
|
||
color: #4fc3f7;
|
||
}
|
||
|
||
.tx {
|
||
color: #81c784;
|
||
}
|
||
|
||
/* Modbus 파싱 스타일 */
|
||
.modbus-frame {
|
||
display: inline-block;
|
||
padding: 5px 10px;
|
||
margin: 5px 0;
|
||
background: #2a2a2a;
|
||
border-radius: 5px;
|
||
border-left: 3px solid #667eea;
|
||
}
|
||
|
||
.modbus-addr {
|
||
color: #ffd54f;
|
||
font-weight: bold;
|
||
}
|
||
|
||
.modbus-func {
|
||
color: #81c784;
|
||
font-weight: bold;
|
||
}
|
||
|
||
.modbus-data {
|
||
color: #4fc3f7;
|
||
}
|
||
|
||
.modbus-crc {
|
||
color: #ff8a65;
|
||
}
|
||
|
||
.modbus-label {
|
||
color: #aaa;
|
||
font-size: 11px;
|
||
margin-left: 5px;
|
||
}
|
||
|
||
.modbus-error {
|
||
color: #f44336;
|
||
}
|
||
|
||
.send-panel {
|
||
background: white;
|
||
padding: 15px;
|
||
border-radius: 8px;
|
||
margin-top: 20px;
|
||
border: 1px solid #e0e0e0;
|
||
}
|
||
|
||
.send-panel h3 {
|
||
color: #333;
|
||
margin-bottom: 15px;
|
||
font-size: 1.1em;
|
||
border-bottom: 2px solid #667eea;
|
||
padding-bottom: 8px;
|
||
}
|
||
|
||
.send-controls {
|
||
display: flex;
|
||
gap: 10px;
|
||
align-items: center;
|
||
}
|
||
|
||
.send-controls input {
|
||
flex: 1;
|
||
}
|
||
|
||
.stats {
|
||
background: white;
|
||
padding: 15px;
|
||
border-radius: 8px;
|
||
margin-top: 20px;
|
||
border: 1px solid #e0e0e0;
|
||
}
|
||
|
||
.stats h3 {
|
||
color: #333;
|
||
margin-bottom: 15px;
|
||
font-size: 1.1em;
|
||
border-bottom: 2px solid #667eea;
|
||
padding-bottom: 8px;
|
||
}
|
||
|
||
.stat-grid {
|
||
display: grid;
|
||
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
|
||
gap: 15px;
|
||
}
|
||
|
||
.stat-item {
|
||
text-align: center;
|
||
padding: 15px;
|
||
background: #f8f9fa;
|
||
border-radius: 8px;
|
||
}
|
||
|
||
.stat-value {
|
||
font-size: 24px;
|
||
font-weight: bold;
|
||
color: #667eea;
|
||
margin-bottom: 5px;
|
||
}
|
||
|
||
.stat-label {
|
||
font-size: 12px;
|
||
color: #666;
|
||
text-transform: uppercase;
|
||
}
|
||
|
||
|
||
|
||
/* ========== 반응형 디자인 ========== */
|
||
@media (max-width: 768px) {
|
||
body {
|
||
padding: 5px;
|
||
}
|
||
|
||
.container {
|
||
border-radius: 10px;
|
||
}
|
||
|
||
.header {
|
||
padding: 15px;
|
||
flex-direction: column;
|
||
text-align: center;
|
||
gap: 10px;
|
||
}
|
||
|
||
.header h1 {
|
||
font-size: 1.4em;
|
||
}
|
||
|
||
.nav {
|
||
padding: 5px;
|
||
}
|
||
|
||
.nav a {
|
||
padding: 12px 15px;
|
||
font-size: 0.85em;
|
||
}
|
||
|
||
.content {
|
||
padding: 15px;
|
||
}
|
||
|
||
.btn {
|
||
padding: 10px 20px;
|
||
font-size: 13px;
|
||
}
|
||
}
|
||
|
||
@media (min-width: 769px) and (max-width: 1024px) {
|
||
.header h1 {
|
||
font-size: 1.6em;
|
||
}
|
||
|
||
.nav a {
|
||
padding: 13px 18px;
|
||
font-size: 0.9em;
|
||
}
|
||
|
||
.content {
|
||
padding: 25px;
|
||
}
|
||
}
|
||
|
||
@media (min-width: 1025px) {
|
||
.content {
|
||
padding: 30px;
|
||
}
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div class="container">
|
||
<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 |