Giter Site home page Giter Site logo

Comments (13)

dresende avatar dresende commented on May 27, 2024

If you're using a serial device..

const modbus = require("modbus");

modbus.serial.connect("/dev/ttyUSB0", { slaveId: 245 },(err, connection) => {
    // ...
    // connection.readCoils(...)
});

If you're using TCP...

const modbus = require("modbus");

modbus.tcp.connect("192.168.253.78", { unitId: 245 },(err, connection) => {
    // ...
    // connection.readCoils(...)
});

from stream.

Mohamedelhrichi avatar Mohamedelhrichi commented on May 27, 2024

tank you a lot for you answer.
I tested this but i have the same probleem when a add a salveid option

var modbus = require("modbus-stream");

 function readclient(ip) {
     modbus.tcp.connect(502, ip,{ slaveId: 245} , { debug: "automaton-2454" }, (err, connection) => {
console.log("connected")
connection.readCoils({ address: 0, quantity: 10 }, (err, res) => {
    if (err) throw err;

    console.log(res.response.data); // response
})
});
 }
 readclient("127.0.0.1") ;

i have receved this message

Error: GatewayTargetDeviceFailedToRespond
    at Object.exports.error (C:\Users\RAYAN\Desktop\mod\node_modules\modbus-pdu\lib\Exception.js:24:13)
    at Timeout._onTimeout (C:\Users\RAYAN\Desktop\mod\node_modules\modbus-stream\lib\transport\transport.js:93:33)
    at ontimeout (timers.js:482:11)
    at tryOnTimeout (timers.js:317:5)
    at Timer.listOnTimeout (timers.js:277:5) 

from stream.

dresende avatar dresende commented on May 27, 2024

slaveId does not exist in TCP, only unitId. I fixed my previous example.

from stream.

Mohamedelhrichi avatar Mohamedelhrichi commented on May 27, 2024

thank you for your answers i rectified my code but it's give the same error

var modbus = require("modbus-stream");
function readclient(ip) {
modbus.tcp.connect( 502, ip,{unitId : 245} ,
{ debug: "automaton-2454" }, (err, connection) => {
console.log("connected")
connection.readInputRegisters({ address: 0, quantity: 10 }, (err, res) => {
if (err) throw err;

console.log(res.response.data); // response

})
});
}

readclient("127.0.0.1") ;

Error: GatewayTargetDeviceFailedToRespond
at Object.exports.error (C:\Users\RAYAN\Desktop\mod\node_modules\modbus-pdu\lib\Exception.js:24:13)
at Timeout._onTimeout (C:\Users\RAYAN\Desktop\mod\node_modules\modbus-stream\lib\transport\transport.js:93:33)
at ontimeout (timers.js:482:11)
at tryOnTimeout (timers.js:317:5)
at Timer.listOnTimeout (timers.js:277:5)

from stream.

dresende avatar dresende commented on May 27, 2024

Do you have a modbus client on your computer? Are you sure it's on port 502 and responds to that request?

from stream.

Mohamedelhrichi avatar Mohamedelhrichi commented on May 27, 2024

i tested with to read my modbus client with node modbus stream ...doesnt work and its gives this error
GatewayPathUnavailable..
but with QmODMASTER it's works perfectely with no probelem
you can see picture
i think that the probelem is in {unitId : 245} parameter
You can see picture
error with node modbus stream
https://drive.google.com/file/d/1AgQh34inx2uvocBD500JRt4DupUii2rC/view?usp=sharing

result with QmODMASTER software
https://drive.google.com/file/d/1BqxfhrceQcQFziSajPmZBLikHdCKgKrX/view?usp=sharing

full error messagewith node modbus stream

$ node index.js
connected
2018-10-20T12:01:37.346Z << automaton-2454 0x[ 00, 01, 00, 00, 00, 06, 01, 04, 01, 03, 00, 0A ]
2018-10-20T12:01:37.441Z >> automaton-2454 0x[ 00, 01, 00, 00, 00, 03, 01, 84, 0A ]
events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: GatewayPathUnavailable
    at handle (C:\Users\1\Desktop\mod\node_modules\modbus-stream\lib\transport\transport.js:127:19)
    at Socket.<anonymous> (C:\Users\1\Desktop\mod\node_modules\modbus-stream\lib\transport\transport.js:183:3)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at Socket.Readable.push (_stream_readable.js:208:10)
    at TCP.onread (net.js:597:20)

1@1-PC MINGW32 ~/Desktop/mod (master)
$ node index.js
connected
2018-10-20T12:03:09.137Z << automaton-2454 0x[ 00, 01, 00, 00, 00, 06, 01, 04, 01, 03, 00, 0A ]
2018-10-20T12:03:09.199Z >> automaton-2454 0x[ 00, 01, 00, 00, 00, 03, 01, 84, 0A ]
events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: GatewayPathUnavailable
    at handle (C:\Users\1\Desktop\mod\node_modules\modbus-stream\lib\transport\transport.js:127:19)
    at Socket.<anonymous> (C:\Users\1\Desktop\mod\node_modules\modbus-stream\lib\transport\transport.js:183:3)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at Socket.Readable.push (_stream_readable.js:208:10)
    at TCP.onread (net.js:597:20)

from stream.

Mohamedelhrichi avatar Mohamedelhrichi commented on May 27, 2024

hello it's OK now it's worked with this code

//var modbus = require("modbus-stream");
const modbus = require("modbus-stream");
var fs = require('fs') ;

 var mydata ;
 var mydata2 ;
 var mydata3 ="bonjour" ; 

 function readclient(ip) {
   

    modbus.tcp.connect(ip, { unitId: 245 },(err, connection) => {
        // ...
        connection.readInputRegisters({ address: 259, quantity: 20 }, (err, res) => {
            if (err) throw err;
        
            console.log(res); 
            mydata = res.response.data ;
            // response
            mydata2=res.pdu ;
            
         //  mydata3 = mydata2.toString('utf8') ;
          //  var ret = mydata3.replace(/Buffer-/g,'');
         //   var ret2 = ret.replace(/<-/g,'');
          //  var ret3 = ret2.replace(/>-/g,'');
          //  mydata3 = ret3 ;
          //  console.log(mydata3) ;
            
      
        })
        });
 }

 //,{ slaveId: 1}
 readclient("10.80.15.62") ;

from stream.

Mohamedelhrichi avatar Mohamedelhrichi commented on May 27, 2024

i want to know how a can use buffer data and convert it to int array or decimal array

from stream.

dresende avatar dresende commented on May 27, 2024

Modbus does not define any types for data, you need to know how and what they have encoded. Show me the response (res or just res.response.data) and tell me what it should have there.

from stream.

Mohamedelhrichi avatar Mohamedelhrichi commented on May 27, 2024

ok thanks its OK a wondefull module thanks a lot

from stream.

sarat12 avatar sarat12 commented on May 27, 2024

Thanks for the decent peace of software! Is there any chance to change slaveId using the same connection? (I do polling on serial port and want to keep connection opened).

from stream.

sarat12 avatar sarat12 commented on May 27, 2024

Sorry for bothering, just figured that out:
connection.transport.slaveId = slaveId;

from stream.

dresende avatar dresende commented on May 27, 2024

😉

from stream.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.