ali-f 4 weeks ago
parent 96abffe191
commit 271ed8485c
  1. 5
      controller/serialPort.js
  2. 15
      services/serialPortService.js

@ -4,7 +4,8 @@ import serialPortList from '../services/serialPortService.js';
const router = new Router() const router = new Router()
router.get('/open/:id', serialPort); router.get('/open/:id', serialPortList);
router.get('/list', serialPortList); // router.get('/list/:id', serialPortList);
// router.get('/open/:id', serialPort);
export default router export default router

@ -8,19 +8,24 @@ import { autoDetect } from '@serialport/bindings-cpp'
async function serialPortList(req, res) { async function serialPortList(req, res) {
SerialPort.list() SerialPort.list()
.then((pathsList) => { .then(async pathsList => {
console.log('Available serial ports:'); for (const pathObject of pathsList) {
for (const path of pathsList) { await new Promise(resolve => {
const serialport = new SerialPort({ path, baudRate: 9600, autoOpen: false, }, function (err) { setTimeout(() => resolve(), 100);
})
const serialPort = new SerialPort({ path: pathObject.path, baudRate: 9600, autoOpen: false, }, function (err) {
if (err) { if (err) {
return console.log('Error: ', err.message); return console.log('Error: ', err.message);
} }
}) })
console.log(111111, serialPort)
const buff = Buffer.allocUnsafe(4); const buff = Buffer.allocUnsafe(4);
buff.writeUInt8(`#0${req.params.id}$`); buff.writeUInt8(`#0${req.params.id}$`);
serialport.write(buff) serialPort.write(buff)
} }
}) })
.catch((error) => { .catch((error) => {

Loading…
Cancel
Save