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.

Universal Modbus TCP/RTU Slave
#21
Yes this is modbus TCP
------------------------------
Ctrl+F5
Reply
#22
Try changing [1] in line 10 to [*] it will be any slave nr.
------------------------------
Ctrl+F5
Reply
#23
(06.03.2023, 15:16)Daniel Wrote: Try changing [1] in line 10 to
[*] it will be any slave nr.
[*]
Thanks, it works.
but there's something strange if the value is over 20 on the logic machine 5  for example 21 the Modbus pool reader will read it as 30, if it's 22 will read it as 31.4, and so on...

please see attached

Attached Files Thumbnail(s)
   
Reply
#24
I updated the original script and the library, now it will work correct.
------------------------------
Ctrl+F5
Reply
#25
(11.10.2022, 16:22)palomino Wrote: Hello Daniel. The script worked very well , thank you very much.

Can I use the source code here to implement LM as a modbus TUR slave?
Reply
#26
Do you mean RTU? No this is not implemented in this version.
------------------------------
Ctrl+F5
Reply
#27
(07.03.2023, 12:34)Daniel Wrote: Do you mean RTU? No this is not implemented in this version.

Yes, I want to implement LM as a modbus RTU slave.
Reply
#28
The original script still works
https://forum.logicmachine.net/showthread.php?tid=181
https://forum.logicmachine.net/showthrea...33#pid3233
------------------------------
Ctrl+F5
Reply
#29
Hello Daniel,
I need to use the LM as modbus TCP slave and I'm trying to use the universal TCP script. The modbus master is the IP 192.168.2.108 and using the communication port 503, the LM is slave Id 3. I just need to write registers from 2002 to 2090 and read registers from 2102 to 2190. I've been trying just to read registers but I get a message error:

"Modbus Universal 13.03.2023 16:37:00
Resident script:6: Address not available
stack traceback:
[C]: in function 'assert'"


Code:
local mb = require('user.mbtcp')
local copas = require('copas')
local socket = require('socket')
local address = '192.168.2.108'
local port = 503
local server = assert(socket.bind(address, port))
mb.setmapping({
  ['3'] = {
    coils = {
    },
    registers = {
      [2104] = '32/5/4',
      [2105] = '32/5/5',
      [2106] = '32/5/6',
      [2107] = '32/5/7',
    }
  }
})
mb.setswap('w')
mb.setfloat16precision(2)

local function handler(sock)
  copas.setErrorHandler(function(msg)
  if msg ~= nil then
    log(msg)
  end
end)
  sock:settimeout(60)
  while true do
    local res, err = mb.tcphandler(sock)
    if not res then
      break
    end
  end
  sock:close()
end
copas.addserver(server, handler, 60)
copas.loop()
Reply
#30
Keep the address variable unchanged. * will automatically use the Ethernet interface IP address.
Reply
#31
(13.03.2023, 15:42)admin Wrote: Keep the address variable unchanged. * will automatically use the Ethernet interface IP address.

Thank you for your quick response admin. I've done what you told me and the error message disappeared but there's no group address value update for the 4 registers I'm trying to read. Maybe I forgot something, is there any instructions manual for the script? Thank you.
Reply
#32
Your master must send write command for value to be updated.
------------------------------
Ctrl+F5
Reply
#33
(13.03.2023, 16:00)Daniel Wrote: Your master must send write command for value to be updated.

The modbus master is a Siemens PLC and it is cyclically writing to these registers.

I can communicate using the attached script but the CPU usage increases a lot and I have to communicate with multiple PLCs.


Code:
require('luamodbus')
mb = luamodbus.tcp()
mb:open('192.168.2.108', 503)
mb:connect()
mb:setslave(3)
local value_1 = mb:readregisters(2104)
    if value_1 then       
        grp.update('32/5/4', value_1)
Reply
#34
mb:readregisters(2104) This is a read command, If you want to test reading you can simply use Read test under modbus tab.
Is yout PLC master or slave?
------------------------------
Ctrl+F5
Reply
#35
(13.03.2023, 16:15)Daniel Wrote: mb:readregisters(2104) This is a read command, If you want to test reading you can simply use Read test under modbus tab.
Is yout PLC master or slave?

The PLC is configured as modbus master. Yes, this is a read command and both are working for me (read command and read test), but I want to use the universal Modbus TCP Slave script that you developed because if I use the read command and the write command for a large number of variables the CPU usage increases a lot. My question is why when I use read command or read test I receive a read result but when using the universal script there's no data update?
Reply
#36
When mobus master will send write command to particular register then this script will update the object. So far you only doing a read so nothing is written to object.
------------------------------
Ctrl+F5
Reply
#37
If 192.168.2.108 is the PLC address then you need to use master mode using profiles not a slave script.
Reply
#38
(13.03.2023, 16:45)admin Wrote: If 192.168.2.108 is the PLC address then you need to use master mode using profiles not a slave script.

You're absolutely right! Done. Thank you admin and Daniel for your help.
Reply
#39
(07.03.2023, 07:59)Fahd Wrote:
(06.03.2023, 15:16)Daniel Wrote: Try changing [1] in line 10 to
[*] it will be any slave nr.
[*]
Thanks, it works.
but there's something strange if the value is over 20 on the logic machine 5  for example 21 the Modbus pool reader will read it as 30, if it's 22 will read it as 31.4, and so on...

please see attached
[*]
I also encountered the same problem when using the "user. mbtcp" script. The value of the data address is 3300, but the value of the mapped 2-byte floating point KNX group address is 33.00.

Another problem is that I added "holding registers" to map the address table, but it doesn't work.

This is my address mapping list:
Code:
mb.setmapping({
  [7] = {
    coils = {
      [1] = '0/0/1',
      [2] = '0/0/2',
      [3] = '0/0/3',
    },
    registers = {
      [1] = '0/1/1',
      [2] = '0/1/2',
      [3] = '0/1/3',
    } ,
    holdingregisters = {
      [1] = '0/1/4',
      [2] = '0/1/5',
    }
  }
})
Reply
#40
holdingregisters = registers, you need to use inputregisters instead.
mb.setfloat16precision(2) sets how float16 is converted to integer. By default it's 2 decimals (0.01 precision). Change 2 to 0 to convert to integer without any decimals.
Reply


Forum Jump: