ali-f 1 month ago
parent f74d79389d
commit dd893f21f9
  1. 4
      controller/serialPort.js
  2. 23
      services/serialPortService.js

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

@ -1,10 +1,23 @@
import { SerialPort } from 'serialport' 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) { async function serialPort(req, res) {
const buff = Buffer.allocUnsafe(4); const buff = Buffer.allocUnsafe(4);
@ -36,4 +49,4 @@ async function serialPort(req, res) {
// }) // })
} }
export default serialPort export default serialPortList
Loading…
Cancel
Save