Skip to content

Commit

Permalink
ServerSerial types and usage (#550)
Browse files Browse the repository at this point in the history
* ServerSerial types and emit initialized

* file

* better

* open callback

* openCallback on SerialPort open

* open on SerialPort

---------

Co-authored-by: cplepage <[email protected]>
  • Loading branch information
cplepage and cplepage authored Apr 4, 2024
1 parent 5357659 commit 49ecaf3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
21 changes: 21 additions & 0 deletions ServerSerial.d.ts
Original file line number Diff line number Diff line change
@@ -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<any> & {
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;
}
6 changes: 3 additions & 3 deletions ServerTCP.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>) => void) |
((addr: number, unitID: number) => Promise<boolean>) |
Expand Down Expand Up @@ -58,5 +58,5 @@ export declare interface ServerTCP {
on(event: 'initialized', listener: FCallback): this;
}

type FCallbackVal<T> = (err: Error | null, value: T) => void;
type FCallback = (err: Error | null) => void;
export type FCallbackVal<T> = (err: Error | null, value: T) => void;
export type FCallback = (err: Error | null) => void;
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {ModbusRTU} from "./ModbusRTU";
export * from "./ServerTCP";
export * from "./ServerSerial";

export default ModbusRTU;
5 changes: 3 additions & 2 deletions servers/serverserial.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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({
Expand All @@ -270,6 +270,7 @@ class ServerSerial extends EventEmitter {
modbus.socks.delete(modbus._server);
});

modbus.emit("initialized");
});

modbus._server.on("data", function(data) {
Expand Down

0 comments on commit 49ecaf3

Please sign in to comment.