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 appreciated
I haven't worked with the TCP stack in CODESYS, so I'll make some assumptions from your explanation:
TCP_Connection.xActiveindicates thatTCP_Writecan be executed (if not busy).TCP_Write.xDoneindicates thatTCP_Writeis free and can be executed.TCP_Writeexecutes when it detects a rising edge onxExecute.If the above is true, and your goal is to repeatedly execute
TCP_Write, then we need 2 things:TCP_Writewhen the connection is ready.TCP_Writeas soon as the last write finishes.By adding
F_TRIGtoxDone, you correctly dealt with the second point, since when a write finishes andxDoneoutput beccomes true,F_TRIGdetects the falling edge (you could probably also useR_TRIGhere) and pulsesxExecute, which starts another write.The problem is the first point.
xDoneis only set (and then reset on the next cycle) when an operation is already executed, so you need to execute the first operation. SettingxExecuteto TRUE from the start is incorrect, since the connection hasn't been established yet. For that, you need to use theTCP_Connection.xActiveoutput, in other words, as soon asxActivebecomes TRUE, you can execute the first write (setxExecuteto TRUE for 1 cycle).You may think that adding a rising edge (
R_TRIG) toxActivewould 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 aR_TRIGto thexActiveoutput and write the result into a temporary variablem for examplexEcecute1, and have theF_TRIGafterxDonewrite the result into another variable, for examplexExecute2. Now we can execute a write when eitherxExecute1orxExecute2is 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,xErrorandeErrorin the documentation, and deciding whether you should pause the toggling ofxExecutein some conditions.