Answer the question
In order to leave comments, you need to log in
How to reinitialize in node-serialport after an error accessing the COM port?
The application works as a web server. When accessing the web server address, a command is sent to the com port and the data in the response is transmitted to the client. But when the connection with the equipment is interrupted / the equipment is turned off, the application, after a certain timeout, writes an error. But when you re-access, after the connection is restored, the equipment also continues to write a communication error. How to do a reinitialization so that it re-applies to the com port?
const express = require('express');
const app = express();
const SerialPort = require('serialport');
app.get('/', (req, res) => {
let comPort = req.query.comPort
const port = new SerialPort('COM' + comPort, {
baudRate: 2400,
dataBits: 7,
parity: 'even'
});
const Readline = SerialPort.parsers.Readline;
const parser = new Readline();
port.pipe(parser);
port.on('open', () => {
port.write("Q\r\n", (err) => {
if (err) {
return console.log('Error on write: ', err.message);
}
})
parser.on('data', (data) => {
console.log('data: ' + data)
port.close()
})
});
// open errors will be emitted as an error event
port.on('error', (err) => {
console.log('Error: ', err.message);
res.status(500).send('Error: ' + err.message)
});
port.on('close', () => {
console.log('closed');
res.json(user_data)
});
});
app.listen(3000, () => {
console.log('Example app listening on port 3000!');
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question