From dd893f21f9260ff73ce8b219a744632465b379de Mon Sep 17 00:00:00 2001 From: ali-f Date: Thu, 17 Oct 2024 18:19:03 +0330 Subject: [PATCH] 1 --- controller/serialPort.js | 4 +++- services/serialPortService.js | 25 +++++++++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) 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