@@ -43,35 +43,42 @@ var modbushandler = {
4343 ReadValue : function ( name ) {
4444 //console.log("read ", this.ValueMap);
4545 var val = this . ValueMap [ name ] ;
46+ let statusCode ;
4647 if ( ! val ) {
47- return opcua . StatusCodes . BadDataUnavailable ;
48+ statusCode = opcua . StatusCodes . BadDataUnavailable ;
4849 }
49- if ( val . q != "good" ) {
50- return opcua . StatusCodes . BadConnectionRejected ; //Bad;
50+ if ( val ?. q != "good" ) {
51+ switch ( val ?. q ) {
52+ case "BadNotConnected" :
53+ statusCode = opcua . StatusCodes . BadNotConnected ;
54+ break ;
55+ case "BadCommunicationError" :
56+ default :
57+ statusCode = opcua . StatusCodes . BadCommunicationError ;
58+ break ;
59+
60+ }
5161 }
52- return val . v ;
62+ return new opcua . DataValue ( { "value" : val ?. v , "statusCode" : statusCode , "sourceTimestamp" : new Date ( ) } ) ;
5363 } ,
54- WriteValue : function ( type , address , variant ) {
55- switch ( type ) {
56- case "holdingregister" :
57- var value = parseInt ( variant . value ) ;
58- this . modbusclient . writeSingleRegister ( address , value ) . then ( function ( resp ) {
59-
60- // resp will look like { fc: 6, byteCount: 4, registerAddress: 13, registerValue: 42 }
61- console . log ( "Writing to holding register address: " + resp . registerAddress + " value: " , resp . registerValue ) ;
62-
63- } ) . fail ( console . log ) ;
64- break ;
65- case "coils" :
66- var value = ( ( variant . value ) === 'true' ) ;
67- this . modbusclient . writeSingleCoil ( address , value ) . then ( function ( resp ) {
68-
69- // resp will look like { fc: 5, byteCount: 4, outputAddress: 5, outputValue: true }
70- console . log ( "Writing to coil address: " + resp . outputAddress + " value: " + resp . outputValue ) ;
71-
72- } ) . fail ( console . log ) ;
73- break ;
64+ WriteValue : async function ( type , address , variant ) {
65+ try {
66+ switch ( type ) {
67+ case "holdingregister" : {
68+ var value = parseInt ( variant . value ) ;
69+ let resp = await this . modbusclient . writeSingleRegister ( address , value ) ;
70+ return true ;
71+ }
72+ case "coils" : {
73+ var value = ( ( variant . value ) === 'true' ) ;
74+ let resp = await this . modbusclient . writeSingleCoil ( address , value ) ;
75+ return true ;
76+ }
77+ }
78+ } catch ( er ) {
79+ console . error ( 'unable to write %s' , address , er ) ;
7480 }
81+ return false ;
7582 } ,
7683 CreateModbusDevice : function ( host , port , unit ) {
7784 this . socket = new net . Socket ( ) ;
@@ -88,17 +95,17 @@ var modbushandler = {
8895 let recon = new Reconnect ( this . socket , options ) ;
8996 this . socket . connect ( options ) ;
9097
91- console . log ( "Created a Modbus device on %s:%d %s" , host , port , unit ) ;
98+ console . log ( "Created a Modbus device on %s:%d %s" , host , port , unit ) ;
9299 this . modbusclient = mclient ;
93100 let connectionState = false ;
94101 this . socket . on ( 'error' , ( ) => {
95102 if ( connectionState )
96- console . warn ( "Lost connection to Modbus device on %s:%d %s" , host , port , unit ) ;
103+ console . warn ( "Lost connection to Modbus device on %s:%d %s" , host , port , unit ) ;
97104 connectionState = false ;
98105 } ) ;
99106 this . socket . on ( 'connect' , ( ) => {
100107 if ( ! connectionState ) {
101- console . info ( "Connection established to Modbus device on %s:%d %s" , host , port , unit ) ;
108+ console . info ( "Connection established to Modbus device on %s:%d %s" , host , port , unit ) ;
102109 }
103110 connectionState = true ;
104111 } ) ;
0 commit comments