diff --git a/app.js b/app.js index 67b4124..8868d13 100755 --- a/app.js +++ b/app.js @@ -1,43 +1,33 @@ -var http = require('http'); -var fs = require('fs'); -var index = fs.readFileSync( 'index.html'); - -var SerialPort = require('serialport'); -const parsers = SerialPort.parsers; - -const parser = new parsers.Readline({ - delimiter: '\r\n' +import { createServer } from "http"; +import { readFileSync } from "fs"; +import { SerialPort } from "serialport"; +import { ReadlineParser } from "@serialport/parser-readline"; +import { Server as SocketIOServer } from "socket.io"; + +const index = readFileSync("index.html"); +const port = new SerialPort({ path: "/dev/cu.usbmodem101", baudRate: 9600 }); +const parser = port.pipe(new ReadlineParser({ delimiter: "\r\n" })); + +const app = createServer(function (_req, res) { + res.writeHead(200, { "Content-Type": "text/html" }); + res.end(index); }); -var port = new SerialPort('/dev/tty.wchusbserialfa1410',{ - baudRate: 9600, - dataBits: 8, - parity: 'none', - stopBits: 1, - flowControl: false +// Specify the CORS options +const io = new SocketIOServer(app, { + cors: { + origin: "*", // Replace with the domain you want to make requests from + methods: ["GET", "POST"], // Allowed request methods + // allowedHeaders: ["my-custom-header"], // Custom headers here + credentials: true, // Allow credentials (cookies, session IDs) + }, }); -port.pipe(parser); - -var app = http.createServer(function(req, res) { - res.writeHead(200, {'Content-Type': 'text/html'}); - res.end(index); -}); - -var io = require('socket.io').listen(app); - -io.on('connection', function(socket) { - - console.log('Node is listening to port'); - -}); +io.on("connection", () => console.log("Node is listening to port")); -parser.on('data', function(data) { - - console.log('Received data from port: ' + data); - - io.emit('data', data); - +parser.on("data", (data) => { + // console.log(data); + io.emit("data", data); }); app.listen(3000); diff --git a/index.html b/index.html index 786fc74..3376ff2 100755 --- a/index.html +++ b/index.html @@ -2,7 +2,7 @@
- +