Logic Machine Forum
Universal Modbus TCP/RTU Slave - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Gateway (https://forum.logicmachine.net/forumdisplay.php?fid=10)
+--- Thread: Universal Modbus TCP/RTU Slave (/showthread.php?tid=4288)

Pages: 1 2 3 4


RE: Universal Modbus TCP Slave - Daniel - 06.03.2023

Yes this is modbus TCP


RE: Universal Modbus TCP Slave - Daniel - 06.03.2023

Try changing [1] in line 10 to [*] it will be any slave nr.


RE: Universal Modbus TCP Slave - Fahd - 07.03.2023

(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


RE: Universal Modbus TCP Slave - Daniel - 07.03.2023

I updated the original script and the library, now it will work correct.


RE: Universal Modbus TCP Slave - rw_echo - 07.03.2023

(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?


RE: Universal Modbus TCP Slave - Daniel - 07.03.2023

Do you mean RTU? No this is not implemented in this version.


RE: Universal Modbus TCP Slave - rw_echo - 07.03.2023

(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.


RE: Universal Modbus TCP Slave - Daniel - 07.03.2023

The original script still works
https://forum.logicmachine.net/showthread.php?tid=181
https://forum.logicmachine.net/showthread.php?tid=181&pid=3233#pid3233


RE: Universal Modbus TCP Slave - David - 13.03.2023

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()



RE: Universal Modbus TCP Slave - admin - 13.03.2023

Keep the address variable unchanged. * will automatically use the Ethernet interface IP address.


RE: Universal Modbus TCP Slave - David - 13.03.2023

(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.


RE: Universal Modbus TCP Slave - Daniel - 13.03.2023

Your master must send write command for value to be updated.


RE: Universal Modbus TCP Slave - David - 13.03.2023

(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)



RE: Universal Modbus TCP Slave - Daniel - 13.03.2023

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?


RE: Universal Modbus TCP Slave - David - 13.03.2023

(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?


RE: Universal Modbus TCP Slave - Daniel - 13.03.2023

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.


RE: Universal Modbus TCP Slave - admin - 13.03.2023

If 192.168.2.108 is the PLC address then you need to use master mode using profiles not a slave script.


RE: Universal Modbus TCP Slave - David - 13.03.2023

(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.


RE: Universal Modbus TCP Slave - rw_echo - 14.03.2023

(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',
    }
  }
})



RE: Universal Modbus TCP Slave - admin - 14.03.2023

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.