|
|
|
import { SerialPort } from 'serialport'
|
|
|
|
import res from "express/lib/response.js";
|
|
|
|
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);
|
|
|
|
// });
|
|
|
|
}
|
|
|
|
|
|
|
|
async function serialPort(req, res) {
|
|
|
|
const buff = Buffer.allocUnsafe(4);
|
|
|
|
buff.writeUInt8(`#0${req.params.id}$`);
|
|
|
|
|
|
|
|
const result = await serialport.write(buff)
|
|
|
|
|
|
|
|
res.send(result)
|
|
|
|
|
|
|
|
// serialport.open(function (err) {
|
|
|
|
// if (err) {
|
|
|
|
// return console.log('Error opening port: ', err.message);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // Because there's no callback to write, write errors will be emitted on the port:
|
|
|
|
// serialport.write('main screen turn on');
|
|
|
|
// })
|
|
|
|
|
|
|
|
// serialport.on('error', function (err) {
|
|
|
|
// console.log('Error: ', err.message);
|
|
|
|
// })
|
|
|
|
|
|
|
|
// serialport.on('readable', function () {
|
|
|
|
// console.log('Data:', serialport.read());
|
|
|
|
// })
|
|
|
|
|
|
|
|
// serialport.on('data', function (data) {
|
|
|
|
// console.log('Data:', data);
|
|
|
|
// })
|
|
|
|
}
|
|
|
|
|
|
|
|
export default serialPortList
|