Skip to content

Commit

Permalink
add termport
Browse files Browse the repository at this point in the history
  • Loading branch information
Egor Bizyaev authored and Egor Bizyaev committed Dec 5, 2022
0 parents commit 3e53a40
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
59 changes: 59 additions & 0 deletions app.js
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);
}
});
20 changes: 20 additions & 0 deletions package.json
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"
}
}

0 comments on commit 3e53a40

Please sign in to comment.