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
#5
I have implemented a Separate write and read function now,  

For the most part it is working well however I do get an issue occurring a few times a day where i fail to get a read back from some random register,  almost never the same regitser. 

When this occurs it was generally forcing my script to fail as i would attempt to do a calculation on a vil value as res from  res,err mb:readregistervalue(_address, _type, _mbOrder).  will return a nil for res,  

TO try and resolve this i wrote some extra code to retry see below

Code:
function tryReadRegister(_Address, _Type, _Oder)
local _Count = 0
local _Result
repeat
   _Count = _Count + 1 
  _Result, err = mb:readregistervalue(_Address, _Type, _Oder)
      if err then
       alert("Modbus Read Error of Type "..tostring(err).."For readregistervalue at Adress "..tostring(_Address).."For Data Type "..tostring(_Type)" and Order"..tostring(_Oder).." Attempt "..tostring(_Count).." Of 3")
         sleep(0.3)
      end
until ((_Count ==3) or _Result)
if not _Result then
            alert("Modbus Read Returned no Result Forced to 0")
      return 0
   else
      return _Result
   end
end

I chose for now to return a 0 to stop the script from failing and to try ID the cause. 

I also tried to improve the situation by delaying the read attempts in between by 300ms (i assumed it possible to put delays inside functions ?

Anyway turns out when the error occurs it is not improved by trying the same again with with a delay.

But the error returned on fail is  "method called on invalid context"  I have looked this up on the forum to find this may have something to do with float32 where the solution was for a profile.  to add "Value send delta"

The script above has been modified with the message to confirm that the failure is on random register address yes, but always on a float32.   I will run it again an wait to get a result. 


But in mean time any support on what could be the issue here would be helpful.
Reply


Messages In This Thread
RE: Modbus TCP Writes Fail after Reads - by merlin - 18.09.2022, 07:45

Forum Jump: