node-fxplc
Version:
Node.js library for low-level Mitsubishi FX (MELSEC) PLC framed protocol communication
119 lines (118 loc) • 4.38 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.TransportSerial = void 0;
// TransportSerial.js - Serial transport implementation using BufferAccumulator.
const errors_js_1 = require("./errors.js");
const transport_buffer_js_1 = require("./transport-buffer.js");
class TransportSerial {
/**
* @param {object} opts
* @param {string} opts.path Serial device path (e.g. COM3, /dev/ttyUSB0)
* @param {number} [opts.baudRate=9600] Baud rate
* @param {number} [opts.timeout=1000] Read timeout in ms
*/
constructor({ path, baudRate = 9600, timeout = 1000 }) {
this.path = path;
this.baudRate = baudRate;
this.timeout = timeout;
this._opened = false;
this._port = null;
this._bufferAcc = new transport_buffer_js_1.BufferAccumulator();
}
/** Lazily open the serial port */
async _ensureOpen() {
if (this._opened)
return;
let SerialPort;
try {
({ SerialPort } = await Promise.resolve().then(() => __importStar(require('serialport'))));
}
catch {
throw new Error("Dipendenza 'serialport' non installata o non disponibile");
}
await new Promise((resolve, reject) => {
const port = new SerialPort({
path: this.path,
baudRate: this.baudRate,
dataBits: 7,
stopBits: 1,
parity: 'even',
autoOpen: true,
lock: false,
}, err => { if (err)
return reject(err); resolve(); });
this._port = port;
port.on('data', d => this._bufferAcc.push(d));
port.on('error', () => this.close());
port.on('close', () => this._bufferAcc.close());
});
this._opened = true;
}
/** Write raw frame data; resets accumulator before each request */
async write(data) {
await this._ensureOpen();
if (!this._port)
throw new errors_js_1.NotConnectedError();
// reset buffer logico prima della richiesta
this._bufferAcc = new transport_buffer_js_1.BufferAccumulator();
await new Promise((resolve, reject) => this._port.write(data, err => err ? reject(err) : resolve()));
}
/** Read bytes from buffer accumulator */
async read(size) {
await this._ensureOpen();
if (!this._port)
throw new errors_js_1.NotConnectedError();
try {
return await this._bufferAcc.read(size, this.timeout);
}
catch {
throw new errors_js_1.NoResponseError();
}
}
/** Close port (idempotent) */
close() {
if (this._port) {
try {
this._port.close();
}
catch { /*noop*/ }
this._port = null;
}
this._opened = false;
this._bufferAcc.close();
}
}
exports.TransportSerial = TransportSerial;