-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
pipe.js
82 lines (62 loc) · 2.18 KB
/
pipe.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
73
74
75
76
77
78
79
80
81
82
// The Tofita Engine
// Copyright (C) 2020 Oleh Petrenko
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// Simple tool to read COM port data from pipe
// `node pipe.js`
// Reconnects automatically after VM reboots, so you don't have to restart this script
// RUN ONCE AND KEEP IT RUNNING FOREVER (keeps CPU usage at low)
// THIS SCRIPT MAY REQUIRE ADMIN RIGHTS TO LISTEN FOR HYPER-V PIPES
/* Hyper-V
Open PowerShell as Administrator
Set-VMComPort -VMName Tofita -Path \\.\pipe\tofita-com1 -Number 1
Set-VMComPort -VMName Tofita -Path \\.\pipe\tofita-com2 -Number 2
Get-VMComPort -VMName Tofita
*/
/* VirtualBox
Set Serial Ports -> Port 1 -> \\.\pipe\tofita-com1
*/
const PIPE_NAME = "tofita-com1"
const PIPE_PATH = "\\\\.\\pipe\\" + PIPE_NAME
// Disallow breaking terminal with special chars
const allowed = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890`~!@#$%^&*()_+-=[]{}\\|;\'",./<>?:\t\r\n '
var client = require('net').connect(PIPE_PATH, function () { })
// Avoid nodejs crashes:
const domain = require('domain').create()
domain.on('error', function (err) {
console.error(err)
})
function reconnect() {
client.removeAllListeners()
client.on('data', function (data) {
let s = []
for (let i = 0; i < data.length; i++) {
if (data[i] == 0) continue
const char = String.fromCharCode(data[i])
if (allowed.indexOf(char) != -1)
s.push(char)
}
process.stdout.write(s.join(''), 'ascii')
})
client.on('end', function () { })
client.on('close', function () {
reconnect()
})
client.on('error', function () {
reconnect()
})
setTimeout(() => {
client.connect(PIPE_PATH)
}, 1)
}
reconnect()