From 49ecaf3caf93dfedf1dab19b2dec01de07aabe27 Mon Sep 17 00:00:00 2001 From: CP Lepage <32472542+cplepage@users.noreply.github.com> Date: Thu, 4 Apr 2024 14:17:24 -0400 Subject: [PATCH] ServerSerial types and usage (#550) * ServerSerial types and emit initialized * file * better * open callback * openCallback on SerialPort open * open on SerialPort --------- Co-authored-by: cplepage --- ServerSerial.d.ts | 21 +++++++++++++++++++++ ServerTCP.d.ts | 6 +++--- index.d.ts | 1 + servers/serverserial.js | 5 +++-- 4 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 ServerSerial.d.ts diff --git a/ServerSerial.d.ts b/ServerSerial.d.ts new file mode 100644 index 0000000..874e6c8 --- /dev/null +++ b/ServerSerial.d.ts @@ -0,0 +1,21 @@ +import * as events from 'events'; +import { SerialPortOpenOptions } from "serialport" +import { FCallback, IServiceVector } from './ServerTCP'; +import { ErrorCallback } from '@serialport/stream'; + +export class ServerSerial extends events.EventEmitter { + constructor(vector: IServiceVector, options: IServerSerialOptions); + close(cb: FCallback): void; +} + +type IServerSerialOptions = SerialPortOpenOptions & { + debug?: boolean, + unitID?: number, + openCallback?: ErrorCallback +} + +export declare interface ServerSerial { + on(event: 'socketError', listener: FCallback): this; + on(event: 'error', listener: FCallback): this; + on(event: 'initialized', listener: FCallback): this; +} diff --git a/ServerTCP.d.ts b/ServerTCP.d.ts index 64b411c..1d86f55 100644 --- a/ServerTCP.d.ts +++ b/ServerTCP.d.ts @@ -5,7 +5,7 @@ export class ServerTCP extends events.EventEmitter { close(cb: FCallback): void; } -interface IServiceVector { +export interface IServiceVector { getCoil?: ((addr: number, unitID: number, cb: FCallbackVal) => void) | ((addr: number, unitID: number) => Promise) | @@ -58,5 +58,5 @@ export declare interface ServerTCP { on(event: 'initialized', listener: FCallback): this; } -type FCallbackVal = (err: Error | null, value: T) => void; -type FCallback = (err: Error | null) => void; +export type FCallbackVal = (err: Error | null, value: T) => void; +export type FCallback = (err: Error | null) => void; diff --git a/index.d.ts b/index.d.ts index d29be8f..f88a711 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,4 +1,5 @@ import {ModbusRTU} from "./ModbusRTU"; export * from "./ServerTCP"; +export * from "./ServerSerial"; export default ModbusRTU; \ No newline at end of file diff --git a/servers/serverserial.js b/servers/serverserial.js index 81ca1c1..e49b48d 100644 --- a/servers/serverserial.js +++ b/servers/serverserial.js @@ -237,7 +237,7 @@ class ServerSerial extends EventEmitter { const optionsWithBindingandSerialport = Object.assign({}, serialportOptions, optionsWithBinding); // create a serial server - modbus._serverPath = new SerialPort(optionsWithBindingandSerialport); + modbus._serverPath = new SerialPort(optionsWithBindingandSerialport, options.openCallback); // create a serial server with a timeout parser modbus._server = modbus._serverPath.pipe(new ServerSerialPipeHandler(optionsWithSerialPortTimeoutParser)); @@ -253,7 +253,7 @@ class ServerSerial extends EventEmitter { // remember open sockets modbus.socks = new Map(); - modbus._server.on("open", function() { + modbus._serverPath.on("open", function() { modbus.socks.set(modbus._server, 0); modbusSerialDebug({ @@ -270,6 +270,7 @@ class ServerSerial extends EventEmitter { modbus.socks.delete(modbus._server); }); + modbus.emit("initialized"); }); modbus._server.on("data", function(data) {