-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Egor Bizyaev
authored and
Egor Bizyaev
committed
Dec 5, 2022
0 parents
commit 3e53a40
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const net = require('net'); | ||
const { SerialPort } = require("serialport"); | ||
|
||
let PORT = 3002; | ||
let HOST = '127.0.0.1'; | ||
|
||
let serialPort = new SerialPort({ | ||
path: '/take/your/path', | ||
baudRate: 115200 | ||
}); | ||
|
||
serialPort.on('open', () => { | ||
console.log("-- Connection to serial port opened --"); | ||
}); | ||
|
||
const server = net.createServer((connection) => { | ||
if (serialPort.isOpen) { | ||
console.log('-- Client connected --'); | ||
} | ||
|
||
connection.on('end', () => { | ||
console.log('-- Client disconnected --'); | ||
connection.destroy(); | ||
}); | ||
|
||
connection.pipe(connection); | ||
|
||
serialPort.on('data', data => { | ||
if (!connection.destroyed) { | ||
console.log(data); | ||
connection.write(data); | ||
} | ||
}); | ||
}); | ||
|
||
server.listen(PORT, HOST, () => { | ||
console.log('Server is listening on port ' + PORT); | ||
}); | ||
|
||
server.on('connection', connection => { | ||
if (!connection.destroyed) { | ||
connection.on('data', data => { | ||
console.log(data); | ||
serialPort.write(data); | ||
}); | ||
} | ||
|
||
connection.pipe(connection); | ||
}); | ||
|
||
server.on('error', (e) => { | ||
if (e.code === 'EADDRINUSE') { | ||
console.log('Address in use, retrying...'); | ||
setTimeout(() => { | ||
server.close(); | ||
server.listen(PORT, HOST); | ||
}, 1000); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "term", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "main.js", | ||
"scripts": { | ||
"start": "node app.js", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"express": "^4.18.2", | ||
"iconv": "^3.0.1", | ||
"iconv-lite": "^0.6.3", | ||
"serialport": "^10.5.0", | ||
"socket.io": "^4.5.3", | ||
"windows-1251": "^3.0.4" | ||
} | ||
} |