This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Modbus TCP Writes Fail after Reads
#1
Hi 

I am having some frustrating issues in determining what is happening with regards to writes failing after a previous read has occurred when using Lua Modbus. 

A Description of what is occuring. 

Using Lua a connection is opened and maintained to the modbus tcp slave. 

Values are first read from the device to determine its current state.  All read results are working fine with correct values returned from the device. 
Depending on values read a new value is written to the unit. and then the previous values are read again to verify the change has occurred.  

When run in this format - after the write registers occurs,  the logged result is 

arg: 1
nil
arg: 2
string: Operation timed out


and then the following reads  of values also return a Nil.  where they previously returned a values prior to write. 

The interesting result is if closing the connection and re-opening it, the value that was attempted to be written was successful.  as reading again on a new connection works just fine.

note i have tried using delays (sleep(1)) as example between each item it has made no impact. 

Code:
require('luamodbus')

mb = luamodbus.tcp()
mb:open('192.168.1.175', 1501)
res, err = mb:connect()
if res then
  log("connected!")
  mb:setslave(1)
local r6 = mb:readregistervalue(0xE004, "uint16", "n")
log('Mode = '.. tostring(r6))
    r6 = mb:readregistervalue(0xE008, "float32", "n")
log('Backup= '.. tostring(r6))
r6 = mb:readregistervalue(0xE00A, "uint16", "n")
log('Default = '.. tostring(r6))
  if (r6 == 1) then
res,err = mb:writeregisters(0xE00A, 7)
log(res,err)
      end
r6 = mb:readregistervalue(0xE004, "uint16", "n")
log('Mode = '.. tostring(r6))
r6 = mb:readregistervalue(0xE008, "float32", "n")
log('Backup = '.. tostring(r6))
  

else
  log('connect failed', err)
end

mb:close()


 
I have also Verified, that if  the only action is to do a connection and write it works correctly.

Code:
mb = luamodbus.tcp()
mb:open('192.168.1.175', 1501)
res, err = mb:connect()
if res then
res,err = mb:writeregisters(0xE00A, 7)
log(res,err)
else
  log('connect failed', err)
end



returns 
arg: 1
  number: 1
  arg: 2
  nil


Can anyone provide some assistance as to why this issue is occurring after a write event only when inside a single connection.  

I have thought that it might be just some form of time out,  but currently i can not find any documentation on luamodbus to increase timeout for write and read
Reply


Messages In This Thread
Modbus TCP Writes Fail after Reads - by merlin - 11.09.2022, 06:32

Forum Jump: