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