SysSockPing is a synchronous function - if you call it in your code it will block that task until the ping timeout expires. There is an alternative SysSockPingAsync, but the code snippet (below) is a little longer. Really one should also call syssockasyncfb.RemoveAllJobs() on application shutdown, but they will timeout anyway themselves.
PROGRAM PLC_PRG
VAR
syssockasyncfb : SysSocketAsync.SysSockAsyncFB;
instAsyncJobParam : SysSocketAsync.ASYNCJOB_PARAM;
taskname : STRING := 'Taskname';
taskhandle : SysSocketAsync.RTS_IEC_HANDLE := SysSocketAsync.SysTypes.RTS_INVALID_HANDLE;
paramis: SysSocketAsync.tSysSockPing;
state: UDINT; //SysSocketAsync.CmpAsyncMgr.GVL
result: SysSocketAsync.RTS_IEC_RESULT;
replyTime: UDINT;
out: SysSocketAsync.RTS_IEC_RESULT;
PING: BOOL;
handle: SysSocketAsync.RTS_IEC_HANDLE;
resultSetJobParam: SysSocketAsync.RTS_IEC_RESULT;
END_VAR
//general parameters for the async manager
instAsyncJobParam.TaskParam.pszTaskname := ADR(taskname); //you could probably put a useful name here
instAsyncJobParam.TaskParam.ulTaskPriority := 128; //very low runtime priority
instAsyncJobParam.TaskParam.ulTaskSleepTime := 0; //"not used, set to 0"
instAsyncJobParam.TaskParam.phTaskHandle := ADR(taskhandle);
instAsyncJobParam.TaskParam.ulEndTaskAfterJob := 1; //why not?
// parameters for the ping itself
paramis.pszIpAddress := '192.168.56.122';
paramis.ulTimeout := 1000; //ms
paramis.pulReplyTime := ADR(replyTime); //when the job is finished, the reply time will be stored here
paramis.pulOut := ADR(out); //when the job is finished, the result will be stored here
//just call the SysSockPingAsync once, and it starts the job. You know it is finished, when PLC_PRG.state = 2
IF PING THEN
PING := FALSE;
resultSetJobParam := syssockasyncfb.AsyncSetJobParams(ulAsyncJobType:= SysSocketAsync.CmpAsyncMgr.GVL.ASYNCJOB_TASK,
ulTimeout:= paramis.ulTimeout * 2,
pAsyncJobParams:= ADR(instAsyncJobParam));
handle := syssockasyncfb.SysSockPingAsync(pParam:= ADR(paramis), pudState:= ADR(state), pResult:= ADR(result));
END_IF
Hi i-campbell, Thanks for your sharing. I want to use socket to send and recv messages. Can you give me a example about it using SysSockPingAsync lib?
hi oooo. I only use SysSocket for the ancillary functions, not for actual sockets. If your sockets are UDP/TCP then best to use Net Base Services Library, which has examples at https://forge.codesys.com/prj/codesys-example/
such as https://forge.codesys.com/prj/codesys-example/nbs/home/Home/
OK, Thank you i-campbell.