Initial push
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
// server/serial.js
|
||||
const { SerialPort } = require('serialport');
|
||||
const { ReadlineParser } = require('@serialport/parser-readline');
|
||||
|
||||
let portInstance = null;
|
||||
let parser = null;
|
||||
let latestPayload = null;
|
||||
let latestImage = null;
|
||||
|
||||
function connectSerial(path) {
|
||||
if (portInstance?.isOpen) portInstance.close();
|
||||
|
||||
portInstance = new SerialPort({ path: path, baudRate: 115200 });
|
||||
parser = portInstance.pipe(new ReadlineParser({ delimiter: '\n' }));
|
||||
parser.on('data', line => {
|
||||
if(line.includes("image")){
|
||||
if(latestImage != null && !latestImage.endsWith("}"))
|
||||
latestImage += line.trim().replace("\n", "");
|
||||
else
|
||||
latestImage = line.trim().replace("\n", "");
|
||||
console.log(latestImage)
|
||||
}
|
||||
else if (line.includes("text")){
|
||||
console.log(latestPayload)
|
||||
latestPayload = line.trim();
|
||||
}
|
||||
});
|
||||
portInstance.on('error', console.error);
|
||||
}
|
||||
|
||||
function listSerialPorts() {
|
||||
return SerialPort.list();
|
||||
}
|
||||
|
||||
function getLatestPacket() {
|
||||
return [latestPayload, latestImage];
|
||||
}
|
||||
|
||||
module.exports = { connectSerial, listSerialPorts, getLatestPacket };
|
||||
Reference in New Issue
Block a user