Browse Source

io页面功能

master
sc 1 year ago
parent
commit
30cf155f5b
  1. 32
      ConvertMoels/BoolRBConvert.cs
  2. 4
      DyeingComputer.csproj
  3. 148
      View/InOutView.xaml
  4. 18
      View/InOutView.xaml.cs
  5. 204
      ViewModel/MainWindowViewModel.cs

32
ConvertMoels/BoolRBConvert.cs

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
namespace DyeingComputer.ConvertMoels
{
internal class BoolRBConvert : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
String G = value.ToString();
// bool G = (bool)value;
if (G == "False")//判断单元格值是否为零
{
return "white";//零返回
}
else
{
return "red";//非零返回红色
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
}

4
DyeingComputer.csproj

@ -62,6 +62,7 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="ConvertMoels\BoolRBConvert.cs" />
<Compile Include="ConvertMoels\GramsSQLConvert.cs" />
<Compile Include="ConvertMoels\ProductTypeSQLConvert.cs" />
<Compile Include="ConvertMoels\RowToIndexConverter.cs" />
@ -303,6 +304,9 @@
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces">
<Version>8.0.0</Version>
</PackageReference>
<PackageReference Include="NModbus">
<Version>3.0.81</Version>
</PackageReference>
<PackageReference Include="ScottPlot.WPF">
<Version>5.0.37</Version>
</PackageReference>

148
View/InOutView.xaml

@ -3,10 +3,152 @@
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:DyeingComputer.View"
mc:Ignorable="d"
xmlns:local="clr-namespace:DyeingComputer.View"
xmlns:lang="clr-namespace:DyeingComputer.Properties"
xmlns:ConvertMoels="clr-namespace:DyeingComputer.ConvertMoels"
xmlns:viewmodel="clr-namespace:DyeingComputer.ViewModel"
mc:Ignorable="d" d:DataContext="{d:DesignInstance Type=viewmodel:MainWindowViewModel}"
d:DesignHeight="630" d:DesignWidth="1280" VerticalAlignment="Top">
<UserControl.Resources>
<ConvertMoels:BoolRBConvert x:Key="BoolRBConvert"/>
</UserControl.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<DataGrid x:Name="Grid_D" AlternationCount="2" IsReadOnly="True" SelectionChanged="Grid_D_SelectionChanged" d:ItemsSource="{d:SampleData ItemCount=256}"
Margin="0,0,0,0" ItemsSource="{Binding Path=SYSData_D,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False" MinColumnWidth="30"
HorizontalGridLinesBrush="#FFC9C9C9" VerticalGridLinesBrush="#FFC9C9C9" GridLinesVisibility="All" BorderBrush="#CCCCCC"
BorderThickness="1,1,1,1" ColumnHeaderHeight="40" HorizontalContentAlignment="Right" AllowDrop="False"
CanUserReorderColumns="False" CanUserSortColumns="False" CanUserResizeRows="False" CanUserResizeColumns="False"
CanUserAddRows="False" CanUserDeleteRows="False" HeadersVisibility ="Column" Grid.Column="0"
Background="White" SelectionMode="Single" FontSize="15" ScrollViewer.HorizontalScrollBarVisibility="Hidden">
<DataGrid.RowStyle >
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Height" Value="30" />
<Setter Property="FontSize" Value="25" />
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="#FFFFFFFF" />
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="#FFF0F0F0" />
</Trigger>
<Trigger Property="IsMouseOver" Value="False"/>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="MinWidth" Value="20"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="#FFC0C0C0"/>
<Setter Property="BorderBrush" Value="#FFC0C0C0"/>
<Setter Property="Foreground" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.Columns>
<!--列信息绑定-->
<DataGridTextColumn Header="{x:Static lang:Resources.Code}" Width="100" Binding="{Binding ID}" IsReadOnly="True"/>
<DataGridTextColumn Header="{x:Static lang:Resources.Directives}" Width="*" Binding="{Binding IOName}" IsReadOnly="True"/>
<DataGridTextColumn Header="{x:Static lang:Resources.ParameterSet}" Width="100" Binding="{Binding DIO}" IsReadOnly="True">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Background" Value="{Binding Path=DIO,Converter={StaticResource BoolRBConvert}}"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<DataGrid x:Name="Grid_A" AlternationCount="2" IsReadOnly="True" SelectionChanged="Grid_A_SelectionChanged" d:ItemsSource="{d:SampleData ItemCount=256}"
Margin="0,0,0,0" ItemsSource="{Binding Path=SYSData_A,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False" MinColumnWidth="30"
HorizontalGridLinesBrush="#FFC9C9C9" VerticalGridLinesBrush="#FFC9C9C9" GridLinesVisibility="All" BorderBrush="#CCCCCC"
BorderThickness="1,1,1,1" ColumnHeaderHeight="40" HorizontalContentAlignment="Right" AllowDrop="False"
CanUserReorderColumns="False" CanUserSortColumns="False" CanUserResizeRows="False" CanUserResizeColumns="False"
CanUserAddRows="False" CanUserDeleteRows="False" HeadersVisibility ="Column" Grid.Column="1"
Background="White" SelectionMode="Single" FontSize="15" ScrollViewer.HorizontalScrollBarVisibility="Hidden">
<DataGrid.RowStyle >
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Height" Value="30" />
<Setter Property="FontSize" Value="25" />
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="#FFFFFFFF" />
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="#FFF0F0F0" />
</Trigger>
<Trigger Property="IsMouseOver" Value="False"/>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="MinWidth" Value="20"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="#FFC0C0C0"/>
<Setter Property="BorderBrush" Value="#FFC0C0C0"/>
<Setter Property="Foreground" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.Columns>
<!--列信息绑定-->
<DataGridTextColumn Header="{x:Static lang:Resources.Code}" Width="100" Binding="{Binding ID}" IsReadOnly="True"/>
<DataGridTextColumn Header="{x:Static lang:Resources.Directives}" Width="*" Binding="{Binding IOName}" IsReadOnly="True"/>
<DataGridTextColumn Header="{x:Static lang:Resources.ParameterSet}" Width="100" Binding="{Binding AIO}" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
<DataGrid x:Name="Grid_M" AlternationCount="2" IsReadOnly="True" SelectionChanged="Grid_M_SelectionChanged" d:ItemsSource="{d:SampleData ItemCount=200}"
Margin="0,0,0,0" ItemsSource="{Binding Path=SYSData_M,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False" MinColumnWidth="30"
HorizontalGridLinesBrush="#FFC9C9C9" VerticalGridLinesBrush="#FFC9C9C9" GridLinesVisibility="All" BorderBrush="#CCCCCC"
BorderThickness="1,1,1,1" ColumnHeaderHeight="40" HorizontalContentAlignment="Right" AllowDrop="False"
CanUserReorderColumns="False" CanUserSortColumns="False" CanUserResizeRows="False" CanUserResizeColumns="False"
CanUserAddRows="False" CanUserDeleteRows="False" HeadersVisibility ="Column" Grid.Column="3"
Background="White" SelectionMode="Single" FontSize="15" ScrollViewer.HorizontalScrollBarVisibility="Hidden">
<DataGrid.RowStyle >
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Height" Value="30" />
<Setter Property="FontSize" Value="25" />
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="#FFFFFFFF" />
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="#FFF0F0F0" />
</Trigger>
<Trigger Property="IsMouseOver" Value="False"/>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="MinWidth" Value="20"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="#FFC0C0C0"/>
<Setter Property="BorderBrush" Value="#FFC0C0C0"/>
<Setter Property="Foreground" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.Columns>
<!--列信息绑定-->
<DataGridTextColumn Header="{x:Static lang:Resources.Code}" Width="100" Binding="{Binding ID}" IsReadOnly="True"/>
<DataGridTextColumn Header="{x:Static lang:Resources.Directives}" Width="*" Binding="{Binding IOName}" IsReadOnly="True"/>
<DataGridTextColumn Header="{x:Static lang:Resources.ParameterSet}" Width="100" Binding="{Binding Value}" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</UserControl>

18
View/InOutView.xaml.cs

@ -1,4 +1,5 @@
using System;
using DyeingComputer.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -24,5 +25,20 @@ namespace DyeingComputer.View
{
InitializeComponent();
}
private void Grid_D_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void Grid_A_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void Grid_M_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}
}

204
ViewModel/MainWindowViewModel.cs

@ -7,6 +7,15 @@ using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using CommonServiceLocator;
using System.Windows.Threading;
using static DyeingComputer.UserClass.SqliteHelper;
using System.Data;
using System.Collections.ObjectModel;
using DyeingComputer.View;
using System.Windows;
using DyeingComputer.UserClass;
using System.Reflection;
using System.Windows.Documents;
using System.Runtime.CompilerServices;
namespace DyeingComputer.ViewModel
{
@ -22,14 +31,24 @@ namespace DyeingComputer.ViewModel
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public void RaisePropertyChanged(string propertyName)
{
if (propertyName != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class MainWindowViewModel: ViewModelBase
public class MainWindowViewModel : ViewModelBase
{
public MainWindowViewModel()
public MainWindowViewModel()
{
WorkNumder = "----------";
CountDown();
SQL_data();
}
public string work_Temp; //显示温度
@ -64,24 +83,193 @@ namespace DyeingComputer.ViewModel
{
DispatcherTimer timer1s = new DispatcherTimer//初始化循环,每0.5秒调用一次Tick
{
Interval = TimeSpan.FromSeconds(1)
Interval = TimeSpan.FromSeconds(0.5)//秒
};
timer1s.Tick += Tick_Event_1S;
timer1s.Start();
//设置定时器
// disTimer.Tick += new EventHandler(DisTimer_Tick);//每一秒执行的方法
// disTimer.Interval = new TimeSpan(10000000); //时间间隔为一秒。
// disTimer.Start();//计时开始
DispatcherTimer disTimer = new DispatcherTimer
{
Interval = TimeSpan.FromMilliseconds(100) //毫秒
};
disTimer.Tick += new EventHandler(DisTimer_Tick);//每一秒执行的方法
disTimer.Start();//计时开始
}
public static object WorkNumder;
int lk;
void Tick_Event_1S(object sender, EventArgs e)//Tick_Event周期执行事件
{
Sys_Time = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
Work_Temp = "130"+" ℃";
Work_Temp = "130" + " ℃";
Work_Numder = WorkNumder.ToString();
Status_Str = "qweqeqwqqfwfqef";
IO_data();
}
void DisTimer_Tick(object sender, EventArgs e)//Tick_Event周期执行事件
{
lk++;
}
private SQLiteHelper SQLiteHelpers = null; //定义数据库
private readonly string DBAddress = Environment.CurrentDirectory + "\\DataBase\\800COMPUTER.db"; //数据库路径
DataTable dt_d = new DataTable("DIO");
DataTable dt_a = new DataTable("AIO");
DataTable dt_m = new DataTable("M");
void SQL_data()//获得io表
{
SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径
SQLiteHelpers.Open(); //打开数据库
dt_d = SQLiteHelpers.ExecuteDataSet("select * from IOName where type='DI' OR type='DO'", null).Tables[0]; //读取表写入缓存
dt_a = SQLiteHelpers.ExecuteDataSet("select * from IOName where type='AI' OR type='AO'", null).Tables[0];
dt_m = SQLiteHelpers.ExecuteDataSet("select * from IOName where type='M'", null).Tables[0];
SQLiteHelpers.Close();
}
void IO_data()//刷新
{
updata_dtm("1", lk);
updata_dtd("1", Convert.ToBoolean(1));
updata_dta("1",10000+lk);
SYSData_A = ToObservableCollection<DATA_A>(dt_a);
SYSData_D = ToObservableCollection<DATA_D>(dt_d);
SYSData_M = ToObservableCollection<DATA_M>(dt_m);
}
void updata_dtm(string key,int Value)//更新M寄存器
{
DataRow[] arrRows = dt_m.Select("ID="+key);
int index = dt_m.Rows.IndexOf(arrRows[0]);
DataRow drEmployee = dt_m.Rows[index];
drEmployee.BeginEdit();
drEmployee["Value"] = Value;
drEmployee.EndEdit();
}
void updata_dtd(string key, bool Value)//更新D寄存器
{
DataRow[] arrRows = dt_d.Select("ID=" + key);
int index = dt_d.Rows.IndexOf(arrRows[0]);
DataRow drEmployee = dt_d.Rows[index];
drEmployee.BeginEdit();
drEmployee["DIO"] = Value;
drEmployee.EndEdit();
}
void updata_dta(string key, int Value)//更新A寄存器
{
DataRow[] arrRows = dt_a.Select("ID=" + key);
int index = dt_a.Rows.IndexOf(arrRows[0]);
DataRow drEmployee = dt_a.Rows[index];
drEmployee.BeginEdit();
drEmployee["AIO"] = Value;
drEmployee.EndEdit();
}
ObservableCollection<DATA_A> sysData_A = new ObservableCollection<DATA_A>();
ObservableCollection<DATA_D> sysData_D = new ObservableCollection<DATA_D>();
ObservableCollection<DATA_M> sysData_M = new ObservableCollection<DATA_M>();
public ObservableCollection<DATA_A> SYSData_A
{
get { return sysData_A; }
set
{
sysData_A = value;
RaisePropertyChanged("SYSData_A");
}
}
public ObservableCollection<DATA_D> SYSData_D
{
get { return sysData_D; }
set
{
sysData_D = value;
RaisePropertyChanged("SYSData_D");
}
}
public ObservableCollection<DATA_M> SYSData_M
{
get { return sysData_M; }
set
{
sysData_M = value;
RaisePropertyChanged("SYSData_M");
}
}
public class DATA_A
{
public string IOName { get; set; }
public int AIO { get; set; }
public String ID { get; set; }
}
public class DATA_D
{
public string IOName { get; set; }
public bool DIO { get; set; }
public String ID { get; set; }
}
public class DATA_M
{
public string IOName { get; set; }
public int Value { get; set; }
public String ID { get; set; }
}
public ObservableCollection<T> ToObservableCollection<T>(DataTable dt) where T : class, new()
{
Type t = typeof(T);
PropertyInfo[] propertys = t.GetProperties();
ObservableCollection<T> lst = new ObservableCollection<T>();
string typeName = string.Empty;
foreach (DataRow dr in dt.Rows)
{
T entity = new T();
foreach (PropertyInfo pi in propertys)
{
typeName = pi.Name;
if (dt.Columns.Contains(typeName))
{
if (!pi.CanWrite) continue;
object value = dr[typeName];
if (value == DBNull.Value) continue;
if (pi.PropertyType == typeof(string))
{
pi.SetValue(entity, value.ToString(), null);
}
else if (pi.PropertyType == typeof(int) || pi.PropertyType == typeof(int?))
{
pi.SetValue(entity, int.Parse(value.ToString()), null);
}
else if (pi.PropertyType == typeof(DateTime?) || pi.PropertyType == typeof(DateTime))
{
pi.SetValue(entity, DateTime.Parse(value.ToString()), null);
}
else if (pi.PropertyType == typeof(float))
{
pi.SetValue(entity, float.Parse(value.ToString()), null);
}
else if (pi.PropertyType == typeof(double))
{
pi.SetValue(entity, double.Parse(value.ToString()), null);
}
else
{
pi.SetValue(entity, value, null);
}
}
}
lst.Add(entity);
}
return lst;
}
}
}
}
Loading…
Cancel
Save