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 TCP manually with Codesys 3.5.16SP2

2023-03-17
2023-04-28
  • jari-koivuluoma - 2023-03-17

    Hello

    Im planning to use varying amounts (1-6) of Beckhoff EK9000 ModbusTCP couplers with varying amounts of basic input and output terminals attached to them.

    I want to be able to change the amount of couplers and terminals without altering the source project.

    So I need to be able to communicate with Modbus TCP slave couplers with their IP addresses (and all other possible settings) coming from variables rather than being static.

    Also the mapping of the input and output data on the devices will not be static either and I need to calculate the right spot of the data based on the amounts, types and positions of the terminals attached to the couplers which is ofcourse also stored in variables rather than being static.

    I have never even used Modbus in the standard way so I might need some help.

    I would imagine that I would have to add Ethernet and Modbus TCP master to the device tree and from there use some library function blocks to read and write to the slaves manually but this is just me guessing.

    The most unclear part is that how do I read and write to the slaves purely by code?

    Could someone please provide me a some kind of rough outlines of how it goes? And/or some example code?

     

    Last edit: jari-koivuluoma 2023-03-17
  • snhatton - 2023-03-31

    I think the IoDrvModbusTCP library would work for you. From here you can write IEC code to connect to and configure your modbus devices without having to manage the device tree. Hope this helps!

     
  • jari-koivuluoma - 2023-04-24

    Yes, that did it and quite straight forwardly after all. Basically I just used a TON and ModbudRequest FB to refresh the data from and to the process image of the couplers. The FB required the a pointer to the slave instance in the device tree nyt nothing fancier than that.

    However I have another problem. I wrote a seperate project to demonstrate the idea and it worked nicely. However after I integrated it to my actual project I started to get exeptions.

    What I get from this screenshot is that the error happens in the Modbus library function PutNWords.

    But also this screenshot seem to show that it has something to do with the other task (BackupTask) I have running every 1 ms. While the main task is running every 20 ms.

    The BackupTask runs a program that writes all of my parameter structures to files and there is about 4000 of them so it takes so long that I put the writing to its own task.

    Parameters are on a Global variables list from where the code in the main task accesses them constantly and from where the PRG_Backup (on the BackupTask) writes them to files and also reads them from files back to the variables.

    The error happens even when the PRG_Backup is not working thus not accessing the shared variables.

    I have assumed that I can share variables like that between tasks and it has been working right until I started using the Modbus code.

    When I disable the BackupTask and move the PRG_Backup to the main task, it works but the backup takes more than 10 minutes.

     

    Last edit: jari-koivuluoma 2023-04-24
  • jari-koivuluoma - 2023-04-28

    The problem was that I had an instance of the ModbusRequest for each query and they were operating on parallel. Apparently you must use only 1 instance or at least not run to them on parallel.

     

    Last edit: jari-koivuluoma 2023-04-28

Log in to post a comment.