James M. (Customer) asked a question.

How do I use the values read from Modbus TCP holding registers in my code?

[UPDATE]: I didn't change anything, and when I turned the plc and hmi on this morning, it worked as expected.

 

I've been setting up Modbus TCP communication between a P2CDS-622 (server) and a Maple Systems cMT2128x HMI (client). When I download the project to the controller and connect, I am seeing (in CODESYS) the correct "current value" displayed in the plc holding register that the HMI is writing to; if I try to assign that value to a variable (even one of the same type) in the plc, I get a 0 value no matter what.

 

I've done several hours of troubleshooting and can't figure out why this is. This is my first time on CODESYS, so I'm assuming its something simple that I did wrong. I did follow the steps given by AutomationDirect for setting up the network and adding a server device. Some of the troubleshooting I have done thus far has included ensuring that the bus cycle task is the task that my program is running under and that the Always Update Variables setting is Enabled 2. I've tried it with the holding registers as writeable vs read-only from the server's perspective.

 

I've attached a few screenshots showing the code, the live values I am seeing (71 is being correctly displayed in the first register), as well as the IO mapping. Any ideas about this? I'm more than happy to provide additional information as needed.

ServerDevice IO MappingMainTask Live ValuesMainTask Code


  • FACTS_MikeSc (AutomationDirect)

    Double check your CPU is in run mode.

     

    In Codesys the Modbus server will always run even if the cpu is in stop mode. So you can read and write values to the address but the cpu won't update any variables in the program if it's in stop mode.

  • James M. (Customer)

    It was in Stop mode early in the troubleshooting process, but I switched it to Run mode once I noticed. Those screenshots are with it in Run mode.

  • James M. (Customer)

    Per my update, it is working without me changing anything. It seemed like I had tried every combination of toggling Run/Stop and power cycling to no avail.

     

    While it is working, I have no idea what "fixed" it. I am curious if you have any ideas of what happened, so I don't repeat this in the future.

     

    Edit: I think I figured it out. If I toggle Run off/on after uploading my code, it starts working. If I upload with Run on, but don't toggle the switch, it doesn't. Is this expected?

    Expand Post
    • FACTS_MikeSc (AutomationDirect)

      Yes that is expected.

       

      When uploading changes to the cpu project, any significant changes to the project like device settings and such will require you to put the cpu back into run mode.

      image 

      Changes to the code will prompt you with these options.

      imageOnly "Login with online change" will keep the cpu in run mode.

       

      Everything else will require you to put the cpu back into run mode even if the switch is in the run mode state.

      Expand Post
  • FACTS_MikeSc (AutomationDirect)

    Thanks for the update. I've been trying to recreate the issue on my end with no success and will do some further digging.

     

    Everything you did for troubleshooting is correct in what I would have done. Hard to say what fixed it for you, but from my experience it usually has to do with that bus cycle task option.

     

  • James M. (Customer)

    Ok, I appreciate that. I saved a copy of the project after it started working, and now the one that I modified is giving the same issue as before, even with the toggling Run/Stop and power cycling. I checked and the working version that I copied without editing is still working.

     

    Edit: Even changing something as simple as integrating the assignment of

    HMIValvePos_GI := %IW0;

     

    into the VAR declaration s.t.

    HMIValvePos_GI : WORD := %IW0;

     

    makes the program stop assigning the current value of %IW0 to HMIValvePos_GI.

     

    This isn't expected behavior as far as I'm aware, correct?

    Expand Post
  • FACTS_MikeSc (AutomationDirect)

    Ok, can you check if there are any error messages in the cpu device logs.

     

    There will be log tab in the cpu device.

    image 

    Also when connected, the green "circles" indicate that device / task is running with no issues. Just pointing this out that these are other indicators if something isn't running correctly.

    Expand Post
  • James M. (Customer)

    imageHere's the log, for the program I described in the edit on my last comment (where I made only a slight change to the MainTask program from the working version).

     

    Edit: I should add that when I login to download, I seem to always get the first prompt ('Application' in RUN mode) but not always the second (login with online change vs download). Is this expected? Also, everything is green in the device tree.

    Expand Post
  • FACTS_MikeSc (AutomationDirect)

    I seem to always get the first prompt.

    Yes that is normal. Any change in the devices Codesys will require the application to be replaced if it sees differences.

     

    As a test you can try dumbing down the program to just have line 2. Creating a new one would be easier. Use software for testing the modbus first rather than the HMI like qModMaster.

     

    Everything from what you told me seems to be running with no issue. I can't reproduce what you are seeing myself.

    Expand Post
  • James M. (Customer)

    It seems I have it working consistently now. I think the first part was what you identified (I needed to toggle RUN/STOP after login with download). The second half, when the issue reappeared, was because of my switch to improperly assigning the tags to the MODBUS registers ( I wasn't using AT in the declaration, which I now realize I didn't screenshot).

     

    Thanks for the help.

10 of 11