Files
serialport/listen.ts
2025-12-12 16:41:45 +02:00

36 lines
1.0 KiB
TypeScript

import { SerialPort } from 'serialport';
import { ReadlineParser } from '@serialport/parser-readline';
// --- Config ---
// This is now the correct port from your dmesg log!
const PORT_PATH = '/dev/ttyACM1';
// This is a guess. Check your manual if 9600 doesn't work.
const BAUD_RATE = 9600;
// --- Code ---
console.log(`Attempting to open port: ${PORT_PATH} at ${BAUD_RATE} baud...`);
const port = new SerialPort({
path: PORT_PATH,
baudRate: BAUD_RATE,
});
// Use the ReadlineParser to get data line-by-line
const parser = port.pipe(new ReadlineParser({ delimiter: '\n' }));
port.on('open', () => {
console.log(`✅ Serial port open. Waiting for scans...`);
});
// Listen to the parser for complete lines
parser.on('data', (line: string) => {
console.log(`[SCANNER DATA]: ${line}`);
});
port.on('error', (err) => {
console.error('❌ Serial port error:', err.message);
if (err.message.includes('Permission denied')) {
console.error("Hint: Did you log out and back in after running 'sudo usermod -aG dialout'?");
}
});