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.
74 lines
2.0 KiB
74 lines
2.0 KiB
import { SerialPort } from 'serialport' |
|
import { autoDetect } from '@serialport/bindings-cpp' |
|
|
|
async function serialPortList(req, res) { |
|
|
|
SerialPort.list() |
|
.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.write(`#0${req.params.id}$`); |
|
|
|
serialPort.on('open', () => { |
|
console.log('Serial port opened'); |
|
serialPort.write(buff) |
|
}); |
|
|
|
serialPort.on('data', (data) => { |
|
console.log('Received data:', data); |
|
// Process the response to check if the box was opened successfully |
|
}); |
|
|
|
serialPort.on('error', (error) => { |
|
console.error('Error:', error); |
|
}); |
|
} |
|
}) |
|
.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 |