From 271ed8485ca476097011101b61462d2a8f0a1bbf Mon Sep 17 00:00:00 2001 From: ali-f Date: Thu, 17 Oct 2024 18:58:51 +0330 Subject: [PATCH] 1 --- controller/serialPort.js | 5 +++-- services/serialPortService.js | 15 ++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/controller/serialPort.js b/controller/serialPort.js index 9c0f529..0bf87fd 100644 --- a/controller/serialPort.js +++ b/controller/serialPort.js @@ -4,7 +4,8 @@ import serialPortList from '../services/serialPortService.js'; const router = new Router() -router.get('/open/:id', serialPort); -router.get('/list', serialPortList); +router.get('/open/:id', serialPortList); +// router.get('/list/:id', serialPortList); +// router.get('/open/:id', serialPort); export default router \ No newline at end of file diff --git a/services/serialPortService.js b/services/serialPortService.js index eef5eb1..427b66d 100644 --- a/services/serialPortService.js +++ b/services/serialPortService.js @@ -8,19 +8,24 @@ import { autoDetect } from '@serialport/bindings-cpp' async function serialPortList(req, res) { SerialPort.list() - .then((pathsList) => { - console.log('Available serial ports:'); - for (const path of pathsList) { - const serialport = new SerialPort({ path, baudRate: 9600, autoOpen: false, }, function (err) { + .then(async pathsList => { + for (const pathObject of pathsList) { + await new Promise(resolve => { + setTimeout(() => resolve(), 100); + }) + + const serialPort = new SerialPort({ path: pathObject.path, baudRate: 9600, autoOpen: false, }, function (err) { if (err) { return console.log('Error: ', err.message); } }) + console.log(111111, serialPort) + const buff = Buffer.allocUnsafe(4); buff.writeUInt8(`#0${req.params.id}$`); - serialport.write(buff) + serialPort.write(buff) } }) .catch((error) => {