@@ -17,32 +17,59 @@ class TcpPort extends EventEmitter {
1717 /**
1818 * Simulate a modbus-RTU port using modbus-TCP connection.
1919 *
20- * @param ip
21- * @param options
20+ * @param {string } ip - IP address of Modbus slave.
21+ * @param {{
22+ * port?: number,
23+ * localAddress?: string,
24+ * family?: 0|4|6,
25+ * timeout?: number,
26+ * socket?: net.Socket
27+ * socketOpts?: {
28+ * fd: number,
29+ * allowHalfOpen?: boolean,
30+ * readable?: boolean,
31+ * writable?: boolean,
32+ * signal?: AbortSignal
33+ * },
34+ * } & net.TcpSocketConnectOpts } options - Options object.
2235 * options.port: Nonstandard Modbus port (default is 502).
2336 * options.localAddress: Local IP address to bind to, default is any.
2437 * options.family: 4 = IPv4-only, 6 = IPv6-only, 0 = either (default).
2538 * @constructor
2639 */
2740 constructor ( ip , options ) {
2841 super ( ) ;
29- const modbus = this ;
42+ const self = this ;
43+ /** @type {boolean } Flag to indicate if port is open */
3044 this . openFlag = false ;
45+ /** @type {(err?: Error) => void } */
3146 this . callback = null ;
3247 this . _transactionIdWrite = 1 ;
48+ /** @type {net.Socket? } - Optional custom socket */
3349 this . _externalSocket = null ;
3450
3551 if ( typeof ip === "object" ) {
3652 options = ip ;
53+ ip = undefined ;
3754 }
3855
39- if ( typeof ( options ) === "undefined" ) options = { } ;
56+ if ( typeof options === "undefined" ) options = { } ;
4057
41- this . connectOptions = {
42- host : ip || options . ip ,
43- port : options . port || MODBUS_PORT ,
44- localAddress : options . localAddress ,
45- family : options . family
58+ this . socketOpts = undefined ;
59+ if ( options . socketOpts ) {
60+ this . socketOpts = options . socketOpts ;
61+ delete options . socketOpts ;
62+ }
63+
64+ /** @type {net.TcpSocketConnectOpts } - Options for net.connect(). */
65+ this . connectOptions = {
66+ // Default options
67+ ...{
68+ host : ip || options . ip ,
69+ port : MODBUS_PORT
70+ } ,
71+ // User options
72+ ...options
4673 } ;
4774
4875 if ( options . socket ) {
@@ -55,18 +82,20 @@ class TcpPort extends EventEmitter {
5582 }
5683
5784 // handle callback - call a callback function only once, for the first event
58- // it will triger
85+ // it will trigger
5986 const handleCallback = function ( had_error ) {
60- if ( modbus . callback ) {
61- modbus . callback ( had_error ) ;
62- modbus . callback = null ;
87+ if ( self . callback ) {
88+ self . callback ( had_error ) ;
89+ self . callback = null ;
6390 }
6491 } ;
6592
6693 // init a socket
67- this . _client = this . _externalSocket || new net . Socket ( ) ;
94+ this . _client = this . _externalSocket || new net . Socket ( this . socketOpts ) ;
6895
6996 if ( options . timeout ) this . _client . setTimeout ( options . timeout ) ;
97+
98+ // register events handlers
7099 this . _client . on ( "data" , function ( data ) {
71100 let buffer ;
72101 let crc ;
@@ -89,34 +118,34 @@ class TcpPort extends EventEmitter {
89118 buffer . writeUInt16LE ( crc , buffer . length - CRC_LENGTH ) ;
90119
91120 // update transaction id and emit data
92- modbus . _transactionIdRead = data . readUInt16BE ( 0 ) ;
93- modbus . emit ( "data" , buffer ) ;
121+ self . _transactionIdRead = data . readUInt16BE ( 0 ) ;
122+ self . emit ( "data" , buffer ) ;
94123
95124 // debug
96- modbusSerialDebug ( { action : "parsed tcp port" , buffer : buffer , transactionId : modbus . _transactionIdRead } ) ;
125+ modbusSerialDebug ( { action : "parsed tcp port" , buffer : buffer , transactionId : self . _transactionIdRead } ) ;
97126
98127 // reset data
99128 data = data . slice ( length + MIN_MBAP_LENGTH ) ;
100129 }
101130 } ) ;
102131
103132 this . _client . on ( "connect" , function ( ) {
104- modbus . openFlag = true ;
133+ self . openFlag = true ;
105134 modbusSerialDebug ( "TCP port: signal connect" ) ;
106135 handleCallback ( ) ;
107136 } ) ;
108137
109138 this . _client . on ( "close" , function ( had_error ) {
110- modbus . openFlag = false ;
139+ self . openFlag = false ;
111140 modbusSerialDebug ( "TCP port: signal close: " + had_error ) ;
112141 handleCallback ( had_error ) ;
113142
114- modbus . emit ( "close" ) ;
115- modbus . removeAllListeners ( ) ;
143+ self . emit ( "close" ) ;
144+ self . removeAllListeners ( ) ;
116145 } ) ;
117146
118147 this . _client . on ( "error" , function ( had_error ) {
119- modbus . openFlag = false ;
148+ self . openFlag = false ;
120149 modbusSerialDebug ( "TCP port: signal error: " + had_error ) ;
121150 handleCallback ( had_error ) ;
122151 } ) ;
@@ -142,7 +171,7 @@ class TcpPort extends EventEmitter {
142171 /**
143172 * Simulate successful port open.
144173 *
145- * @param callback
174+ * @param { (err?: Error) => void } callback
146175 */
147176 open ( callback ) {
148177 if ( this . _externalSocket === null ) {
@@ -159,7 +188,7 @@ class TcpPort extends EventEmitter {
159188 /**
160189 * Simulate successful close port.
161190 *
162- * @param callback
191+ * @param { (err?: Error) => void } callback
163192 */
164193 close ( callback ) {
165194 this . callback = callback ;
@@ -170,7 +199,7 @@ class TcpPort extends EventEmitter {
170199 /**
171200 * Simulate successful destroy port.
172201 *
173- * @param callback
202+ * @param { (err?: Error) => void } callback
174203 */
175204 destroy ( callback ) {
176205 this . callback = callback ;
@@ -182,7 +211,7 @@ class TcpPort extends EventEmitter {
182211 /**
183212 * Send data to a modbus-tcp slave.
184213 *
185- * @param data
214+ * @param { Buffer } data
186215 */
187216 write ( data ) {
188217 if ( data . length < MIN_DATA_LENGTH ) {
0 commit comments