-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialMonitor.js
More file actions
262 lines (229 loc) · 6.63 KB
/
serialMonitor.js
File metadata and controls
262 lines (229 loc) · 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import { SerialPort } from 'serialport';
import { ReadlineParser } from '@serialport/parser-readline';
import inquirer from 'inquirer';
import chalk from 'chalk';
import fs from 'fs';
import path from 'path';
let serialConnection = null;
let logStream = null;
async function main() {
console.log(chalk.blue('==================================='));
console.log(chalk.blue(' Serial Monitor CLI'));
console.log(chalk.blue('==================================='));
try {
await showMainMenu();
} catch (error) {
console.error(chalk.red(`Error: ${error.message}`));
process.exit(1);
}
}
async function showMainMenu() {
if (serialConnection) {
await showConnectedMenu();
} else {
await showDisconnectedMenu();
}
}
async function showDisconnectedMenu() {
const { action } = await inquirer.prompt([
{
type: 'list',
name: 'action',
message: 'What would you like to do?',
choices: [
{ name: 'Connect to a device', value: 'connect' },
{ name: 'Exit', value: 'exit' }
]
}
]);
if (action === 'connect') {
await connectToDevice();
} else if (action === 'exit') {
console.log(chalk.blue('Goodbye!'));
process.exit(0);
}
}
async function showConnectedMenu() {
const { action } = await inquirer.prompt([
{
type: 'list',
name: 'action',
message: `Connected to ${serialConnection.path}. What would you like to do?`,
choices: [
{ name: 'Disconnect', value: 'disconnect' },
{ name: 'Send command', value: 'send' },
{ name: 'Start/Stop logging to file', value: 'log' },
{ name: 'Exit', value: 'exit' }
]
}
]);
if (action === 'disconnect') {
await disconnectFromDevice();
await showMainMenu();
} else if (action === 'send') {
await sendCommand();
await showConnectedMenu();
} else if (action === 'log') {
await toggleLogging();
await showConnectedMenu();
} else if (action === 'exit') {
await disconnectFromDevice();
console.log(chalk.blue('Goodbye!'));
process.exit(0);
}
}
async function connectToDevice() {
try {
// Get available ports
const ports = await SerialPort.list();
if (ports.length === 0) {
console.log(chalk.yellow('No serial ports detected. Please connect a device.'));
await showMainMenu();
return;
}
// Create port choices with detailed information
const portChoices = ports.map(port => ({
name: `${port.path} - ${port.manufacturer || 'Unknown'} ${port.serialNumber ? `(SN: ${port.serialNumber})` : ''}`,
value: port.path
}));
// Let user select a port
const { selectedPort } = await inquirer.prompt([
{
type: 'list',
name: 'selectedPort',
message: 'Select a serial port:',
choices: portChoices
}
]);
// Let user select baud rate
const { baudRate } = await inquirer.prompt([
{
type: 'list',
name: 'baudRate',
message: 'Select baud rate:',
choices: [9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600],
default: 115200
}
]);
console.log(chalk.yellow(`Connecting to ${selectedPort} at ${baudRate} baud...`));
// Create the serial connection
serialConnection = new SerialPort({
path: selectedPort,
baudRate: parseInt(baudRate),
dataBits: 8,
stopBits: 1,
parity: 'none'
});
// Set up parser for incoming data
const parser = serialConnection.pipe(new ReadlineParser({ delimiter: '\r\n' }));
// Handle port events
serialConnection.on('open', () => {
console.debug('\n', chalk.green(`Connected to ${selectedPort} at ${baudRate} baud`));
});
serialConnection.on('error', (err) => {
console.error(chalk.red(`Serial port error: ${err.message}`));
serialConnection = null;
showMainMenu();
});
serialConnection.on('close', () => {
console.log(chalk.yellow('Serial connection closed'));
serialConnection = null;
if (logStream) {
logStream.end();
logStream = null;
}
showMainMenu();
});
// Display incoming data
parser.on('data', (data) => {
const timestamp = new Date().toISOString();
console.log(chalk.cyan(`[${timestamp}] ${data}`));
const streamMsg = `[${timestamp}] ${data}`;
// Log to file if enabled
if (logStream) {
logStream.write(`${streamMsg}\n`);
}
console.debug(
`\n ${streamMsg}`
);
});
await showMainMenu();
} catch (error) {
console.error(chalk.red(`Connection error: ${error.message}`));
serialConnection = null;
await showMainMenu();
}
}
async function disconnectFromDevice() {
if (serialConnection) {
return new Promise((resolve) => {
serialConnection.close(() => {
console.log(chalk.yellow('Disconnected from serial device'));
serialConnection = null;
if (logStream) {
logStream.end();
logStream = null;
console.log(chalk.yellow('Logging stopped'));
}
resolve();
});
});
}
}
async function sendCommand() {
if (!serialConnection) {
console.log(chalk.red('Not connected to any device'));
return;
}
const { command } = await inquirer.prompt([
{
type: 'input',
name: 'command',
message: 'Enter command to send:',
}
]);
try {
serialConnection.write(command + '\r\n');
console.log(chalk.green(`Command sent: ${command}`));
} catch (error) {
console.error(chalk.red(`Failed to send command: ${error.message}`));
}
}
async function toggleLogging() {
if (logStream) {
// Stop logging
logStream.end();
logStream = null;
console.log(chalk.yellow('Logging stopped'));
} else {
// Start logging
const { fileName } = await inquirer.prompt([
{
type: 'input',
name: 'fileName',
message: 'Enter log file name:',
default: `serial_log_${new Date().toISOString().replace(/:/g, '-')}.txt`
}
]);
try {
const logDir = path.join(process.cwd(), 'logs');
// Create logs directory if it doesn't exist
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir);
}
const logPath = path.join(logDir, fileName);
logStream = fs.createWriteStream(logPath, { flags: 'a' });
console.log(chalk.green(`Logging to ${logPath}`));
} catch (error) {
console.error(chalk.red(`Failed to create log file: ${error.message}`));
}
}
}
// Handle process exit
process.on('SIGINT', async () => {
console.log(chalk.yellow('\nGracefully shutting down...'));
await disconnectFromDevice();
process.exit(0);
});
// Start the application
main();