Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Modbus read/write, PI as slave

2015-02-19
2018-10-02
  • urbanlarsson - 2015-02-19

    Hi!

    I don't quit get it how to use the PI as an modbus slave.

    For example

    I want the master to read/write the bit 40000.00 in the PI, but i want the PI to be able to change the 40000.00 also.

    Lets say I want 40000.00 to toggle every time a usere pushes a button connected to the master, but at the same time I want the PI-runtime to toggle the 40000.00.
    I have no problem toggling the 40000 in the master, but it can't read back the actuall status of 40000.00 so if the runtime toggles it, the master doesn't know that and all gets out of controll.

    /U

     
  • Strucc - 2015-02-19

    It's not really a PI, nor Modbus specific problem, but a common issue with data synchronization.

    Try to design your system according to the following rules:
    - Every piece of data has one, and only one source (it does not matter if it's the master or the slave)
    - The data source (physical input, analog, digital, or output of some calculations) is updated regularly
    - Any number of consumers can receive, use that data

    IF you need the data to be modified by any external source, then you have to program it around your data source: It will be using an external data source as a consumer, and decide - it really depends on the nature of the data you'd like to synchronize - when to update local data. In some cases, locking mechanisms has to be used, what is quite hard to do with modbus, you can only rely on timing.

    Another model is a "token" based data synchronization: you pass a "token" around the data producers, and they can only update the common data, when they have the token. It can be time based, counter based, etc...

     
  • urbanlarsson - 2015-02-19

    The problem isn't data sync, it's that I'm used to slaves that can both read and write to there own modbus registers, the PI only can read from 40000, it can't write to it.

    So it can't be used both for input and output.

    /U

     
  • Strucc - 2015-02-19

    It should be possible with holding registers...
    Just "map" it to a variable, that you can read and write. The variable will be updated, if it's updated by the master.
    I didn't try with PI, but I think Modbus TCP and RTU functionality is common for all platforms.

     
  • urbanlarsson - 2015-02-19

    You only have input and outregister, no comboregister,

    runtime can read from 40000 and write to 30000, and there is som function called input and output registers overlay, but I don't get it to work as I want and are used to.

    /U

     
  • urbanlarsson - 2015-02-19

    Yes i'm using the slave as your screendump, and there are only inputs AND output registers, no combo-register (eg 40000 to 49999 in "normal" modbus),
    the runtime can't alter the variables mapped to modbus inputs.

    /U

     
  • Strucc - 2015-02-19

    Did you see the
    Checkbox in the ModBus TCP Slave device, ModbusTCP tab / Data model?

     
  • urbanlarsson - 2015-02-19

    Yes, but i don't really get it how it works All my tests fail.

    /U

     
  • Strucc - 2015-02-19

    I get it now...

    Yep, it's a typical synchronization issue.
    I created a sample project (running on CodeSys Control WinV3), I hope it helps.
    Monitor the GVL_MASTER, and GVL_SLAVE variable lists. You can overwrite slave data from the Master, by changing wToSlave...

    ModbusMasterSlaveDemo.project [167.45 KiB]

     
  • philobedo - 2015-02-22

    I have the same problem when IP is Modbus slave, the holding register values do not change whether in writing from the Modbus master or from the slave. I use the demo.
    Best regards.

     
  • penngolliwog - 2018-10-02

    Thanks Strucc for the sample program.
    I had the same problem.

    I have expanded your idea and written a FB to handle synchronism.

    FB_ModbusIO.zip [2.33 KiB]

     

Log in to post a comment.