94 lines
1.9 KiB
JavaScript
94 lines
1.9 KiB
JavaScript
// const { SerialPort } = require('serialport')
|
|
// // Create a port
|
|
// const port = new SerialPort({
|
|
// path: '/com16/tty-usbserial1',
|
|
// baudRate: 9600,
|
|
// })
|
|
|
|
// port.on('readable', function () {
|
|
// console.log('Data:', port.read())
|
|
// })
|
|
|
|
// // Switches the port into "flowing mode"
|
|
// port.on('data', function (data) {
|
|
// console.log('Data:', data.toString('UTF8'))
|
|
// })
|
|
const fs = require('fs');
|
|
const http = require('node:http');
|
|
const {
|
|
SerialPort
|
|
} = require('serialport')
|
|
const {
|
|
ReadlineParser
|
|
} = require('@serialport/parser-readline')
|
|
|
|
const port = new SerialPort({
|
|
path: '/com11',
|
|
baudRate: 9600
|
|
})
|
|
|
|
var str = "";
|
|
var obj;
|
|
|
|
const parser = port.pipe(new ReadlineParser({
|
|
delimiter: '\r\n'
|
|
}))
|
|
parser.on('data', (data) => {
|
|
if (str != data) {
|
|
try {
|
|
obj = JSON.parse(data);
|
|
|
|
console.log(obj);
|
|
} catch {
|
|
console.log("Ошибка");
|
|
}
|
|
|
|
}
|
|
str = data;
|
|
})
|
|
|
|
|
|
// Create a local server to receive data from
|
|
const server = http.createServer((req, res) => {
|
|
|
|
console.log(req.url);
|
|
if (req.url == '/') {
|
|
fs.readFile('index.html', (err, data) => {
|
|
if (err) {
|
|
res.writeHead(404, "Not Found");
|
|
res.write("No file found")
|
|
res.end();
|
|
} else {
|
|
res.writeHead(200, {
|
|
'Content-Type': 'text/html'
|
|
})
|
|
res.write(data)
|
|
res.end();
|
|
}
|
|
})
|
|
} else if (req.url == '/pin') {
|
|
res.writeHead(200, {
|
|
'Content-Type': 'application/json'
|
|
});
|
|
res.end(str);
|
|
} else if (req.url == '/potentiometer') {
|
|
res.writeHead(200, {
|
|
'Content-Type': 'application/json'
|
|
});
|
|
if (obj) {
|
|
res.end(obj.valuePotentiometer.toString());
|
|
}
|
|
} else if (req.url == '/gpu') {
|
|
res.writeHead(200, {
|
|
'Content-Type': 'application/json'
|
|
});
|
|
if (obj) {
|
|
res.end(obj.valueGPU.toString());
|
|
}
|
|
} else {
|
|
res.writeHead(404, "Not Found");
|
|
res.end();
|
|
}
|
|
});
|
|
|
|
server.listen(2048); |