42 lines
967 B
JavaScript
42 lines
967 B
JavaScript
// const { SerialPort } = require('serialport')
|
|
// // or
|
|
// import { SerialPort } from 'serialport'
|
|
|
|
// // Create a port
|
|
// const port = new SerialPort({
|
|
// path: '/dev/tty-usbserial1',
|
|
// baudRate: 57600,
|
|
// })
|
|
|
|
const { SerialPort } = require('serialport')
|
|
const { ReadlineParser } = require('@serialport/parser-readline')
|
|
const http = require('node:http');
|
|
const port = new SerialPort({ path: 'COM9', baudRate: 9600 })
|
|
|
|
const parser = port.pipe(new ReadlineParser({ delimiter: '\r\n' }))
|
|
|
|
var value_arduino = "";
|
|
|
|
parser.on('data', (data => {
|
|
if (value_arduino != data) {
|
|
console.log(data);
|
|
}
|
|
value_arduino = data;
|
|
}));
|
|
|
|
const server = http.createServer((req, res) => {
|
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
console.log( req.url);
|
|
if (req.url == "/hello") {
|
|
res.end("/hello");
|
|
|
|
}
|
|
else {
|
|
res.end(value_arduino);
|
|
console.log( req.url);
|
|
}
|
|
|
|
|
|
});
|
|
|
|
server.listen(3070); |