Add linqpad samples

This commit is contained in:
Peter Butzhammer
2024-02-09 14:09:34 +01:00
parent 9a51a407ec
commit 096435f4d1
6 changed files with 168 additions and 0 deletions

View File

@@ -27,4 +27,10 @@
<PackageReference Include="System.Reactive" Version="6.0.0" />
</ItemGroup>
<ItemGroup>
<Content Include="linqpad-samples/**/*.*">
<Pack>true</Pack>
<PackagePath>linqpad-samples\;content</PackagePath>
</Content>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,37 @@
<Query Kind="Statements">
<NuGetReference Prerelease="true">Sharp7.Rx</NuGetReference>
<Namespace>Sharp7.Rx</Namespace>
<Namespace>System.Reactive.Linq</Namespace>
<Namespace>System.Reactive.Threading.Tasks</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
</Query>
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<short>($"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);
}

View File

@@ -0,0 +1,39 @@
<Query Kind="Statements">
<NuGetReference Prerelease="true">Sharp7.Rx</NuGetReference>
<Namespace>Sharp7.Rx</Namespace>
<Namespace>System.Reactive.Linq</Namespace>
<Namespace>System.Reactive.Threading.Tasks</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
</Query>
// 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();
}

View File

@@ -0,0 +1,4 @@
Establish connection.linq
Write and read value.linq
Create Notification.linq
Multiple notifications.linq

View File

@@ -0,0 +1,47 @@
<Query Kind="Statements">
<NuGetReference Prerelease="true">Sharp7.Rx</NuGetReference>
<Namespace>Sharp7.Rx</Namespace>
<Namespace>System.Reactive.Linq</Namespace>
<Namespace>System.Reactive.Threading.Tasks</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
</Query>
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<short>($"DB{db}.Int6", Sharp7.Rx.Enums.TransmissionMode.OnChange).Dump("Int 6");
plc.CreateNotification<float>($"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);
}

View File

@@ -0,0 +1,35 @@
<Query Kind="Statements">
<NuGetReference Prerelease="true">Sharp7.Rx</NuGetReference>
<Namespace>Sharp7.Rx</Namespace>
<Namespace>System.Reactive.Linq</Namespace>
<Namespace>System.Reactive.Threading.Tasks</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
</Query>
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<short>($"DB{db}.Int6");
value.Dump();
await Task.Delay(200);
}