Skip to content

Commit

Permalink
* manage connection loss to NC
Browse files Browse the repository at this point in the history
  • Loading branch information
MRIIOT committed Feb 2, 2023
1 parent 3b41394 commit 3080be7
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion examples/windows/config.machines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ machines:
port: !!int 8193
timeout_s: !!int 3
l99.driver.fanuc.strategies.FanucMultiStrategy, fanuc:
stay_connected: !!bool false
stay_connected: !!bool true
#exclusions:
# 1:
# 2: [ X, S ]
Expand Down
2 changes: 1 addition & 1 deletion fanuc/platform/Connect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public dynamic Connect()
_machine.FocasEndpoint.Port,
_machine.FocasEndpoint.ConnectionTimeout,
out _handle);
});
}, throwOnSocketError: false); // Exclude Connect from throwing if EW_SOCKET

var nr = new
{
Expand Down
5 changes: 4 additions & 1 deletion fanuc/platform/Disconnect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ public async Task<dynamic> DisconnectAsync()

public dynamic Disconnect()
{
var ndr = _nativeDispatch(() => { return (Focas.focas_ret) Focas.cnc_freelibhndl(_handle); });
var ndr = _nativeDispatch(() =>
{
return (Focas.focas_ret) Focas.cnc_freelibhndl(_handle);
}, throwOnSocketError: false); // Exclude Disconnect from throwing if EW_SOCKET

var nr = new
{
Expand Down
9 changes: 8 additions & 1 deletion fanuc/platform/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public Platform(FanucMachine machine)

private readonly string _docBasePath = "https://docs.ladder99.com/drivers/fanuc-driver/focas-api";

private NativeDispatchReturn _nativeDispatch(Func<Focas.focas_ret> func)
private NativeDispatchReturn _nativeDispatch(Func<Focas.focas_ret> func, bool throwOnSocketError = true)
{
Focas.focas_ret innerRc = Focas.focas_ret.EW_OK;
long innerElapsed = 0;
Expand All @@ -80,6 +80,13 @@ private NativeDispatchReturn _nativeDispatch(Func<Focas.focas_ret> func)
outerSw.Stop();

//Console.WriteLine($"elapsed={innerElapsed}, waiting={outerSw.ElapsedMilliseconds - innerElapsed}");

// Throw if connection loss detected.
if (throwOnSocketError && innerRc == Focas.focas_ret.EW_SOCKET)
{
_logger.Error($"[{_machine.Id}] Connection lost.");
throw new SocketException((int)Focas.focas_ret.EW_SOCKET);
}

return new NativeDispatchReturn
{
Expand Down
8 changes: 4 additions & 4 deletions fanuc/strategies/FanucExtendedStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -648,9 +648,9 @@ protected virtual async Task CollectBeginAsync()
await SetNative("connect",
await Platform.ConnectAsync());
await Peel("connection",
new[]
new dynamic[]
{
Get("connect")
Get("connect")!
},
new dynamic[]
{
Expand Down Expand Up @@ -729,9 +729,9 @@ protected virtual async Task CollectEndAsync()
await SetNative("disconnect",
await Platform.DisconnectAsync());
await Peel("connection",
new[]
new dynamic[]
{
Get("disconnect")
Get("disconnect")!
},
new dynamic[]
{
Expand Down

0 comments on commit 3080be7

Please sign in to comment.