tcpcodesys

Codesys Automatic xExecute NBS.TCP_write


I've recently started experimenting with CODESYS for a project and am fairly new to it. I'm currently working with Function Block Diagrams (FBD) to set up TCP communication between a server and a client, where the client is implemented as a Python script. Once the communication is established, my connection status changes to True, which is the expected behavior.

My challenge arises with the TCP write module, which requires the xEnable_write flag to toggle between True and False. If xEnable_write remains True, the module only performs a single write operation. To address this, I introduced a falling edge trigger (F_TRIG), expecting it to help in toggling xEnable_write and it does, However, I've encountered an issue where in the beginning after connxion established I need to manually set xEnable_write to True for the system to work as intended. I'm looking for a way to automate this process so that xEnable_write becomes True automatically upon establishing a connection .

Initially, I tried setting xEnable_write to True from the start, but this approach didn't work well since it left the flag permanently True, preventing further toggling. I also experimented with using a TON timer, but I faced similar issues.

I am seeking a solution that automatically toggles xEnable_write between True and False after a connection is established, ensuring continuous write operations without manual intervention. Any advice or guidance on how to achieve this would be greatly appreciatedenter image description here


Solution

  • I haven't worked with the TCP stack in CODESYS, so I'll make some assumptions from your explanation:

    If the above is true, and your goal is to repeatedly execute TCP_Write, then we need 2 things:

    1. Start executing TCP_Write when the connection is ready.
    2. Execute TCP_Write as soon as the last write finishes.

    By adding F_TRIG to xDone, you correctly dealt with the second point, since when a write finishes and xDone output beccomes true, F_TRIG detects the falling edge (you could probably also use R_TRIG here) and pulses xExecute, which starts another write.

    The problem is the first point. xDone is only set (and then reset on the next cycle) when an operation is already executed, so you need to execute the first operation. Setting xExecute to TRUE from the start is incorrect, since the connection hasn't been established yet. For that, you need to use the TCP_Connection.xActive output, in other words, as soon as xActive becomes TRUE, you can execute the first write (set xExecute to TRUE for 1 cycle).

    You may think that adding a rising edge (R_TRIG) to xActive would solve it, but the problem with having 2 triggers write to the same variable is that they would overwrite each other. To solve this, one way would be to add a R_TRIG to the xActive output and write the result into a temporary variablem for example xEcecute1, and have the F_TRIG after xDone write the result into another variable, for example xExecute2. Now we can execute a write when either xExecute1 or xExecute2 is set to true, in other words, when either the connection is first established, or a previous write is done (TCP_Write.xExecute := xExecute1 OR_ELSE xExecute2).

    If you want to go one step further, I'd suggest looking at xBusy, xError and eError in the documentation, and deciding whether you should pause the toggling of xExecute in some conditions.