Hoping someone here has dealt with sockets in v3 of codesys....
I have some socket handles which i store in a persisant variable GVL.
My codesys (v3) application opens the socket handles. This is fine for reset warm, cold, on-line edits, download and run etc but I have a problem where if I do a Reset origin, I loose the handles to the sockets (they are initialised back to 0xFFFFF.... without the windows socket handles actually being closed).
Then when I try and start the application again after the reset origin I get a socket bind() error (The provided address is already in use - ERR_SOCK_ADDRINUSE) when i try to create the socket handles. This is expected because although the codesys application has lost the handle references, the sockets are still open in windows xpe. I have to actually Stop the SP RTE runtime, redownload, and restart the application before I can get my sockets back. When I stop the SP RTE runtime, it is only at this point where windows disposes of the socket handles.
I guess these are my questions....
Should the "Reset Origin" not close the Windows handles for me?
If it did do this, then I would not get the bind "address already in use" error.
Failing point 1, I need to be able to actually close the socket handles in response to a reset origin command.
Is there any way to subscribe to application events such as reset origin, run, stop etc?
Failing point 2, is there a better way of maintaining socket handles so that I do not have to manually Stop and
start/redownload SP RTE to get my sockets back?
Is there any way of closing a socket without having a reference to its handle? Ie. Discovering its handle
from the socket address (port, ipaddress etc)
Thanks for any help!
Laythe
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I had the same behavior for serial ports.
You need to close the port before stopping the application otherwise the port remains opened untill the SP RTE V3 service is stopped.
In V2.3 there are systems events (Tasks) which can call a POU. E.g. the system even stop or reset could call a POU which can close the port just before the application is actually stopped.
In the help of V3.4.1.0 is described that this is not yet implemented.
It might be that the new "network" library 3.4.1.0 in the category "USE CASES" now handles this better.
Did you use the FB's of this library?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
No, i have been using v3.3 SP2. I have upgraded now and will have a play with the new libraries. I am currently using the SysSocket library and making calls SysSockCreate() etc.
I will let you know how I get on!
Thanks again
Laythe
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
It seems we can register against application events with V3 . I've now got my code closing the windows socket on a EVT_StopDone event, and hey-presto no more socket errors!
For anyone interested in this....
Add CmpApp and CmpEventMgr libraries.
Then declare ApplicationEventHandler:
FUNCTION_BLOCKApplicationEventHandlerIMPLEMENTSICmpEventCallbackVAR_INPUTEND_VARVAR_OUTPUTEND_VARVARÂ Â res:UDINT;Â Â evt1:UDINT;Â Â evt3:UDINT;Â Â cb1:UDINT;Â Â cb3:UDINT;END_VAR
Add 3 methods to this FB:
EventCallback:
METHODEventCallback : UDINTVAR_INPUT
  pEventParam : POINTERTOEventParam;END_VARVAREND_VARIF(pEventParam^.EventId=EVT_StartDone)THEN
  ApplicationStart(); END_IFIF(pEventParam^.EventId=EVT_StopDone)THEN
  ApplicationStop();END_IF
There are other events you can subscribe to but I havnt played with any of them... they are listed in CmpApp library. ApplicationStart()/ApplicationStop() are just my own PRG blocks....
FB_init:
METHODFB_initVAR_INPUTÂ Â Â Â bInitRetains:BOOL;Â Â bInCopyCode:BOOL;END_VARVAREND_VAR//RegisteroneventEVT_StartDoneevt1:=EventOpen(16#10002,16#2,res);cb1:=EventRegisterCallback(evt1,THIS^,res);//RegisteroneventEVT_StopDoneevt3:=EventOpen(16#10004,16#2,res);cb3:=EventRegisterCallback(evt3,THIS^,res);
FB_exit:
METHODFB_exitVAR_INPUTÂ Â bInCopyCode:BOOL;END_VARVAREND_VAR(*registeroneventEVT_StartDone*)res:=EventUnregisterCallback(evt1,cb1);EventClose(evt1,res);(*registeroneventEVT_StopDone*)res:=EventUnregisterCallback(evt3,cb3);EventClose(evt3,res);
Then make sure you instantiate an instance of the ApplicationEventHandler FB: Eg:
mApplicationEventHandler:ApplicationEventHandler;
This is based on the "Sys Info" example in the FTP folder. I'm not sure why 3S don't document things like this properly....I had to go poking around in the HTML documentation to guess EVT_StopDone = 16#10004 based on the example of EVT_StartDone being equal to 16#10002 and EVT_StopDone being listed 2 places down. Not difficult, just a pain in the butt!!
Anyway, cheers
Laythe
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
Hoping someone here has dealt with sockets in v3 of codesys....
I have some socket handles which i store in a persisant variable GVL.
My codesys (v3) application opens the socket handles. This is fine for reset warm, cold, on-line edits, download and run etc but I have a problem where if I do a Reset origin, I loose the handles to the sockets (they are initialised back to 0xFFFFF.... without the windows socket handles actually being closed).
Then when I try and start the application again after the reset origin I get a socket bind() error (The provided address is already in use - ERR_SOCK_ADDRINUSE) when i try to create the socket handles. This is expected because although the codesys application has lost the handle references, the sockets are still open in windows xpe. I have to actually Stop the SP RTE runtime, redownload, and restart the application before I can get my sockets back. When I stop the SP RTE runtime, it is only at this point where windows disposes of the socket handles.
I guess these are my questions....
Should the "Reset Origin" not close the Windows handles for me?
If it did do this, then I would not get the bind "address already in use" error.
Failing point 1, I need to be able to actually close the socket handles in response to a reset origin command.
Is there any way to subscribe to application events such as reset origin, run, stop etc?
Failing point 2, is there a better way of maintaining socket handles so that I do not have to manually Stop and
start/redownload SP RTE to get my sockets back?
Is there any way of closing a socket without having a reference to its handle? Ie. Discovering its handle
from the socket address (port, ipaddress etc)
Thanks for any help!
Laythe
I had the same behavior for serial ports.
You need to close the port before stopping the application otherwise the port remains opened untill the SP RTE V3 service is stopped.
In V2.3 there are systems events (Tasks) which can call a POU. E.g. the system even stop or reset could call a POU which can close the port just before the application is actually stopped.
In the help of V3.4.1.0 is described that this is not yet implemented.
It might be that the new "network" library 3.4.1.0 in the category "USE CASES" now handles this better.
Did you use the FB's of this library?
Hi Tim, thanks for the reply.
No, i have been using v3.3 SP2. I have upgraded now and will have a play with the new libraries. I am currently using the SysSocket library and making calls SysSockCreate() etc.
I will let you know how I get on!
Thanks again
Laythe
Cheers Tim,
It seems we can register against application events with V3 . I've now got my code closing the windows socket on a EVT_StopDone event, and hey-presto no more socket errors!
For anyone interested in this....
Add CmpApp and CmpEventMgr libraries.
Then declare ApplicationEventHandler:
Add 3 methods to this FB:
EventCallback:
There are other events you can subscribe to but I havnt played with any of them... they are listed in CmpApp library. ApplicationStart()/ApplicationStop() are just my own PRG blocks....
FB_init:
FB_exit:
Then make sure you instantiate an instance of the ApplicationEventHandler FB: Eg:
This is based on the "Sys Info" example in the FTP folder. I'm not sure why 3S don't document things like this properly....I had to go poking around in the HTML documentation to guess EVT_StopDone = 16#10004 based on the example of EVT_StartDone being equal to 16#10002 and EVT_StopDone being listed 2 places down. Not difficult, just a pain in the butt!!
Anyway, cheers
Laythe
Perfect !!! Thanks for the info.