Skip to content

Commit 49ecaf3

Browse files
cplepagecplepage
and
cplepage
authored
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 <[email protected]>
1 parent 5357659 commit 49ecaf3

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

ServerSerial.d.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as events from 'events';
2+
import { SerialPortOpenOptions } from "serialport"
3+
import { FCallback, IServiceVector } from './ServerTCP';
4+
import { ErrorCallback } from '@serialport/stream';
5+
6+
export class ServerSerial extends events.EventEmitter {
7+
constructor(vector: IServiceVector, options: IServerSerialOptions);
8+
close(cb: FCallback): void;
9+
}
10+
11+
type IServerSerialOptions = SerialPortOpenOptions<any> & {
12+
debug?: boolean,
13+
unitID?: number,
14+
openCallback?: ErrorCallback
15+
}
16+
17+
export declare interface ServerSerial {
18+
on(event: 'socketError', listener: FCallback): this;
19+
on(event: 'error', listener: FCallback): this;
20+
on(event: 'initialized', listener: FCallback): this;
21+
}

ServerTCP.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export class ServerTCP extends events.EventEmitter {
55
close(cb: FCallback): void;
66
}
77

8-
interface IServiceVector {
8+
export interface IServiceVector {
99
getCoil?:
1010
((addr: number, unitID: number, cb: FCallbackVal<boolean>) => void) |
1111
((addr: number, unitID: number) => Promise<boolean>) |
@@ -58,5 +58,5 @@ export declare interface ServerTCP {
5858
on(event: 'initialized', listener: FCallback): this;
5959
}
6060

61-
type FCallbackVal<T> = (err: Error | null, value: T) => void;
62-
type FCallback = (err: Error | null) => void;
61+
export type FCallbackVal<T> = (err: Error | null, value: T) => void;
62+
export type FCallback = (err: Error | null) => void;

index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {ModbusRTU} from "./ModbusRTU";
22
export * from "./ServerTCP";
3+
export * from "./ServerSerial";
34

45
export default ModbusRTU;

servers/serverserial.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ class ServerSerial extends EventEmitter {
237237
const optionsWithBindingandSerialport = Object.assign({}, serialportOptions, optionsWithBinding);
238238

239239
// create a serial server
240-
modbus._serverPath = new SerialPort(optionsWithBindingandSerialport);
240+
modbus._serverPath = new SerialPort(optionsWithBindingandSerialport, options.openCallback);
241241

242242
// create a serial server with a timeout parser
243243
modbus._server = modbus._serverPath.pipe(new ServerSerialPipeHandler(optionsWithSerialPortTimeoutParser));
@@ -253,7 +253,7 @@ class ServerSerial extends EventEmitter {
253253
// remember open sockets
254254
modbus.socks = new Map();
255255

256-
modbus._server.on("open", function() {
256+
modbus._serverPath.on("open", function() {
257257
modbus.socks.set(modbus._server, 0);
258258

259259
modbusSerialDebug({
@@ -270,6 +270,7 @@ class ServerSerial extends EventEmitter {
270270
modbus.socks.delete(modbus._server);
271271
});
272272

273+
modbus.emit("initialized");
273274
});
274275

275276
modbus._server.on("data", function(data) {

0 commit comments

Comments
 (0)