Cloud-Automation / node-modbus

Modbus TCP Client/Server implementation for Node.JS
471 stars 175 forks source link

It didn't connect the server.I need help! #111

Closed panyunyi closed 7 years ago

panyunyi commented 7 years ago

Hello,sorry to bother you! I really need some help. image

modscan32 is worked in this config.But jsmodbus didn't work.

panyunyi commented 7 years ago

image

stefanpoeter commented 7 years ago

Are you sure you connect to the right port and ip address? The timeout can be from the tcp connection since you have set auto reconnect to true.

panyunyi commented 7 years ago

Yes,I'm sure connect to the right port and ip address.The first picture is modscan.They have the same config. image image

stefanpoeter commented 7 years ago

Can you post the code here, so that I can reproduce.

panyunyi commented 7 years ago

var modbus = require('jsmodbus');

// create a modbus client var client = modbus.client.tcp.complete({ 'host' : '192.168.1.8', 'port' : 10000, 'autoReconnect' : false, 'reconnectTimeout' : 1000, 'timeout' : 5000, 'unitId' : 1 });

client.connect();

// reconnect with client.reconnect()

client.on('connect', function () {

client.readInputRegisters(2, 10).then(function (resp) {

    // resp will look like { fc: 4, byteCount: 20, register: [ values 0 - 10 ], payload: <Buffer> }
    console.log(resp);

}, console.error);

});

client.on('error', function (err) {

console.log(err);

})

stefanpoeter commented 7 years ago

The best way to solve these kind of problems is to do it step by step. Try the code below. There are no modbus requests, just the connection. I would bet that this fails. If so, check your server and firewall settings.

var modbus = require('jsmodbus');

// create a modbus client
var client = modbus.client.tcp.complete({
    'host' : '192.168.1.8',
    'port' : 10000,
    'autoReconnect' : false,
    'reconnectTimeout' : 1000,
    'timeout' : 5000,
    'unitId' : 1
});

client.connect();

client.on('connect', function () {
    console.log("Connected!");
});

client.on('error', function (err) {
    console.log("Connection error!", err);
})
panyunyi commented 7 years ago

OK!Thank you for your patience.I will try.

panyunyi commented 7 years ago

Hi...It's connected.But the readInputRegisters didn't work.I think it maybe the response code is wrong. image image The right code is: 0000 0000 0006 0104 0001 0014 How should I modify the demo code to success?

panyunyi commented 7 years ago

image It's worked!Thank you!