ali-f 1 month 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()
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

@ -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) => {

Loading…
Cancel
Save