|Encapsulating listen sockets in Function Blocks [message #1801517]
||Mon, 21 January 2019 15:14
| Caspar Gries
Registered: January 2019
I'm implementing a very simple protocol which connects a FORTE instance to a server PC that controls a few sensors/actuators via its GPIO pins. The protocol itself defines a UDP command packet which is sent by FORTE to control the actuators, and a status broadcast message with the current sensor values, that is sent in regular intervals by the server.
I want to wrap the protocol details in FBs. This is very easy for commands, it's just a FB that accepts events, creates an UDP command packet from the related data and sends it out. The other direction is trickier, since I need to open an UDP port and wait for incoming messages from the server. The question is now, how should this be modelled in FORTE? I can think of the following approaches:
1. Open a listen socket and do a blocking read until data arrives --> probably not a good idea if there is only one execution thread, since FORTE will then effectively hang.
2. Do a non-blocking read. However, we then need a "poll" event input for the FB that needs to be triggerered constantly, otherwise we will never fetch arriving packets from the socket.
3. Spawn a dedicated thread that does a blocking read, generating an output event when new data arrives.
From these options, I would strongly opt for the third, since I think it's the cleanest. The question is, can this be implemented in the way I imagine? In particular, can FBs create events on their own, and is FORTE thread-safe so we can do it in a separate thread? Is there anything else I have to watch for? Maybe there is already some demo code that can serve as a template?
Powered by FUDForum
. Page generated in 0.02011 seconds