mirror of
https://github.com/OpenLogics/MewtocolNet.git
synced 2025-12-06 03:01:24 +00:00
Fix multiple poller situational problems
This commit is contained in:
129
Examples.WPF/Views/ConnectView.xaml
Normal file
129
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>
|
||||
78
Examples.WPF/Views/ConnectView.xaml.cs
Normal file
78
Examples.WPF/Views/ConnectView.xaml.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using Examples.WPF.ViewModels;
|
||||
using MewtocolNet;
|
||||
using MewtocolNet.ComCassette;
|
||||
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);
|
||||
|
||||
App.ViewModel.Plc = Mewtocol.Ethernet(viewModel.SelectedIP, parsedInt)
|
||||
.WithPoller()
|
||||
.WithRegisters(b => {
|
||||
b.Struct<short>("DT0").Build();
|
||||
b.Struct<short>("DT0").AsArray(30).Build();
|
||||
})
|
||||
.Build();
|
||||
|
||||
await App.ViewModel.Plc.ConnectAsync();
|
||||
|
||||
if (App.ViewModel.Plc.IsConnected) {
|
||||
|
||||
App.MainWindow.mainContent.Content = new PlcDataView();
|
||||
|
||||
}
|
||||
|
||||
viewModel.IsConnecting = false;
|
||||
|
||||
}, System.Windows.Threading.DispatcherPriority.Send);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
76
Examples.WPF/Views/PlcDataView.xaml
Normal file
76
Examples.WPF/Views/PlcDataView.xaml
Normal file
@@ -0,0 +1,76 @@
|
||||
<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 Margin="10">
|
||||
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="auto"/>
|
||||
<RowDefinition/>
|
||||
<RowDefinition/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<StackPanel>
|
||||
|
||||
<TextBlock>
|
||||
|
||||
<Run Text="{Binding Plc.PlcInfo.TypeName, Mode=OneWay}"
|
||||
FontSize="24"
|
||||
FontWeight="SemiBold"/>
|
||||
|
||||
<Run Text="{Binding Plc.PlcInfo.CpuVersion, StringFormat='v{0}', Mode=OneWay}"
|
||||
FontSize="24"
|
||||
FontWeight="Light"/>
|
||||
|
||||
</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>
|
||||
|
||||
</StackPanel>
|
||||
|
||||
</Grid>
|
||||
</UserControl>
|
||||
32
Examples.WPF/Views/PlcDataView.xaml.cs
Normal file
32
Examples.WPF/Views/PlcDataView.xaml.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using Examples.WPF.ViewModels;
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user