mirror of
https://github.com/OpenLogics/MewtocolNet.git
synced 2025-12-06 03:01:24 +00:00
Move examples from root to /Examples
This commit is contained in:
129
Examples/Examples.WPF/Views/ConnectView.xaml
Normal file
129
Examples/Examples.WPF/Views/ConnectView.xaml
Normal file
@@ -0,0 +1,129 @@
|
||||
<UserControl x:Class="Examples.WPF.Views.ConnectView"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:Examples.WPF.Views"
|
||||
xmlns:vm="clr-namespace:Examples.WPF.ViewModels"
|
||||
mc:Ignorable="d"
|
||||
d:DataContext="{d:DesignInstance vm:ConnectViewViewModel, IsDesignTimeCreatable=True}"
|
||||
d:DesignHeight="450" d:DesignWidth="800">
|
||||
<Grid Margin="10">
|
||||
<StackPanel>
|
||||
|
||||
<StackPanel.Resources>
|
||||
<Style TargetType="TextBox">
|
||||
<Setter Property="Padding" Value="5"/>
|
||||
</Style>
|
||||
</StackPanel.Resources>
|
||||
|
||||
<StackPanel>
|
||||
<TextBlock Text="Connect to a PLC"
|
||||
FontSize="24"/>
|
||||
<Label Content="Set your connection type"/>
|
||||
<ComboBox SelectedIndex="0" x:Name="conTypeCombo">
|
||||
<ComboBoxItem>Ethernet</ComboBoxItem>
|
||||
<ComboBoxItem IsEnabled="{Binding HasComports}">Serial</ComboBoxItem>
|
||||
</ComboBox>
|
||||
</StackPanel>
|
||||
|
||||
<Separator/>
|
||||
|
||||
<StackPanel MinWidth="200">
|
||||
|
||||
<TextBlock Text="Cassettes"/>
|
||||
|
||||
<DataGrid ItemsSource="{Binding FoundCassettes}"
|
||||
SelectionChanged="SelectedCassette"
|
||||
AutoGenerateColumns="False"
|
||||
MinHeight="150"
|
||||
MaxHeight="200"
|
||||
IsReadOnly="True">
|
||||
<DataGrid.Columns>
|
||||
<DataGridTextColumn Header="Name" Binding="{Binding Name}" Width=".5*"/>
|
||||
<DataGridTextColumn Header="IP" Binding="{Binding IPAddress}" Width=".3*"/>
|
||||
<DataGridTextColumn Header="Port" Binding="{Binding Port}" Width=".3*"/>
|
||||
<DataGridCheckBoxColumn Header="DHCP" Binding="{Binding UsesDHCP}" Width="auto"/>
|
||||
<DataGridTextColumn Header="MAC" Binding="{Binding MacAddressStr, Mode=OneWay}" Width="auto"/>
|
||||
<DataGridTextColumn Header="Status" Binding="{Binding Status}" Width="auto"/>
|
||||
</DataGrid.Columns>
|
||||
<DataGrid.RowStyle>
|
||||
<Style TargetType="{x:Type DataGridRow}">
|
||||
<Style.Triggers>
|
||||
<DataTrigger Binding="{Binding Port}" Value="0">
|
||||
<Setter Property="IsEnabled" Value="False"/>
|
||||
</DataTrigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</DataGrid.RowStyle>
|
||||
</DataGrid>
|
||||
|
||||
<TextBlock Text="Connection"/>
|
||||
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<Label Content="IP Address"
|
||||
VerticalAlignment="Center"/>
|
||||
<TextBox Text="{Binding SelectedIP}"
|
||||
VerticalAlignment="Center"/>
|
||||
<Label Content="Port"
|
||||
VerticalAlignment="Center"/>
|
||||
<TextBox Text="{Binding SelectedPort}"
|
||||
VerticalAlignment="Center"/>
|
||||
<Button Content="Connect"
|
||||
Click="ClickedConnectEth"
|
||||
VerticalAlignment="Center"
|
||||
Padding="5"
|
||||
Margin="10,0,0,0">
|
||||
<Button.Style>
|
||||
<Style TargetType="Button">
|
||||
<Style.Triggers>
|
||||
<DataTrigger Binding="{Binding IsConnecting}">
|
||||
<Setter Property="IsEnabled" Value="False"/>
|
||||
</DataTrigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</Button.Style>
|
||||
</Button>
|
||||
</StackPanel>
|
||||
|
||||
<StackPanel.Style>
|
||||
<Style TargetType="StackPanel">
|
||||
<Setter Property="Visibility" Value="Collapsed"/>
|
||||
<Style.Triggers>
|
||||
<DataTrigger Binding="{Binding SelectedIndex, ElementName=conTypeCombo}" Value="0">
|
||||
<Setter Property="Visibility" Value="Visible"/>
|
||||
</DataTrigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</StackPanel.Style>
|
||||
|
||||
</StackPanel>
|
||||
|
||||
<StackPanel MinWidth="200"
|
||||
HorizontalAlignment="Left">
|
||||
|
||||
<StackPanel Orientation="Vertical">
|
||||
<Label Content="COM Port"/>
|
||||
<ComboBox ItemsSource="{Binding ComPorts}"
|
||||
SelectedIndex="0"/>
|
||||
<Label Content="BaudRate"/>
|
||||
<ComboBox ItemsSource="{Binding BaudRates}"
|
||||
SelectedIndex="0"/>
|
||||
</StackPanel>
|
||||
|
||||
<StackPanel.Style>
|
||||
<Style TargetType="StackPanel">
|
||||
<Setter Property="Visibility" Value="Collapsed"/>
|
||||
<Style.Triggers>
|
||||
<DataTrigger Binding="{Binding SelectedIndex, ElementName=conTypeCombo}" Value="1">
|
||||
<Setter Property="Visibility" Value="Visible"/>
|
||||
</DataTrigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</StackPanel.Style>
|
||||
|
||||
</StackPanel>
|
||||
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</UserControl>
|
||||
161
Examples/Examples.WPF/Views/ConnectView.xaml.cs
Normal file
161
Examples/Examples.WPF/Views/ConnectView.xaml.cs
Normal file
@@ -0,0 +1,161 @@
|
||||
using Examples.WPF.RegisterCollections;
|
||||
using Examples.WPF.ViewModels;
|
||||
using MewtocolNet;
|
||||
using MewtocolNet.ComCassette;
|
||||
using MewtocolNet.Logging;
|
||||
using MewtocolNet.Registers;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace Examples.WPF.Views;
|
||||
|
||||
/// <summary>
|
||||
/// Interaktionslogik für ConnectView.xaml
|
||||
/// </summary>
|
||||
public partial class ConnectView : UserControl {
|
||||
|
||||
private ConnectViewViewModel viewModel;
|
||||
|
||||
public ConnectView() {
|
||||
|
||||
InitializeComponent();
|
||||
viewModel = new ConnectViewViewModel();
|
||||
this.DataContext = viewModel;
|
||||
|
||||
Unloaded += (s, e) => viewModel.EndTimer();
|
||||
|
||||
}
|
||||
|
||||
private void SelectedCassette(object sender, SelectionChangedEventArgs e) {
|
||||
|
||||
var cassette = (CassetteInformation)((DataGrid)sender).SelectedItem;
|
||||
if (cassette == null) return;
|
||||
|
||||
viewModel.SelectedCassette(cassette);
|
||||
|
||||
}
|
||||
|
||||
private void ClickedConnectEth(object sender, RoutedEventArgs e) {
|
||||
|
||||
Application.Current.Dispatcher.BeginInvoke(async () => {
|
||||
|
||||
viewModel.IsConnecting = true;
|
||||
|
||||
var parsedInt = int.Parse(viewModel.SelectedPort);
|
||||
|
||||
IRegister<short> heartbeatSetter = null!;
|
||||
IRegister<bool> outputContactReference = null!;
|
||||
IRegister<bool> testBoolReference = null!;
|
||||
IRegister<Word> wordRefTest = null!;
|
||||
|
||||
//build a new interface
|
||||
App.ViewModel.Plc = Mewtocol.Ethernet(viewModel.SelectedIP, parsedInt)
|
||||
.WithPoller()
|
||||
.WithInterfaceSettings(setting => {
|
||||
|
||||
setting.TryReconnectAttempts = 10;
|
||||
setting.TryReconnectDelayMs = 2000;
|
||||
setting.SendReceiveTimeoutMs = 1000;
|
||||
setting.HeartbeatIntervalMs = 3000;
|
||||
setting.MaxDataBlocksPerWrite = 128;
|
||||
setting.MaxOptimizationDistance = 20;
|
||||
|
||||
})
|
||||
.WithCustomPollLevels(lvl => {
|
||||
|
||||
lvl.SetLevel(2, 3);
|
||||
lvl.SetLevel(3, TimeSpan.FromSeconds(5));
|
||||
lvl.SetLevel(4, TimeSpan.FromSeconds(10));
|
||||
|
||||
})
|
||||
.WithRegisterCollections(collector => {
|
||||
|
||||
App.ViewModel.TestRegCollection = collector.AddCollection<TestRegisterCollection>();
|
||||
|
||||
})
|
||||
.WithRegisters(b => {
|
||||
|
||||
b.Bool("X4").Build();
|
||||
b.Bool("Y4").Build(out outputContactReference);
|
||||
b.Bool("R10A").PollLevel(PollLevel.FirstIteration).Build(out testBoolReference);
|
||||
|
||||
b.Struct<short>("DT1000").Build(out heartbeatSetter);
|
||||
|
||||
//these will be merged into one
|
||||
b.Struct<Word>("DT1000").Build(out wordRefTest);
|
||||
b.Struct<Word>("DT1000").Build(out wordRefTest);
|
||||
|
||||
b.Struct<ushort>("DT1001").PollLevel(2).Build();
|
||||
b.Struct<Word>("DT1002").PollLevel(2).Build();
|
||||
|
||||
b.Struct<int>("DDT1010").PollLevel(2).Build();
|
||||
b.Struct<uint>("DDT1012").PollLevel(2).Build();
|
||||
b.Struct<DWord>("DDT1014").PollLevel(2).Build();
|
||||
b.Struct<float>("DDT1016").PollLevel(2).Build();
|
||||
b.Struct<TimeSpan>("DDT1018").PollLevel(2).Build();
|
||||
|
||||
b.Struct<DateAndTime>("DDT1020").PollLevel(2).Build();
|
||||
b.Struct<DateAndTime>("DDT1022").PollLevel(2).Build();
|
||||
|
||||
b.String("DT1028", 32).PollLevel(3).Build();
|
||||
b.String("DT1046", 5).PollLevel(4).Build();
|
||||
|
||||
b.Struct<Word>("DT1000").AsArray(5).PollLevel(1).Build();
|
||||
|
||||
})
|
||||
.WithHeartbeatTask(async (plc) => {
|
||||
|
||||
var randShort = (short)new Random().Next(short.MinValue, short.MaxValue);
|
||||
|
||||
//write direct
|
||||
//await heartbeatSetter.WriteAsync(randShort);
|
||||
//or by anonymous
|
||||
await plc.Register.Struct<short>("DT1000").WriteAsync(randShort);
|
||||
|
||||
//write a register without a reference
|
||||
bool randBool = new Random().Next(0, 2) == 1;
|
||||
await plc.Register.Bool("Y4").WriteAsync(randBool);
|
||||
|
||||
if (testBoolReference.Value != null)
|
||||
await testBoolReference.WriteAsync(!testBoolReference.Value.Value);
|
||||
|
||||
await plc.Register.Struct<DateAndTime>("DDT1022").WriteAsync(DateAndTime.FromDateTime(DateTime.UtcNow));
|
||||
|
||||
})
|
||||
.Build();
|
||||
|
||||
//connect to it
|
||||
await App.ViewModel.Plc.ConnectAsync(async () => {
|
||||
|
||||
await App.ViewModel.Plc.RestartProgramAsync();
|
||||
|
||||
});
|
||||
|
||||
await App.ViewModel.Plc.AwaitFirstDataCycleAsync();
|
||||
|
||||
if (App.ViewModel.Plc.IsConnected) {
|
||||
|
||||
App.MainWindow.mainContent.Content = new PlcDataView();
|
||||
|
||||
}
|
||||
|
||||
viewModel.IsConnecting = false;
|
||||
|
||||
}, System.Windows.Threading.DispatcherPriority.Send);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
350
Examples/Examples.WPF/Views/PlcDataView.xaml
Normal file
350
Examples/Examples.WPF/Views/PlcDataView.xaml
Normal file
@@ -0,0 +1,350 @@
|
||||
<UserControl x:Class="Examples.WPF.Views.PlcDataView"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:vm="clr-namespace:Examples.WPF.ViewModels"
|
||||
xmlns:local="clr-namespace:Examples.WPF.Views"
|
||||
mc:Ignorable="d"
|
||||
d:DataContext="{d:DesignInstance vm:PlcDataViewViewModel, IsDesignTimeCreatable=True}"
|
||||
d:DesignHeight="450"
|
||||
d:DesignWidth="800">
|
||||
<Grid>
|
||||
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="auto"/>
|
||||
<RowDefinition Height="auto"/>
|
||||
<RowDefinition/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<Menu>
|
||||
<MenuItem Header="PLC">
|
||||
<MenuItem Header="Disconnect" IsEnabled="{Binding Plc.IsConnected}"
|
||||
Click="ClickedDisconnect"/>
|
||||
<MenuItem Header="Connect" IsEnabled="{Binding Plc.IsConnected, Converter={StaticResource bInv}}"
|
||||
Click="ClickedConnect"/>
|
||||
<MenuItem Header="Set Random DT1000" IsEnabled="{Binding Plc.IsConnected}"
|
||||
Click="ClickedSetRandom"/>
|
||||
<MenuItem Header="Queue test" IsEnabled="{Binding Plc.IsConnected}"
|
||||
Click="ClickedAddQueueTest"/>
|
||||
<MenuItem Header="Toggle OP mode" IsEnabled="{Binding Plc.IsConnected}"
|
||||
Click="ClickedToggleRunMode"/>
|
||||
</MenuItem>
|
||||
</Menu>
|
||||
|
||||
<StackPanel Margin="10"
|
||||
Grid.Row="1">
|
||||
|
||||
<TextBlock IsEnabled="{Binding Plc.IsConnected}">
|
||||
|
||||
<Run Text="{Binding Plc.PlcInfo.TypeName, Mode=OneWay}"
|
||||
FontSize="24"
|
||||
BaselineAlignment="Center"
|
||||
FontWeight="SemiBold"/>
|
||||
|
||||
<Run Text="{Binding Plc.PlcInfo.CpuVersion, StringFormat='v{0}', Mode=OneWay}"
|
||||
FontSize="24"
|
||||
FontWeight="Light"/>
|
||||
|
||||
<Ellipse Width="10"
|
||||
Height="10"
|
||||
Fill="Lime"
|
||||
IsEnabled="{Binding Plc.IsConnected}"/>
|
||||
|
||||
<Run>
|
||||
<Run.Style>
|
||||
<Style TargetType="Run">
|
||||
<Style.Triggers>
|
||||
<DataTrigger Binding="{Binding Plc.IsRunMode, Mode=OneWay}" Value="True">
|
||||
<Setter Property="Text" Value="RUN MODE"/>
|
||||
</DataTrigger>
|
||||
<DataTrigger Binding="{Binding Plc.IsRunMode, Mode=OneWay}" Value="False">
|
||||
<Setter Property="Text" Value="NO RUN MODE"/>
|
||||
</DataTrigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</Run.Style>
|
||||
</Run>
|
||||
|
||||
<TextBlock.Style>
|
||||
<Style TargetType="TextBlock">
|
||||
<Style.Triggers>
|
||||
<Trigger Property="IsEnabled" Value="False">
|
||||
<Setter Property="Visibility" Value="Collapsed"/>
|
||||
</Trigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</TextBlock.Style>
|
||||
|
||||
</TextBlock>
|
||||
|
||||
<TextBlock IsEnabled="{Binding Plc.IsConnected, Converter={StaticResource bInv}}">
|
||||
|
||||
<Run Text="Disconnected"
|
||||
FontSize="24"
|
||||
BaselineAlignment="Center"
|
||||
FontWeight="SemiBold"/>
|
||||
|
||||
<Ellipse Width="10"
|
||||
Height="10"
|
||||
Fill="Red"
|
||||
IsEnabled="{Binding Plc.IsConnected}"/>
|
||||
|
||||
<TextBlock.Style>
|
||||
<Style TargetType="TextBlock">
|
||||
<Style.Triggers>
|
||||
<Trigger Property="IsEnabled" Value="False">
|
||||
<Setter Property="Visibility" Value="Collapsed"/>
|
||||
</Trigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</TextBlock.Style>
|
||||
|
||||
</TextBlock>
|
||||
|
||||
<TextBlock Text="{Binding Plc.PlcInfo.TypeCode, StringFormat='#{0:X}', Mode=OneWay}"
|
||||
FontSize="18"/>
|
||||
|
||||
<ItemsControl ItemsSource="{Binding Plc.PlcInfo.OperationModeTags, Mode=OneWay}">
|
||||
<ItemsControl.ItemsPanel>
|
||||
<ItemsPanelTemplate>
|
||||
<StackPanel Orientation="Horizontal"/>
|
||||
</ItemsPanelTemplate>
|
||||
</ItemsControl.ItemsPanel>
|
||||
<ItemsControl.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<Border Background="LightGray"
|
||||
CornerRadius="5"
|
||||
Margin="2"
|
||||
Padding="5">
|
||||
<TextBlock Text="{Binding}"/>
|
||||
</Border>
|
||||
</DataTemplate>
|
||||
</ItemsControl.ItemTemplate>
|
||||
</ItemsControl>
|
||||
|
||||
<ItemsControl ItemsSource="{Binding Plc.PlcInfo.HardwareInformationTags, Mode=OneWay}">
|
||||
<ItemsControl.ItemsPanel>
|
||||
<ItemsPanelTemplate>
|
||||
<StackPanel Orientation="Horizontal"/>
|
||||
</ItemsPanelTemplate>
|
||||
</ItemsControl.ItemsPanel>
|
||||
<ItemsControl.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<Border Background="LightGray"
|
||||
CornerRadius="5"
|
||||
Margin="2"
|
||||
Padding="5">
|
||||
<TextBlock Text="{Binding}"/>
|
||||
</Border>
|
||||
</DataTemplate>
|
||||
</ItemsControl.ItemTemplate>
|
||||
</ItemsControl>
|
||||
|
||||
<TextBlock>
|
||||
|
||||
<Run Text="Reconnecting..."/>
|
||||
<Run Text="{Binding PlcCurrentReconnectArgs.ReconnectTry, Mode=OneWay, StringFormat='{}{0}/'}"/>
|
||||
<Run Text="{Binding PlcCurrentReconnectArgs.MaxAttempts, Mode=OneWay}"/> in
|
||||
<Run Text="{Binding PlcCurrentReconnectArgs.RetryCountDownRemaining, Mode=OneWay}"/>
|
||||
|
||||
<TextBlock.Style>
|
||||
<Style TargetType="TextBlock">
|
||||
<Style.Triggers>
|
||||
<DataTrigger Binding="{Binding PlcCurrentReconnectArgs, Mode=OneWay}" Value="{x:Null}">
|
||||
<Setter Property="Visibility" Value="Collapsed"/>
|
||||
</DataTrigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</TextBlock.Style>
|
||||
|
||||
</TextBlock>
|
||||
|
||||
</StackPanel>
|
||||
|
||||
<Grid Grid.Row="2">
|
||||
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="1.5*"/>
|
||||
<ColumnDefinition Width="auto"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="auto"/>
|
||||
<RowDefinition/>
|
||||
<RowDefinition Height="auto"/>
|
||||
<RowDefinition/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<GridSplitter Grid.Column="1"
|
||||
Grid.Row="1"
|
||||
Grid.RowSpan="3"
|
||||
VerticalAlignment="Stretch"
|
||||
HorizontalAlignment="Stretch"
|
||||
Background="Gray"
|
||||
ShowsPreview="true"
|
||||
Width="10">
|
||||
<GridSplitter.Template>
|
||||
<ControlTemplate>
|
||||
<Rectangle Fill="Gray"
|
||||
Width="2"
|
||||
Margin="2"/>
|
||||
</ControlTemplate>
|
||||
</GridSplitter.Template>
|
||||
</GridSplitter>
|
||||
|
||||
<TextBlock Text="Underlying Registers"
|
||||
FontSize="18"
|
||||
Margin="10"/>
|
||||
|
||||
<DataGrid Grid.Row="1"
|
||||
Grid.RowSpan="3"
|
||||
AutoGenerateColumns="False"
|
||||
IsReadOnly="True"
|
||||
ItemsSource="{Binding Plc.Registers, Mode=OneWay}">
|
||||
<DataGrid.RowStyle>
|
||||
<Style TargetType="DataGridRow">
|
||||
<Style.Triggers>
|
||||
<DataTrigger Binding="{Binding IsAutoGenerated}" Value="True">
|
||||
<Setter Property="IsEnabled" Value="False"/>
|
||||
</DataTrigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</DataGrid.RowStyle>
|
||||
<DataGrid.Columns>
|
||||
<DataGridTextColumn Header="FP Address" Binding="{Binding PLCAddressName}"/>
|
||||
<DataGridTextColumn Header="Type" Binding="{Binding UnderlyingSystemType.Name}"/>
|
||||
<DataGridTemplateColumn Header="Value" Width="*">
|
||||
<DataGridTemplateColumn.CellTemplate>
|
||||
<DataTemplate>
|
||||
<Border HorizontalAlignment="Left"
|
||||
Padding="2,0">
|
||||
<TextBlock Text="{Binding ValueStr}"/>
|
||||
<Border.Style>
|
||||
<Style TargetType="Border">
|
||||
<Style.Triggers>
|
||||
<DataTrigger Binding="{Binding ValueStr}" Value="True">
|
||||
<Setter Property="Background" Value="Blue"/>
|
||||
<Setter Property="TextElement.Foreground" Value="White"/>
|
||||
</DataTrigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</Border.Style>
|
||||
</Border>
|
||||
</DataTemplate>
|
||||
</DataGridTemplateColumn.CellTemplate>
|
||||
</DataGridTemplateColumn>
|
||||
<DataGridTextColumn Header="Poll Level" Binding="{Binding PollLevel, Mode=OneWay}"/>
|
||||
<DataGridTextColumn Header="Update Frequency" Binding="{Binding UpdateFreqHz, StringFormat='{}{0} Hz',Mode=OneWay}"/>
|
||||
<DataGridTemplateColumn Width="15">
|
||||
<DataGridTemplateColumn.CellTemplate>
|
||||
<DataTemplate>
|
||||
<Border Background="{Binding MemoryArea, Mode=OneWay, Converter={StaticResource hashColor}}"/>
|
||||
</DataTemplate>
|
||||
</DataGridTemplateColumn.CellTemplate>
|
||||
</DataGridTemplateColumn>
|
||||
</DataGrid.Columns>
|
||||
</DataGrid>
|
||||
|
||||
<TextBlock Text="Memory Areas"
|
||||
Grid.Column="2"
|
||||
FontSize="18"
|
||||
Margin="10"/>
|
||||
|
||||
<Border Grid.Column="2"
|
||||
Grid.Row="1"
|
||||
VerticalAlignment="Stretch"
|
||||
HorizontalAlignment="Stretch"
|
||||
BorderBrush="LightBlue"
|
||||
BorderThickness="1.5">
|
||||
|
||||
<DataGrid IsReadOnly="True"
|
||||
AutoGenerateColumns="False"
|
||||
ItemsSource="{Binding Plc.MemoryAreas, Mode=OneWay}">
|
||||
|
||||
<DataGrid.Columns>
|
||||
|
||||
<DataGridTextColumn Header="Address Range" Binding="{Binding AddressRange, Mode=OneWay}"/>
|
||||
|
||||
<DataGridTemplateColumn Width="15">
|
||||
<DataGridTemplateColumn.CellTemplate>
|
||||
<DataTemplate>
|
||||
<Border Background="{Binding Converter={StaticResource hashColor}}"/>
|
||||
</DataTemplate>
|
||||
</DataGridTemplateColumn.CellTemplate>
|
||||
</DataGridTemplateColumn>
|
||||
|
||||
<DataGridTextColumn Header="Words" Binding="{Binding UnderlyingWordsString, Mode=OneWay}"/>
|
||||
|
||||
</DataGrid.Columns>
|
||||
|
||||
</DataGrid>
|
||||
|
||||
</Border>
|
||||
|
||||
<TextBlock Text="Property Bindings"
|
||||
Grid.Column="2"
|
||||
Grid.Row="2"
|
||||
FontSize="18"
|
||||
Margin="10"/>
|
||||
|
||||
<Border Grid.Column="2"
|
||||
Grid.Row="3"
|
||||
VerticalAlignment="Stretch"
|
||||
HorizontalAlignment="Stretch"
|
||||
BorderBrush="LightBlue"
|
||||
BorderThickness="1.5">
|
||||
|
||||
<ScrollViewer>
|
||||
|
||||
<StackPanel>
|
||||
|
||||
<TextBlock Text="Boolean internal relays"/>
|
||||
|
||||
<TextBlock>
|
||||
<Run Text="R11A Nullable property: "/>
|
||||
<Run Text="{Binding RegCollection.TestR11A}"
|
||||
FontWeight="Bold"/>
|
||||
</TextBlock>
|
||||
|
||||
<TextBlock>
|
||||
<Run Text="R11A Duplicate non nullable property: "/>
|
||||
<Run Text="{Binding RegCollection.TestR11A_Duplicate_NonNullable}"
|
||||
FontWeight="Bold"/>
|
||||
</TextBlock>
|
||||
|
||||
<TextBlock>
|
||||
<Run Text="R16B"/>
|
||||
<Run Text="{Binding RegCollection.TestR16B}"
|
||||
FontWeight="Bold"/>
|
||||
</TextBlock>
|
||||
|
||||
<TextBlock>
|
||||
<Run Text="DT1000 Word duplicate: "/>
|
||||
<Run Text="{Binding RegCollection.TestDT100_Word_Duplicate, Mode=OneWay}"
|
||||
FontWeight="Bold"/>
|
||||
</TextBlock>
|
||||
|
||||
<TextBlock>
|
||||
<Run Text="DT1000 Word direct bit read (Idx 0): "/>
|
||||
<Run Text="{Binding RegCollection.TestDT100_Word_Duplicate_SingleBit, Mode=OneWay}"
|
||||
FontWeight="Bold"/>
|
||||
</TextBlock>
|
||||
|
||||
<TextBlock>
|
||||
<Run Text="DDT1010 DWord direct bit read (Idx 1): "/>
|
||||
<Run Text="{Binding RegCollection.TestDDT1010_DWord_Duplicate_SingleBit, Mode=OneWay}"
|
||||
FontWeight="Bold"/>
|
||||
</TextBlock>
|
||||
|
||||
</StackPanel>
|
||||
|
||||
</ScrollViewer>
|
||||
|
||||
</Border>
|
||||
|
||||
</Grid>
|
||||
|
||||
</Grid>
|
||||
</UserControl>
|
||||
81
Examples/Examples.WPF/Views/PlcDataView.xaml.cs
Normal file
81
Examples/Examples.WPF/Views/PlcDataView.xaml.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using Examples.WPF.ViewModels;
|
||||
using MewtocolNet.Registers;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace Examples.WPF.Views;
|
||||
|
||||
public partial class PlcDataView : UserControl {
|
||||
|
||||
private PlcDataViewViewModel viewModel;
|
||||
|
||||
public PlcDataView() {
|
||||
|
||||
InitializeComponent();
|
||||
|
||||
viewModel = new PlcDataViewViewModel();
|
||||
this.DataContext = viewModel;
|
||||
|
||||
}
|
||||
|
||||
private void ClickedDisconnect(object sender, RoutedEventArgs e) {
|
||||
|
||||
viewModel.Plc.Disconnect();
|
||||
|
||||
}
|
||||
|
||||
private async void ClickedConnect(object sender, RoutedEventArgs e) {
|
||||
|
||||
await viewModel.Plc.ConnectAsync();
|
||||
|
||||
}
|
||||
|
||||
private async void ClickedSetRandom(object sender, RoutedEventArgs e) {
|
||||
|
||||
var reg = (IRegister<ushort>?)viewModel.Plc.GetAllRegisters()?.FirstOrDefault(x => x.PLCAddressName == "DT1001");
|
||||
|
||||
if(reg != null) {
|
||||
|
||||
await reg.WriteAsync((ushort)new Random().Next(ushort.MinValue, ushort.MaxValue));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private async void ClickedAddQueueTest(object sender, RoutedEventArgs e) {
|
||||
|
||||
var tasks = new List<Task<short>>();
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
|
||||
var t = viewModel.Plc.Register.Struct<short>("DT1000").ReadAsync();
|
||||
|
||||
tasks.Add(t);
|
||||
|
||||
}
|
||||
|
||||
var list = await Task.WhenAll(tasks);
|
||||
|
||||
Console.WriteLine();
|
||||
|
||||
}
|
||||
|
||||
private async void ClickedToggleRunMode(object sender, RoutedEventArgs e) {
|
||||
|
||||
await viewModel.Plc.ToggleOperationModeAsync();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user