Hi Martin, i made a plcnext library based on the thread example. Upon setting a connect boolean to true, it opens a tcp connection to a specified IP in a thread using the delegateThreadBody the block should able to be used multiple times in the same project and each block should be able to connect to a different IP. This, I think, means that a new thread has to be started for each unique IP. Is this doable ? Do you have any tips on the best way to achieve this ? Best regards, Thomas
Hi Thomas, It is possible to create multiple threads from one Component, and this can be implemented using standard C++ features, rather than any features specific to PLCnext Control - e.g. you could maintain a Vector of Thread instances, and manage the lifetime of those instances from the main WorkerThread in your component (for example). If you can create and manage multiple threads from any old C++ program running on Debian or Ubuntu, then you can do exactly the same thing in your C++ PLCnext component. The main difference is that, in your PLCnext project, you have the Thread and WorkerThread classes, in addition to the standard C++ thread classes, if you want to use them. In all cases, you need to know how to write thread-safe code, including how to manage access to static data from multiple threads. Hope this helps. Martin.
… another thought: If you expect the users of your code to always be writing their programs in PLCnext Engineer - and if your code does not require the use of C/C++ - then you could consider implementing your code in a Function Block (FB) in either Structured Text (ST) or C#. This FB would handle one single TCP connection. Then, users of your block simply create one instance of your FB for each TCP connection they need. However, since it is not (currently) possible to create FB instances dynamically at run-time, perhaps this would not suit your application. Martin.
Thanks Martin, The library is indeed intended to be encapsulated in FB’s to be used by PLCnE users. it would make development much easier if it were possible to create new instances of the program for every FB that uses it instead of using 1 process with multiple threads but my guess is that’s not possible with a PLCnext C++ program? unfortunately its not possible for me to write it in ST… Best regards,
The library is indeed intended to be encapsulated in FB’s to be used by PLCnE users.
Maybe I am misunderstanding your use of the word “encapsulate”, but … the only two options for writing FBs for your PLCnE library are C# or an IEC-61131 language, right?
that’s [create new instances of the program for every FB that uses it] not possible with a PLCnext C++ program?
Correct … in fact it’s currently not possible to “call” a C++ program from an FB, you can only exchange data between the two via GDS ports … but you can always tell the user that they must create a new instance of your C++ program for every TCP connection they want to make, just like they would need to create a new instance of an FB for every TCP connection they want to make (if that’s the way you implemented it). Does this make sense?