tuxnsk / nodejs_libmodbus

libmodbus binding for nodejs
41 stars 66 forks source link

Fixed problem with "response not from requested slave", Now "Connection timed out" ?! #23

Closed mrspicy closed 7 years ago

mrspicy commented 8 years ago

I'm working with nodejs_libmodbus in this szenario:

First I have had the following Problem: When I was trying to read registers from slave 1 while slave 2 was also turned on, I've got the error message "Response not from requested slave", although I've ensured that both slaves has got different slave id's. When slave 2 is turned off, the communication between the master and slave 1 works fine. When the master is trying to read the registers from slave 2, while slave 1 is turned on, no errors occurs.

Then I've found the reason for it! In the function "createSlaveRtu" in "modbus.js" the function "connect_async", written by tuxnsk, is called. Within these function the connection is established and the slave is listening for all request send on the modbus. But the slave never checks if the request sent by the master is for him or for an other slave. He always sends his data on the modbus by every (!) received request from the master. I've fixed this problem by adding the following if-statement in the line 847 of "modbus.js":

The receive function shall store the indication request in req and return the request length if sucessful. Otherwise it shall return -1 and set errno. (see the libmodbus documentation)

After adding this if-statement slave 2 can be turned on and the communication between slave 1 and the master works without any problems.

BUT NOW I've got another problem :( When I'm now trying to read registers from slave 2, the slave only is answering on every second request from the master. By reading 8 Registers with "getReg()" I only get the value of the registers 1, 3, 5, and 7 OR the value of registers 2, 4, 6 and 8 (don't know why it's sometimes 1,3,5,7 and other times it's 2,4,6,8). On the other half of requests I get the error message "Connection timed out".

I'm searching for hours what's the cause for this failure, but cannot find anything. Does anyone have an idea? Can anyone help me?

mrspicy commented 8 years ago

Hello together! I've read the modbus specification now. Thereby I've got the idea that too short response timeouts could cause the "connection timed out"-error. Because of the fact, that by using nodejs_libmodbus the modbus-master doesn't have the api to set response timeouts, I've changed my testing enviroments. I'm now using the npm package "modbus-rtu" to configurate my Raspberry Pi as a modbus master. With this package you can easily change the response-timeout. My modbus slave 2 is still the second raspberry pi with installed npm "modbus" to configurate it as a modbus slave.

I've tried various settings for the response timeout. However I've still got the same problem, that slave 2 only answers on every second request. As a conclusion of this tests I would say, that the response-timeout of the master isn't the reason for my described error. There must be a bug in npm "modbus" by tuxnsk, but I cannot find anything.

Furthermore I've ascertained that slave 2 only goes four times into the "recive"-function of "createSlaveRtu" in "modbus.js", although the master tries to read 8 seperate registers (why?). While the master reads out the 8 register from slave 1 (analog input module), slave 2 (the raspi) is going 8 times into the recive function, where it recognize that the request has an other slave id than itself and doesn't send a reply on the modbus (see my if-statement, described in my first comment). So:

Because of the fact that response timeout from the master doesn't cause the error. Do someone have any other ideas?