mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-22 14:01:42 +03:00
1 parent
bc7166324a
commit
5dd16d4b49
4 files changed
+318
No files matched your search
@@ -0,0 +1,13 @@
|
|||||||
|
import { DeviceAnalytics } from '@vicons/tabler';
|
||||||
|
import { defineTool } from '../tool';
|
||||||
|
|
||||||
|
export const tool = defineTool({
|
||||||
|
name: 'Serial Port Console',
|
||||||
|
path: '/serial-console',
|
||||||
|
description: 'Connect, read and send message to Serial Port',
|
||||||
|
keywords: ['serial', 'port', 'console'],
|
||||||
|
component: () => import('./serial-console.vue'),
|
||||||
|
icon: DeviceAnalytics,
|
||||||
|
createdAt: new Date('2025-11-11'),
|
||||||
|
category: 'Forensic',
|
||||||
|
});
|
||||||
@@ -0,0 +1,135 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { nextTick, onMounted, ref } from 'vue';
|
||||||
|
import { NButton, NCard, NInput, NInputGroup, NSelect, NSpace } from 'naive-ui';
|
||||||
|
import { useSerialPort } from './useSerialPort';
|
||||||
|
|
||||||
|
const input = ref('');
|
||||||
|
const outputRef = ref<HTMLElement | null>(null);
|
||||||
|
const selectedPort = ref<SerialPort | undefined>();
|
||||||
|
|
||||||
|
const {
|
||||||
|
isConnected,
|
||||||
|
outputLines,
|
||||||
|
baudRate,
|
||||||
|
lineEnding,
|
||||||
|
availablePorts,
|
||||||
|
connect,
|
||||||
|
disconnect,
|
||||||
|
send,
|
||||||
|
listPorts,
|
||||||
|
} = useSerialPort();
|
||||||
|
|
||||||
|
const baudOptions = [
|
||||||
|
{ label: '9600', value: 9600 },
|
||||||
|
{ label: '19200', value: 19200 },
|
||||||
|
{ label: '38400', value: 38400 },
|
||||||
|
{ label: '57600', value: 57600 },
|
||||||
|
{ label: '115200', value: 115200 },
|
||||||
|
];
|
||||||
|
|
||||||
|
const lineEndingOptions = [
|
||||||
|
{ label: 'LF (\\n)', value: 'LF' },
|
||||||
|
{ label: 'CR (\\r)', value: 'CR' },
|
||||||
|
{ label: 'CRLF (\\r\\n)', value: 'CRLF' },
|
||||||
|
];
|
||||||
|
|
||||||
|
const portOptions = computed(() =>
|
||||||
|
availablePorts.value.map((p, i) => ({
|
||||||
|
label: `Port ${i + 1} (${p.getInfo().usbVendorId ?? 'Unknown'})`,
|
||||||
|
value: p,
|
||||||
|
})),
|
||||||
|
);
|
||||||
|
|
||||||
|
function sendCommand() {
|
||||||
|
if (!input.value.trim()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
send(input.value);
|
||||||
|
input.value = '';
|
||||||
|
nextTick(() => {
|
||||||
|
outputRef.value?.scrollTo({ top: outputRef.value.scrollHeight, behavior: 'smooth' });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
listPorts();
|
||||||
|
});
|
||||||
|
|
||||||
|
function isWindowSecureContext() {
|
||||||
|
return window.isSecureContext;
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<c-alert v-if="!isWindowSecureContext()" mb-2>
|
||||||
|
{{ $t('tools.pgp-encryption.texts.tag-your-browser-is-not-in') }}<n-a href="https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts" target="_blank">
|
||||||
|
{{ $t('tools.pgp-encryption.texts.tag-secure-context-https') }}
|
||||||
|
</n-a>{{ $t('tools.pgp-encryption.texts.tag-this-tool-may-not-work-correctly-and-require-https-to-work-fully') }}
|
||||||
|
</c-alert>
|
||||||
|
|
||||||
|
<NCard title="Serial Terminal">
|
||||||
|
<NSpace>
|
||||||
|
<NSelect
|
||||||
|
v-model:value="baudRate"
|
||||||
|
:options="baudOptions"
|
||||||
|
placeholder="Baud Rate"
|
||||||
|
style="width: 150px"
|
||||||
|
/>
|
||||||
|
<NSelect
|
||||||
|
v-model:value="lineEnding"
|
||||||
|
:options="lineEndingOptions"
|
||||||
|
placeholder="Line Ending"
|
||||||
|
style="width: 120px"
|
||||||
|
/>
|
||||||
|
<NSelect
|
||||||
|
v-model:value="selectedPort"
|
||||||
|
:options="portOptions"
|
||||||
|
label-field="label"
|
||||||
|
value-field="value"
|
||||||
|
placeholder="Select Port"
|
||||||
|
style="width: 200px"
|
||||||
|
/>
|
||||||
|
<NButton type="primary" :disabled="isConnected" @click="connect(selectedPort)">
|
||||||
|
Connect
|
||||||
|
</NButton>
|
||||||
|
<NButton type="error" :disabled="!isConnected" @click="disconnect">
|
||||||
|
Disconnect
|
||||||
|
</NButton>
|
||||||
|
</NSpace>
|
||||||
|
|
||||||
|
<div ref="outputRef" class="terminal-output">
|
||||||
|
<div v-for="(line, index) in outputLines" :key="index" class="terminal-line">
|
||||||
|
{{ line }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<NInputGroup>
|
||||||
|
<NInput
|
||||||
|
v-model:value="input"
|
||||||
|
placeholder="Type a command..."
|
||||||
|
:disabled="!isConnected"
|
||||||
|
@keydown.enter.prevent="sendCommand"
|
||||||
|
/>
|
||||||
|
<NButton type="primary" :disabled="!isConnected" @click="sendCommand">
|
||||||
|
Send
|
||||||
|
</NButton>
|
||||||
|
</NInputGroup>
|
||||||
|
</NCard>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.terminal-output {
|
||||||
|
background-color: #000;
|
||||||
|
color: #0f0;
|
||||||
|
font-family: monospace;
|
||||||
|
padding: 1rem;
|
||||||
|
height: 300px;
|
||||||
|
overflow-y: auto;
|
||||||
|
border-radius: 4px;
|
||||||
|
margin: 1rem 0;
|
||||||
|
box-shadow: inset 0 0 5px #0f0;
|
||||||
|
}
|
||||||
|
.terminal-line {
|
||||||
|
white-space: pre-wrap;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
Vendored
+17
@@ -0,0 +1,17 @@
|
|||||||
|
// serial.d.ts
|
||||||
|
interface SerialPort {
|
||||||
|
open(options: { baudRate: number }): Promise<void>
|
||||||
|
close(): Promise<void>
|
||||||
|
readable: ReadableStream | null
|
||||||
|
writable: WritableStream | null
|
||||||
|
getInfo(): { usbVendorId?: number; usbProductId?: number }
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Serial extends EventTarget {
|
||||||
|
getPorts(): Promise<SerialPort[]>
|
||||||
|
requestPort(options?: { filters: Array<{ usbVendorId?: number; usbProductId?: number }> }): Promise<SerialPort>
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Navigator {
|
||||||
|
serial: Serial
|
||||||
|
}
|
||||||
@@ -0,0 +1,153 @@
|
|||||||
|
// composables/useSerialPort.ts
|
||||||
|
import { onBeforeUnmount, ref } from 'vue';
|
||||||
|
|
||||||
|
export function useSerialPort() {
|
||||||
|
const isConnected = ref(false);
|
||||||
|
const outputLines = ref<string[]>([]);
|
||||||
|
const baudRate = ref(9600);
|
||||||
|
const lineEnding = ref<'CR' | 'LF' | 'CRLF'>('LF');
|
||||||
|
const availablePorts = ref<SerialPort[]>([]);
|
||||||
|
|
||||||
|
let port: SerialPort | null = null;
|
||||||
|
let reader: ReadableStreamDefaultReader<string> | null = null;
|
||||||
|
let writer: WritableStreamDefaultWriter<string> | null = null;
|
||||||
|
let inputDone: Promise<void> | null | undefined = null;
|
||||||
|
let outputDone: Promise<void> | null | undefined = null;
|
||||||
|
let disconnecting = false;
|
||||||
|
let reconnectAttempts = 0;
|
||||||
|
const maxReconnects = 3;
|
||||||
|
|
||||||
|
function appendOutput(data: string) {
|
||||||
|
outputLines.value.push(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function listPorts() {
|
||||||
|
availablePorts.value = await navigator.serial.getPorts();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function connect(selectedPort?: SerialPort) {
|
||||||
|
try {
|
||||||
|
port = selectedPort || await navigator.serial.requestPort();
|
||||||
|
await openPort();
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
appendOutput(`[Connect error] ${err}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function openPort() {
|
||||||
|
try {
|
||||||
|
await port!.open({ baudRate: baudRate.value });
|
||||||
|
isConnected.value = true;
|
||||||
|
reconnectAttempts = 0;
|
||||||
|
|
||||||
|
const decoder = new TextDecoderStream();
|
||||||
|
inputDone = port!.readable!.pipeTo(decoder!.writable);
|
||||||
|
reader = decoder.readable.getReader();
|
||||||
|
readLoop();
|
||||||
|
|
||||||
|
const encoder = new TextEncoderStream();
|
||||||
|
outputDone = encoder.readable.pipeTo(port!.writable!);
|
||||||
|
writer = encoder.writable.getWriter();
|
||||||
|
|
||||||
|
appendOutput('[Connected]');
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
appendOutput(`[Open error] ${err}`);
|
||||||
|
attemptReconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function disconnect() {
|
||||||
|
if (!port || disconnecting) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
disconnecting = true;
|
||||||
|
appendOutput('[Disconnecting...]');
|
||||||
|
|
||||||
|
try {
|
||||||
|
reader?.cancel();
|
||||||
|
await inputDone?.catch(() => {});
|
||||||
|
reader?.releaseLock();
|
||||||
|
|
||||||
|
await writer?.close().catch(() => {});
|
||||||
|
await outputDone?.catch(() => {});
|
||||||
|
writer?.releaseLock();
|
||||||
|
|
||||||
|
await port.close();
|
||||||
|
appendOutput('[Disconnected]');
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
appendOutput(`[Teardown error] ${err}`);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
isConnected.value = false;
|
||||||
|
disconnecting = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function readLoop() {
|
||||||
|
try {
|
||||||
|
while (true) {
|
||||||
|
const { value, done } = await reader!.read();
|
||||||
|
if (done) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (value) {
|
||||||
|
appendOutput(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
if (!disconnecting) {
|
||||||
|
appendOutput(`[Read error] ${err}`);
|
||||||
|
attemptReconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function send(data: string) {
|
||||||
|
if (!writer || !isConnected.value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const ending = lineEnding.value === 'CR'
|
||||||
|
? '\r'
|
||||||
|
: lineEnding.value === 'CRLF'
|
||||||
|
? '\r\n'
|
||||||
|
: '\n';
|
||||||
|
writer.write(data + ending);
|
||||||
|
appendOutput(`> ${data}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function attemptReconnect() {
|
||||||
|
if (reconnectAttempts >= maxReconnects) {
|
||||||
|
appendOutput('[Reconnect failed]');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
reconnectAttempts++;
|
||||||
|
appendOutput(`[Reconnecting... attempt ${reconnectAttempts}]`);
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 1000 * reconnectAttempts));
|
||||||
|
try {
|
||||||
|
await openPort();
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
attemptReconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onBeforeUnmount(() => {
|
||||||
|
disconnect();
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
isConnected,
|
||||||
|
outputLines,
|
||||||
|
baudRate,
|
||||||
|
lineEnding,
|
||||||
|
availablePorts,
|
||||||
|
connect,
|
||||||
|
disconnect,
|
||||||
|
send,
|
||||||
|
listPorts,
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in new issue
Block a user