@@ -22,6 +22,15 @@ var crc16 = require("./utils/crc16");
2222var modbusSerialDebug = require ( "debug" ) ( "modbus-serial" ) ;
2323
2424var PORT_NOT_OPEN_MESSAGE = "Port Not Open" ;
25+ var PORT_NOT_OPEN_ERRNO = "ECONNREFUSED" ;
26+
27+ var PortNotOpenError = function ( ) {
28+ Error . captureStackTrace ( this , this . constructor ) ;
29+ this . name = this . constructor . name ;
30+ this . message = PORT_NOT_OPEN_MESSAGE ;
31+ this . errno = PORT_NOT_OPEN_ERRNO ;
32+ } ;
33+
2534/**
2635 * @fileoverview ModbusRTU module, exports the ModbusRTU class.
2736 * this class makes ModbusRTU calls fun and easy.
@@ -350,7 +359,7 @@ ModbusRTU.prototype.writeFC1 = function(address, dataAddress, length, next) {
350359ModbusRTU . prototype . writeFC2 = function ( address , dataAddress , length , next , code ) {
351360 // check port is actually open before attempting write
352361 if ( this . _port . isOpen ( ) === false ) {
353- if ( next ) next ( new Error ( PORT_NOT_OPEN_MESSAGE ) ) ;
362+ if ( next ) next ( new PortNotOpenError ( ) ) ;
354363 return ;
355364 }
356365
@@ -403,7 +412,7 @@ ModbusRTU.prototype.writeFC3 = function(address, dataAddress, length, next) {
403412ModbusRTU . prototype . writeFC4 = function ( address , dataAddress , length , next , code ) {
404413 // check port is actually open before attempting write
405414 if ( this . _port . isOpen ( ) === false ) {
406- if ( next ) next ( new Error ( PORT_NOT_OPEN_MESSAGE ) ) ;
415+ if ( next ) next ( new PortNotOpenError ( ) ) ;
407416 return ;
408417 }
409418
@@ -444,7 +453,7 @@ ModbusRTU.prototype.writeFC4 = function(address, dataAddress, length, next, code
444453ModbusRTU . prototype . writeFC5 = function ( address , dataAddress , state , next ) {
445454 // check port is actually open before attempting write
446455 if ( this . _port . isOpen ( ) === false ) {
447- if ( next ) next ( new Error ( PORT_NOT_OPEN_MESSAGE ) ) ;
456+ if ( next ) next ( new PortNotOpenError ( ) ) ;
448457 return ;
449458 }
450459
@@ -489,7 +498,7 @@ ModbusRTU.prototype.writeFC5 = function(address, dataAddress, state, next) {
489498ModbusRTU . prototype . writeFC6 = function ( address , dataAddress , value , next ) {
490499 // check port is actually open before attempting write
491500 if ( this . _port . isOpen ( ) === false ) {
492- if ( next ) next ( new Error ( PORT_NOT_OPEN_MESSAGE ) ) ;
501+ if ( next ) next ( new PortNotOpenError ( ) ) ;
493502 return ;
494503 }
495504
@@ -530,7 +539,7 @@ ModbusRTU.prototype.writeFC6 = function(address, dataAddress, value, next) {
530539ModbusRTU . prototype . writeFC15 = function ( address , dataAddress , array , next ) {
531540 // check port is actually open before attempting write
532541 if ( this . _port . isOpen ( ) === false ) {
533- if ( next ) next ( new Error ( PORT_NOT_OPEN_MESSAGE ) ) ;
542+ if ( next ) next ( new PortNotOpenError ( ) ) ;
534543 return ;
535544 }
536545
@@ -586,7 +595,7 @@ ModbusRTU.prototype.writeFC15 = function(address, dataAddress, array, next) {
586595ModbusRTU . prototype . writeFC16 = function ( address , dataAddress , array , next ) {
587596 // check port is actually open before attempting write
588597 if ( this . _port . isOpen ( ) === false ) {
589- if ( next ) next ( new Error ( PORT_NOT_OPEN_MESSAGE ) ) ;
598+ if ( next ) next ( new PortNotOpenError ( ) ) ;
590599 return ;
591600 }
592601
@@ -629,7 +638,9 @@ require("./apis/promise")(ModbusRTU);
629638// exports
630639module . exports = ModbusRTU ;
631640module . exports . TestPort = require ( "./ports/testport" ) ;
632- module . exports . RTUBufferedPort = require ( "./ports/rtubufferedport" ) ;
641+ try {
642+ module . exports . RTUBufferedPort = require ( "./ports/rtubufferedport" ) ;
643+ } catch ( err ) { }
633644module . exports . TcpPort = require ( "./ports/tcpport" ) ;
634645module . exports . TcpRTUBufferedPort = require ( "./ports/tcprtubufferedport" ) ;
635646module . exports . TelnetPort = require ( "./ports/telnetport" ) ;
0 commit comments