diff --git a/Sharp7.Rx/Sharp7.Rx.csproj b/Sharp7.Rx/Sharp7.Rx.csproj index faf60fe..16d6add 100644 --- a/Sharp7.Rx/Sharp7.Rx.csproj +++ b/Sharp7.Rx/Sharp7.Rx.csproj @@ -27,4 +27,10 @@ + + + true + linqpad-samples\;content + + diff --git a/Sharp7.Rx/linqpad-samples/Create Notification.linq b/Sharp7.Rx/linqpad-samples/Create Notification.linq new file mode 100644 index 0000000..57d9720 --- /dev/null +++ b/Sharp7.Rx/linqpad-samples/Create Notification.linq @@ -0,0 +1,37 @@ + + Sharp7.Rx + Sharp7.Rx + System.Reactive.Linq + System.Reactive.Threading.Tasks + System.Threading.Tasks + + +var ip = "10.30.3.221"; // Set IP address of S7 +var db = 3; // Set to an existing DB + +// For rack number and cpu mpi address see +// https://github.com/fbarresi/Sharp7/wiki/Connection#rack-and-slot +var rackNumber = 0; +var cpuMpiAddress = 0; + +using var plc = new Sharp7Plc(ip, rackNumber, cpuMpiAddress); + +await plc.InitializeAsync(); +await plc.ConnectionState + .FirstAsync(c => c == Sharp7.Rx.Enums.ConnectionState.Connected) + .ToTask(); + +"Connection established".Dump(); + +// create an IObservable +var observable = plc.CreateNotification($"DB{db}.Int6", Sharp7.Rx.Enums.TransmissionMode.OnChange); + +observable.Dump(); + +for (int i = 0; i < 10; i++) +{ + await plc.SetValue($"DB{db}.Int6", (short)i); + await Task.Delay(300); +} + + diff --git a/Sharp7.Rx/linqpad-samples/Establish connection.linq b/Sharp7.Rx/linqpad-samples/Establish connection.linq new file mode 100644 index 0000000..33f6f69 --- /dev/null +++ b/Sharp7.Rx/linqpad-samples/Establish connection.linq @@ -0,0 +1,39 @@ + + Sharp7.Rx + Sharp7.Rx + System.Reactive.Linq + System.Reactive.Threading.Tasks + System.Threading.Tasks + + +// Set IP address of S7 +var ip = "10.30.3.221"; + +// For rack number and cpu mpi address see +// https://github.com/fbarresi/Sharp7/wiki/Connection#rack-and-slot +var rackNumber = 0; +var cpuMpiAddress = 0; + +// Create Sharp7Plc +using var plc = new Sharp7Plc(ip, rackNumber, cpuMpiAddress); + +// Initialize connection +await plc.InitializeAsync(); + +// wait for connection to be established +await plc.ConnectionState + .FirstAsync(c => c == Sharp7.Rx.Enums.ConnectionState.Connected) + .ToTask(); + +"Connection established".Dump(); + +try +{ + await Task.Delay(Timeout.Infinite, this.QueryCancelToken); +} +catch (TaskCanceledException) +{ + "Script stopped by user. Disconnecting by disposing plc.".Dump(); +} + + diff --git a/Sharp7.Rx/linqpad-samples/FileOrder.txt b/Sharp7.Rx/linqpad-samples/FileOrder.txt new file mode 100644 index 0000000..6d18ca6 --- /dev/null +++ b/Sharp7.Rx/linqpad-samples/FileOrder.txt @@ -0,0 +1,4 @@ +Establish connection.linq +Write and read value.linq +Create Notification.linq +Multiple notifications.linq \ No newline at end of file diff --git a/Sharp7.Rx/linqpad-samples/Multiple notifications.linq b/Sharp7.Rx/linqpad-samples/Multiple notifications.linq new file mode 100644 index 0000000..ec8db01 --- /dev/null +++ b/Sharp7.Rx/linqpad-samples/Multiple notifications.linq @@ -0,0 +1,47 @@ + + Sharp7.Rx + Sharp7.Rx + System.Reactive.Linq + System.Reactive.Threading.Tasks + System.Threading.Tasks + + +var ip = "10.30.3.221"; // Set IP address of S7 +var db = 3; // Set to an existing DB + +// For rack number and cpu mpi address see +// https://github.com/fbarresi/Sharp7/wiki/Connection#rack-and-slot +var rackNumber = 0; +var cpuMpiAddress = 0; + +using var plc = new Sharp7Plc(ip, rackNumber, cpuMpiAddress); + +plc.ConnectionState.Dump(); + +await plc.InitializeAsync(); +await plc.ConnectionState + .FirstAsync(c => c == Sharp7.Rx.Enums.ConnectionState.Connected) + .ToTask(); + +// create an IObservable +plc.CreateNotification($"DB{db}.Int6", Sharp7.Rx.Enums.TransmissionMode.OnChange).Dump("Int 6"); +plc.CreateNotification($"DB{db}.Real10", Sharp7.Rx.Enums.TransmissionMode.OnChange).Dump("Real 10"); + + + +for (int i = 0; i < 15; i++) +{ + switch (i%3) + { + case 0: + await plc.SetValue($"DB{db}.Int6", (short)i); + break; + case 1: + await plc.SetValue($"DB{db}.Real10", i * 0.123f); + break; + } + + await Task.Delay(300); +} + + diff --git a/Sharp7.Rx/linqpad-samples/Write and read value.linq b/Sharp7.Rx/linqpad-samples/Write and read value.linq new file mode 100644 index 0000000..ee5780e --- /dev/null +++ b/Sharp7.Rx/linqpad-samples/Write and read value.linq @@ -0,0 +1,35 @@ + + Sharp7.Rx + Sharp7.Rx + System.Reactive.Linq + System.Reactive.Threading.Tasks + System.Threading.Tasks + + +var ip = "10.30.3.221"; // Set IP address of S7 +var db = 3; // Set to an existing DB + +// For rack number and cpu mpi address see +// https://github.com/fbarresi/Sharp7/wiki/Connection#rack-and-slot +var rackNumber = 0; +var cpuMpiAddress = 0; + +using var plc = new Sharp7Plc(ip, rackNumber, cpuMpiAddress); + +await plc.InitializeAsync(); +await plc.ConnectionState + .FirstAsync(c => c == Sharp7.Rx.Enums.ConnectionState.Connected) + .ToTask(); + +"Connection established".Dump(); + +for (int i = 0; i < 10; i++) +{ + await plc.SetValue($"DB{db}.Int6", (short)i); + var value = await plc.GetValue($"DB{db}.Int6"); + value.Dump(); + + await Task.Delay(200); +} + +