Files
xOld_appRoboticsDriver/robot/ComSender.js

40 lines
1.0 KiB
JavaScript
Executable File

// comSender.js
const { SerialPort } = require('serialport');
module.exports = class ComSender {
constructor(comPort = '/dev/ttyS0', baudRate = 115200) {
this.comPort = comPort;
this.baudRate = baudRate;
this.port = null;
}
async init() {
return new Promise((resolve, reject) => {
this.port = new SerialPort(
{
path: this.comPort,
baudRate: this.baudRate,
dataBits: 8,
stopBits: 1,
parity: 'none',
autoOpen: true,
},
(err) => (err ? reject(err) : resolve())
);
});
}
moveTo(mOld, mNew) {
if (!this.port || !this.port.isOpen) {
console.error('Serial port not open. Call init() first.');
return;
}
const msg = 'Hello from Node at ' + this.baudRate + ' baud\r\n';
this.port.write(msg, (err) => {
if (err) return console.error('Write error:', err);
this.port.drain(() => console.log('Wrote to ' + this.comPort));
});
}
}