diff --git a/controller/serialPort.js b/controller/serialPort.js index 5e78d67..9c0f529 100644 --- a/controller/serialPort.js +++ b/controller/serialPort.js @@ -1,8 +1,10 @@ import { Router } from 'express'; import serialPort from '../services/serialPortService.js'; +import serialPortList from '../services/serialPortService.js'; const router = new Router() -router.get('/:id', serialPort); +router.get('/open/:id', serialPort); +router.get('/list', serialPortList); export default router \ No newline at end of file diff --git a/services/serialPortService.js b/services/serialPortService.js index 400784d..c313bfa 100644 --- a/services/serialPortService.js +++ b/services/serialPortService.js @@ -1,10 +1,23 @@ import { SerialPort } from 'serialport' +import res from "express/lib/response.js"; +import { autoDetect } from '@serialport/bindings-cpp' -const serialport = new SerialPort({ path: 'COM3', baudRate: 9600, autoOpen: false, }, function (err) { - if (err) { - return console.log('Error: ', err.message); - } -}) + + + +async function serialPortList(req, res) { + const data = autoDetect() + console.log('data', await data.open()) + // .then((ports) => { + // console.log('Available serial ports:'); + // ports.forEach((port) => { + // console.log(`- ${port.comName} (${port.pnpId})`); + // }); + // }) + // .catch((error) => { + // console.error('Error listing serial ports:', error); + // }); +} async function serialPort(req, res) { const buff = Buffer.allocUnsafe(4); @@ -36,4 +49,4 @@ async function serialPort(req, res) { // }) } -export default serialPort \ No newline at end of file +export default serialPortList \ No newline at end of file