This repository has been archived by the owner on Dec 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
dump-can.js
executable file
·72 lines (64 loc) · 2.06 KB
/
dump-can.js
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
#!/usr/bin/env node
const cli = require('commander');
const Panda = require('./lib').default;
const wait = require('./src/delay');
const package = require('./package');
cli
.version(package.version)
.description('Dump data from connected Panda over either USB or wifi. Uses pandajs under the hood.')
.option('-w, --wifi', 'Connect to Panda over wifi instead of USB')
.option('-a, --all', 'Print every message instead of just summaries (VERY spammy, one message per line JSON encoded)')
.option('-n, --no-health', 'Don\'t print startup/connection messages (useful with --all and output redirection)')
.option('-e, --no-errors', 'Don\'t print errors either')
.option('-i, --index <i>', 'Choose a different connected panda than the first one (zero indexed)', parseInt)
.parse(process.argv);
var panda = new Panda({
wifi: cli.wifi,
selectDevice: (devices) => {
return devices[Math.min(devices.length, cli.index || 0)];
}
});
panda.onMessage(function (msg) {
if (cli.all) {
msg.forEach((m) => console.log(JSON.stringify(m, function(k, v) {
if (!k) {
return v;
}
if (k === 'data' && v.type === 'Buffer') {
return v.data;
// return '0x' + Buffer.from(v.data).toString('hex');
}
return v;
})));
} else {
console.log('Message count:', msg.length);
console.log('First message CAN count:', msg[0].canMessages.length);
console.log('First CAN message:', msg[0].canMessages[0]);
}
});
panda.onError(function (err) {
if (cli.errors) {
console.error('Error:', err);
process.exit(1);
}
});
if (cli.health) {
panda.onConnect(function (data) {
console.log('Connected:', data);
});
panda.onDisconnect(function (data) {
console.log('Disconnected:', data);
});
}
connectAndRun();
async function connectAndRun () {
await panda.connect();
if (cli.health) {
var health = await panda.getHealth();
console.log(health);
console.log('Connect finished, waiting then reading all messages...');
await wait(1000);
console.log('Start reading...');
}
panda.unpause();
}