File tree Expand file tree Collapse file tree 4 files changed +28
-5
lines changed
Expand file tree Collapse file tree 4 files changed +28
-5
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff line change 11import { ModbusRTU } from "./ModbusRTU" ;
22export * from "./ServerTCP" ;
3+ export * from "./ServerSerial" ;
34
45export default ModbusRTU ;
Original file line number Diff line number Diff 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 ) {
You can’t perform that action at this time.
0 commit comments