Nodejs Serialport get Path From SerialNumber or PNPID

62 Views Asked by At

Im trying to make Serialport automatically find the Port depending on a set Serialnumber.

Cant really figure out a simple Solution, everythign would involve wrapping everything in 20 Async await's.

let SELECTED_PORT = "";
SerialPort.list().then((ports) => {
    console.log("PORTS", ports)
    ports.forEach((port) => {
        console.log(port)
        if (port.serialNumber == SERIALNUMBER) {
            console.log("FOUND PORT", port.path);
            SELECTED_PORT = port.path;
        }
    });
});

//

console.log("SELECTED PORT", SELECTED_PORT)

const port = new SerialPort({
    path: SELECTED_PORT,
    baudRate: 19200,
})

something like that would work, but the new Serialport runs before the list is done. I'm sure theres a simple solution, i just cant get it.

0

There are 0 best solutions below