Usage
Setup
Needed Variables
UDPCommMgr_typ commMgr;
STRING[255] sendBuffer;
STRING[255] recvBuffer;
Initialization
commMgr.IN.PAR.pSendData = &sendBuffer;
commMgr.IN.PAR.pReceiveData = &recvBuffer;
commMgr.IN.PAR.MaxReceiveLength = sizeof(recvBuffer);
strcpy(commMgr.IN.CFG.LocalInterface.IPAddress, "127.0.0.1");
commMgr.IN.CFG.LocalInterface.Port = 5000;
strcpy(commMgr.IN.CFG.RemoteInterface.IPAddress, "127.0.0.1");
commMgr.IN.CFG.RemoteInterface.Port = 5001;
commMgr.IN.CMD.Enable = TRUE;
Setting commMgr.IN.CMD.Enable to TRUE is required to open the socket; the communication manager stays idle until this is set. If LocalInterface.IPAddress is an empty string, the socket is opened on all local interfaces.
Sending Data
Set commMgr.IN.PAR.SendLength to the number of bytes to send, then set commMgr.IN.CMD.Send to trigger the send (or hold it TRUE continuously if AllowContinuousSend is set). commMgr.OUT.STAT.DataSent goes TRUE once the datagram has been sent, and commMgr.OUT.STAT.SentDataLength reports how many bytes went out.
commMgr.IN.PAR.SendLength = strlen(sendBuffer);
commMgr.IN.CMD.Send = TRUE;
// Call UDPCommFn_Cyclic(&commMgr) every scan while commMgr.IN.CMD.Enable is TRUE
if (commMgr.OUT.STAT.DataSent) {
commMgr.IN.CMD.Send = FALSE;
}
Receiving Data
While the communication manager is enabled, incoming datagrams are copied into commMgr.IN.PAR.pReceiveData automatically. commMgr.OUT.STAT.NewDataAvailable goes TRUE when a datagram has arrived, with commMgr.OUT.STAT.ReceivedDataLength and commMgr.OUT.STAT.ReceivedFromInterface describing it. Set commMgr.IN.CMD.AcknowledgeData to clear the flag and resume listening for the next datagram (or set AllowContinuousReceive to clear it automatically). To stop listening without closing the socket, set commMgr.IN.PAR.DisableReceiving to TRUE.
if (commMgr.OUT.STAT.NewDataAvailable) {
// recvBuffer now holds commMgr.OUT.STAT.ReceivedDataLength bytes
commMgr.IN.CMD.AcknowledgeData = TRUE;
}
Handling Errors
If commMgr.OUT.STAT.Error is set, commMgr.OUT.STAT.ErrorID and commMgr.OUT.STAT.ErrorString describe the failure. See Structures and Enums for the list of error codes reported in commMgr.OUT.STAT.ErrorID.
How you recover depends on where the error occurred:
- Opening the socket (for example, due to an invalid configuration): the communication manager parks in an error state and will not reopen the socket on its own. Recover by cycling
commMgr.IN.CMD.EnabletoFALSEand back toTRUE. - Sending or receiving data: the socket stays open and the error is only reported in the status. Set
commMgr.IN.CMD.AcknowledgeErrorto clearError,ErrorID, andErrorString.