From 96abffe191cf0cfeb8bc5ca7971d5db14e966793 Mon Sep 17 00:00:00 2001 From: ali-f Date: Thu, 17 Oct 2024 18:48:19 +0330 Subject: [PATCH] 1 --- services/serialPortService.js | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/services/serialPortService.js b/services/serialPortService.js index 7eaeeb4..eef5eb1 100644 --- a/services/serialPortService.js +++ b/services/serialPortService.js @@ -6,18 +6,26 @@ import { autoDetect } from '@serialport/bindings-cpp' async function serialPortList(req, res) { - const portList = await SerialPort.list() - console.log(portList) - res.send(portList) - // .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); - // }); + + 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) { + if (err) { + return console.log('Error: ', err.message); + } + }) + + const buff = Buffer.allocUnsafe(4); + buff.writeUInt8(`#0${req.params.id}$`); + + serialport.write(buff) + } + }) + .catch((error) => { + console.error('Error listing serial ports:', error); + }); } async function serialPort(req, res) {