You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.5 KiB

2 months ago
import { SerialPort } from 'serialport'
4 weeks ago
import res from "express/lib/response.js";
import { autoDetect } from '@serialport/bindings-cpp'
2 months ago
4 weeks ago
async function serialPortList(req, res) {
4 weeks ago
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) {
if (err) {
return console.log('Error: ', err.message);
}
})
const buff = Buffer.allocUnsafe(4);
buff.writeUInt8(`#0${req.params.id}$`);
serialport.write(buff)
}
})
.catch((error) => {
console.error('Error listing serial ports:', error);
});
4 weeks ago
}
2 months ago
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);
// })
}
4 weeks ago
export default serialPortList