refactor: 将Framework库和RT、UI放在同一个解决方案中。

This commit is contained in:
SL
2026-06-01 17:48:28 +08:00
parent 1948f4290d
commit b73d39a6b1
4331 changed files with 506148 additions and 49363 deletions
@@ -0,0 +1,236 @@
using MECF.Framework.Simulator.Core.Driver;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
namespace MECF.Framework.Simulator.Core.Pumps.DRYVacuum
{
class DRYVacuumPump : SerialPortDeviceSimulator
{
Stopwatch _timer = new Stopwatch();
private System.Timers.Timer _tick;
Random _rd = new Random();
public event Action<string> receiveMsg;
public bool IsRunS { get; set; }
public bool IsMPS { get; set; }
public bool IsBPS { get; set; }
public bool IsAlarmRondom { get; set; }
public bool IsAlarm { get; set; }
private object _locker = new object();
public DRYVacuumPump(string port)
: base(port, -1, "\r", ' ', false)
{
// ResultValue = "";
_tick = new System.Timers.Timer();
_tick.Interval = 200;
_tick.Elapsed += _tick_Elapsed;
_tick.Start();
IsRunS = false;
IsMPS = false;
IsBPS = false;
IsAlarmRondom = false;
IsAlarm = false;
}
private byte[] GetRandomValue()
{
Thread.Sleep(20);
Random random = new Random();
return Encoding.ASCII.GetBytes(random.Next(16).ToString());
}
private byte GetValue()
{
Random random = new Random();
return (byte)random.Next(16);
}
private void _tick_Elapsed(object sender, ElapsedEventArgs e)
{
lock (_locker)
{
if (_timer.IsRunning && _timer.Elapsed > TimeSpan.FromSeconds(10))
{
_timer.Stop();
//IsAtSpeed = true;
}
}
}
protected override void ProcessUnsplitMessage(byte[] message)
{
if (message.Length != 8)
{ return; }
//var arry = ModRTU_CRC(message.Take(6).ToArray()).Skip(2).ToArray();
//if (CheckModRTU_CRC(arry) != CheckModRTU_CRC(message.Skip(2).ToArray()))
//{ return; }
base.ProcessUnsplitMessage(message);
byte prefix = message[0];
var mw = message.Take(4);
var mws = mw.Skip(1);
string command = Encoding.ASCII.GetString(message.Take(4).Skip(1).ToArray());
byte AddressHigh = (byte)(message[2]);
byte AddressLow = message[3];
switch (command)
{
case "M21":
{
byte[] speedmsg = new byte[27];
speedmsg[0] = message[0];
speedmsg[1] = message[1];
speedmsg[2] = message[2];
speedmsg[3] = message[3];
if (IsRunS)
speedmsg[4] = Encoding.ASCII.GetBytes("S")[0];
else speedmsg[4] = Encoding.ASCII.GetBytes("N")[0];
if (IsMPS)
speedmsg[5] = Encoding.ASCII.GetBytes("S")[0];
else speedmsg[5] = Encoding.ASCII.GetBytes("R")[0];
if (IsBPS)
speedmsg[6] = Encoding.ASCII.GetBytes("S")[0];
else speedmsg[6] = Encoding.ASCII.GetBytes("R")[0];
if (IsAlarmRondom)
{
speedmsg[7] = GetRandomValue()[0];
speedmsg[8] = GetRandomValue()[0];
speedmsg[9] = GetRandomValue()[0];
speedmsg[10] = GetRandomValue()[0];
speedmsg[11] = GetRandomValue()[0];
speedmsg[12] = GetRandomValue()[0];
speedmsg[13] = GetRandomValue()[0];
speedmsg[14] = GetRandomValue()[0];
speedmsg[15] = GetRandomValue()[0];
speedmsg[16] = GetRandomValue()[0];
speedmsg[17] = GetRandomValue()[0];
speedmsg[18] = GetRandomValue()[0];
speedmsg[19] = GetRandomValue()[0];
speedmsg[20] = GetRandomValue()[0];
speedmsg[21] = GetRandomValue()[0];
speedmsg[22] = GetRandomValue()[0];
}
else
{
if (IsAlarm)
{
speedmsg[7] = Encoding.ASCII.GetBytes("0")[0];
speedmsg[8] = Encoding.ASCII.GetBytes("0")[0];
speedmsg[9] = Encoding.ASCII.GetBytes("0")[0];
speedmsg[10] = Encoding.ASCII.GetBytes("F")[0];
speedmsg[11] = Encoding.ASCII.GetBytes("0")[0];
speedmsg[12] = Encoding.ASCII.GetBytes("0")[0];
speedmsg[13] = Encoding.ASCII.GetBytes("2")[0];
speedmsg[14] = Encoding.ASCII.GetBytes("0")[0];
speedmsg[15] = Encoding.ASCII.GetBytes("0")[0];
speedmsg[16] = Encoding.ASCII.GetBytes("0")[0]; ;
speedmsg[17] = Encoding.ASCII.GetBytes("0")[0];
speedmsg[18] = Encoding.ASCII.GetBytes("4")[0];
speedmsg[19] = Encoding.ASCII.GetBytes("0")[0];
speedmsg[20] = Encoding.ASCII.GetBytes("0")[0];
speedmsg[21] = Encoding.ASCII.GetBytes("2")[0];
speedmsg[22] = Encoding.ASCII.GetBytes("3")[0];
}
else
{
speedmsg[7] = Encoding.ASCII.GetBytes("0")[0];
speedmsg[8] = Encoding.ASCII.GetBytes("0")[0];
speedmsg[9] = Encoding.ASCII.GetBytes("0")[0];
speedmsg[10] = Encoding.ASCII.GetBytes("0")[0];
speedmsg[11] = Encoding.ASCII.GetBytes("0")[0];
speedmsg[12] = Encoding.ASCII.GetBytes("0")[0];
speedmsg[13] = Encoding.ASCII.GetBytes("0")[0];
speedmsg[14] = Encoding.ASCII.GetBytes("0")[0];
speedmsg[15] = Encoding.ASCII.GetBytes("0")[0];
speedmsg[16] = Encoding.ASCII.GetBytes("0")[0]; ;
speedmsg[17] = Encoding.ASCII.GetBytes("0")[0];
speedmsg[18] = Encoding.ASCII.GetBytes("0")[0];
speedmsg[19] = Encoding.ASCII.GetBytes("0")[0];
speedmsg[20] = Encoding.ASCII.GetBytes("0")[0];
speedmsg[21] = Encoding.ASCII.GetBytes("0")[0];
speedmsg[22] = Encoding.ASCII.GetBytes("0")[0];
}
}
speedmsg[23] =0x03;
var CheckLow = SUMCHECK(speedmsg.Take(24).ToArray());
speedmsg[24] = Convert.ToByte(CheckLow[0]); ;
speedmsg[25] = Convert.ToByte(CheckLow[1]); ;
speedmsg[26] =0x0D;
OnWriteMessage(speedmsg);
}
break;
}
}
public byte[] ModRTU_CRC(byte[] value)
{
var val = 0xFFFF;
for (int i = 0; i < value.Length; i++)
{
val ^= value[i];
for (int j = 0; j < 8; j++)
{
var xdabit = (int)(val & 0x01);
val >>= 1;
if (xdabit == 1)
val ^= 0xA001;
}
}
if (val == 0x0000)
return value;
var arg = value.ToList();
arg.Add((byte)(val & 0xFF));
arg.Add((byte)(val >> 8));
return arg.ToArray();
}
public static string SUMCHECK(byte[] buffer)
{
// byte[] buffer = Encoding.ASCII.GetBytes(command);
int sum = 0;
for (int i = 0; i < buffer.Length; i++)
{
sum += Convert.ToInt16(buffer[i]);
}
var SUM = 0x02 + sum + 0x03;
return (SUM & 0xff).ToString("X2");
}
private static byte CheckModRTU_CRC(byte[] buffer)
{
//ushort crc = 0xFFFF;
// var buf = System.Text.Encoding.UTF8.GetBytes(String.Join(Environment.NewLine, buffer));
var buf = buffer;
var len = buffer.Length;
byte temp = buffer[0];
for (int i = 1; i < buffer.Length; i++)
{
temp = (byte)(temp ^ buffer[i]);
}
return (byte)temp;
}
}
}
@@ -0,0 +1,109 @@
<UserControl x:Class="MECF.Framework.Simulator.Core.Pumps.DRYVacuum.DRYVacuumPumpView"
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:MECF.Framework.Simulator.Core.FFUs"
mc:Ignorable="d"
xmlns:commons="clr-namespace:MECF.Framework.Simulator.Core.Commons"
Height="900" Width="900" Background="LightBlue" Initialized="UserControl_Initialized">
<UserControl.Resources>
<commons:ConnectionStatusBackgroundConverter x:Key="connectionStatusBackgroundConverter"/>
<Style TargetType="Label">
<Setter Property="FontSize" Value="16" />
<Setter Property="Padding" Value="10,0,10,0" />
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="150"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="400"></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
</Grid.RowDefinitions>
<Label Grid.Row="0" Content="{Binding Title}" FontSize="20" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Height="50" Background="{Binding IsConnected, Converter={StaticResource connectionStatusBackgroundConverter}}" ></Label>
<StackPanel Grid.Row="1" Orientation="Horizontal">
<ComboBox Name="PortName" Text="{Binding PortSetPoint}" VerticalContentAlignment="Center" Width="120" Margin="20,0,0,0" Height="35" />
<Label Content="Listen port:"></Label>
<Label Content="{Binding LocalPort}"></Label>
<Label Content="{Binding ConnectionStatus}"></Label>
<Button Width="100" Height="35" Content="Enable" Command="{Binding EnableCommand}" IsEnabled="{Binding IsEnableEnable}"></Button>
<Button Width="100" Height="35" Content="Disable" Command="{Binding DisableCommand}" IsEnabled="{Binding IsEnableDisable}"></Button>
<Label Content="Connection:"></Label>
<Label Content="{Binding RemoteConnection}"></Label>
</StackPanel>
</Grid>
<Canvas Grid.Row="1" Width="900">
<!--<TextBox Text="{Binding ActualTemp, UpdateSourceTrigger=PropertyChanged }" Canvas.Left="250" Canvas.Top="19" Margin="0" Width="94"></TextBox>-->
<CheckBox Content=" Power-saving operation mode" IsChecked="{Binding IsRunS}" Canvas.Left="56" Canvas.Top="28" RenderTransformOrigin="1.288,-4.39"></CheckBox>
<!--<TextBlock Text="SettingTemp:" Canvas.Left="151" Canvas.Top="88" ></TextBlock>
<TextBox Text="{Binding SettingTemp, UpdateSourceTrigger=PropertyChanged }" Canvas.Left="250" Canvas.Top="76" Margin="0" Width="94"></TextBox>-->
<CheckBox Content="MP-S(being stopped)" IsChecked="{Binding IsMPS}" Canvas.Left="56" Canvas.Top="65" RenderTransformOrigin="1.288,-4.39"></CheckBox>
<CheckBox Content="BP-S(being stopped)" IsChecked="{Binding IsBPS}" Canvas.Left="56" Canvas.Top="103" RenderTransformOrigin="1.288,-4.39"></CheckBox>
<CheckBox Content="AlarmRondom" IsChecked="{Binding IsAlarmRondom}" Canvas.Left="458" Canvas.Top="28" RenderTransformOrigin="1.288,-4.39"></CheckBox>
<CheckBox Content="Alarm" IsChecked="{Binding IsAlarm}" Canvas.Left="458" Canvas.Top="54" RenderTransformOrigin="1.288,-4.39"></CheckBox>
<!-- <TextBlock Text="Result:" Canvas.Left="155" Canvas.Top="42"></TextBlock>
<TextBox Text="{Binding ResultValue, UpdateSourceTrigger=PropertyChanged }" Canvas.Left="212" Canvas.Top="31" Margin="0" Width="284"></TextBox>-->
</Canvas>
<StackPanel Grid.Row="2" Orientation="Horizontal" Width="900">
<Button Content="Clear Log" Width="100" Height="35" Command="{Binding ClearLogCommand}"></Button>
</StackPanel>
<DataGrid Grid.Row="3" FontSize="16" AutoGenerateColumns="False" CanUserAddRows="False" CanUserResizeRows="False" CanUserSortColumns="False" ItemsSource="{Binding TransactionLogItems}"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
Width="900" Height="380" VerticalAlignment="Top">
<DataGrid.Columns>
<DataGridTextColumn Header="Time" Width="160" IsReadOnly="True" Binding="{Binding OccurTime, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn Header="Incoming" Width="350" IsReadOnly="True" Binding="{Binding Incoming, UpdateSourceTrigger=PropertyChanged}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="Height" Value="auto"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="Outgoing" Width="350" IsReadOnly="True" Binding="{Binding Outgoing, UpdateSourceTrigger=PropertyChanged}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="Height" Value="auto"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</UserControl>
@@ -0,0 +1,58 @@
using Aitex.Core.UI.MVVM;
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 MECF.Framework.Simulator.Core.Pumps.DRYVacuum
{
/// <summary>
/// DRYVacuumPumpView.xaml 的交互逻辑
/// </summary>
public partial class DRYVacuumPumpView : UserControl
{
public static int CurrentTick = 0;
public DRYVacuumPumpView()
{
InitializeComponent();
DataContext = new DRYVacuumPumpViewModel((CurrentTick++ == 0) ? "COM42" : "COM46");
(DataContext as TimerViewModelBase).Start();
this.Loaded += OnViewLoaded;
}
private void OnViewLoaded(object sender, RoutedEventArgs e)
{
if (DataContext == null)
{
}
}
private void UserControl_Initialized(object sender, EventArgs e)
{
InitSerialPort();
}
//GetPortNames()方法:获取当前计算机的串行端口名的数组
private void InitSerialPort()
{
PortName.Items.Clear();
foreach (string com in System.IO.Ports.SerialPort.GetPortNames())
{
PortName.Items.Add(com);
}
PortName.SelectedIndex = 0;
}
}
}
@@ -0,0 +1,86 @@
using MECF.Framework.Simulator.Core.Commons;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MECF.Framework.Simulator.Core.Pumps.DRYVacuum
{
class DRYVacuumPumpViewModel : SerialPortDeviceViewModel
{
private DRYVacuumPump _reader;
public string ResultValue { get; set; }
public bool IsRunS
{
get
{
return _reader.IsRunS;
}
set
{
_reader.IsRunS = value;
}
}
public bool IsMPS
{
get
{
return _reader.IsMPS;
}
set
{
_reader.IsMPS = value;
}
}
public bool IsBPS
{
get
{
return _reader.IsBPS;
}
set
{
_reader.IsBPS = value;
}
}
public bool IsAlarmRondom
{
get
{
return _reader.IsAlarmRondom;
}
set
{
_reader.IsAlarmRondom = value;
}
}
public bool IsAlarm
{
get
{
return _reader.IsAlarm;
}
set
{
_reader.IsAlarm = value;
}
}
public DRYVacuumPumpViewModel(string port) : base("DRYVacuumPumpViewModel")
{
_reader = new DRYVacuumPump(port);
Init(_reader, true);
_reader.receiveMsg += _reader_receiveMsg;
}
private void _reader_receiveMsg(string obj)
{
ResultValue = obj;
}
}
}
@@ -0,0 +1,76 @@
<UserControl x:Class="MECF.Framework.Simulator.Core.Pumps.Magpower.MagpowerPumpSimulatorView"
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:MECF.Framework.Simulator.Core.Pumps"
mc:Ignorable="d"
xmlns:commons="clr-namespace:MECF.Framework.Simulator.Core.Commons"
Height="900" Width="900">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="150"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="400"></RowDefinition>
</Grid.RowDefinitions>
<commons:SocketTitleView Grid.Row="0"></commons:SocketTitleView>
<Canvas Grid.Row="1" Width="900">
<CheckBox Content="Fail" IsChecked="{Binding IsFailed}" Canvas.Left="36" Canvas.Top="31"></CheckBox>
<CheckBox Content="Pump on" IsChecked="{Binding IsPumpOn}" Canvas.Left="36" Canvas.Top="86"></CheckBox>
<CheckBox Content="OverTemp" IsChecked="{Binding IsOverTemp}" Canvas.Left="172" Canvas.Top="86"></CheckBox>
<CheckBox Content="At Speed" IsChecked="{Binding IsAtSpeed}" Canvas.Left="324" Canvas.Top="90"></CheckBox>
<TextBlock Text="Result:" Canvas.Left="155" Canvas.Top="42"></TextBlock>
<TextBox Text="{Binding ResultValue, UpdateSourceTrigger=PropertyChanged }" Canvas.Left="212" Canvas.Top="31" Margin="0" Width="284"></TextBox>
</Canvas>
<StackPanel Grid.Row="2" Orientation="Horizontal" Width="900">
<Button Content="Clear Log" Width="100" Height="35" Command="{Binding ClearLogCommand}"></Button>
</StackPanel>
<DataGrid Grid.Row="3" FontSize="16" AutoGenerateColumns="False" CanUserAddRows="False" CanUserResizeRows="False" CanUserSortColumns="False" ItemsSource="{Binding TransactionLogItems}"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
Width="900" Height="380" VerticalAlignment="Top">
<DataGrid.Columns>
<DataGridTextColumn Header="Time" Width="160" IsReadOnly="True" Binding="{Binding OccurTime, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn Header="Incoming" Width="350" IsReadOnly="True" Binding="{Binding Incoming, UpdateSourceTrigger=PropertyChanged}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="Height" Value="auto"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="Outgoing" Width="350" IsReadOnly="True" Binding="{Binding Outgoing, UpdateSourceTrigger=PropertyChanged}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="Height" Value="auto"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</UserControl>
@@ -0,0 +1,117 @@
using Aitex.Core.UI.MVVM;
using Aitex.Core.Utilities;
using MECF.Framework.Simulator.Core.Commons;
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 MECF.Framework.Simulator.Core.Pumps.Magpower
{
/// <summary>
/// MagpowerPumpSimulatorView.xaml 的交互逻辑
/// </summary>
public partial class MagpowerPumpSimulatorView : UserControl
{
public MagpowerPumpSimulatorView()
{
InitializeComponent();
this.DataContext = new MagpowerPumpSimulatorViewModel("COM22");
this.Loaded += MagpowerPumpSimulatorView_Loaded;
}
private void MagpowerPumpSimulatorView_Loaded(object sender, RoutedEventArgs e)
{
(DataContext as MagpowerPumpSimulatorViewModel).EnableTimer(true);
}
}
class MagpowerPumpSimulatorViewModel : SerialPortDeviceViewModel
{
public string Title
{
get { return "Magpower Pump Simulator"; }
}
private MagpowerSimulator _reader;
public bool IsFailed
{
get
{
return _reader.Failed;
}
set
{
_reader.Failed = value;
}
}
public bool IsPumpOn
{
get
{
return _reader.IsPumpOn;
}
set
{
_reader.IsPumpOn = value;
}
}
public bool IsOverTemp
{
get
{
return _reader.IsOverTemp;
}
set
{
_reader.IsOverTemp = value;
}
}
public bool IsAtSpeed
{
get
{
return _reader.IsAtSpeed;
}
set
{
_reader.IsAtSpeed = value;
}
}
//private string _value;
[IgnorePropertyChange]
public string ResultValue
{
get
{
return _reader.ResultValue;
}
set
{
_reader.ResultValue = value;
}
}
public MagpowerPumpSimulatorViewModel(string port) : base("MagpowerPumpSimulatorViewModel")
{
_reader = new MagpowerSimulator(port);
Init(_reader);
}
}
}
@@ -0,0 +1,238 @@
using MECF.Framework.Simulator.Core.Driver;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace MECF.Framework.Simulator.Core.Pumps.Magpower
{
class MagpowerSimulator : SerialPortDeviceSimulator
{
public bool Failed { get; set; }
public bool IsPumpOn { get; set; }
public bool IsNormalSpeed { get; set; }
public bool IsOverTemp { get; set; }
public bool IsAtSpeed { get; set; }
Stopwatch _timer = new Stopwatch();
private System.Timers.Timer _tick;
Random _rd = new Random();
private object _locker = new object();
public string ResultValue { get; set; }
public MagpowerSimulator(string port)
: base(port, -1, "\r", ' ')
{
ResultValue = "";
_tick = new System.Timers.Timer();
_tick.Interval = 200;
_tick.Elapsed += _tick_Elapsed;
_tick.Start();
IsAtSpeed = true;
}
private void _tick_Elapsed(object sender, ElapsedEventArgs e)
{
lock (_locker)
{
if (_timer.IsRunning && _timer.Elapsed > TimeSpan.FromSeconds(10))
{
_timer.Stop();
IsAtSpeed = true;
}
}
}
private Byte[] GetBytesFromBinaryString(String binary)
{
var list = new List<Byte>();
for (int i = 0; i < binary.Length; i += 8)
{
String t = binary.Substring(i, 8);
list.Add(Convert.ToByte(t, 2));
}
return list.ToArray();
}
private string GetCharsFormFromBinaryString(String binary)
{
return Encoding.ASCII.GetString(GetBytesFromBinaryString(binary));
}
private int _speed = 1200;
int iStep = 0;
protected override void ProcessUnsplitMessage(string msg)
{
//if (!Failed)
{
iStep++;
if (msg.Contains("TMPON"))
{
lock (_locker)
{
if (!IsPumpOn)
{
IsPumpOn = true;
IsAtSpeed = false;
_timer.Restart();
OnWriteMessage("#001,OK\r");
}
else
{
OnWriteMessage("#001,Err3\r");
}
}
return;
}
if (msg.Contains("TMPOFF"))
{
lock (_locker)
{
if (IsPumpOn)
{
IsPumpOn = false;
IsAtSpeed = false;
_timer.Restart();
OnWriteMessage("#001,OK\r");
}
else
{
OnWriteMessage("#001,Err3\r");
}
}
return;
}
if (msg.Contains("NSP"))
{
lock (_locker)
{
if (!IsNormalSpeed)
{
IsNormalSpeed = true;
_timer.Restart();
OnWriteMessage("#001,OK\r");
}
else
{
OnWriteMessage("#001,Err3\r");
}
}
return;
}
if (msg.Contains("SBY"))
{
lock (_locker)
{
if (IsNormalSpeed)
{
IsNormalSpeed = false;
_timer.Restart();
OnWriteMessage("#001,OK\r");
}
else
{
OnWriteMessage("#001,Err3\r");
}
}
return;
}
if (msg.Contains("DEF"))
{
lock (_locker)
{
_timer.Restart();
string error = iStep % 10 < 5 ? "Err2,Err3" : "OK";
OnWriteMessage($"#001,{error}\r");
}
return;
}
if (msg.Contains("SPD"))
{
lock (_locker)
{
_timer.Restart();
string sSpeed = _speed.ToString("D6");
OnWriteMessage($"#001,{sSpeed} rpm\r");
_speed++;
if(_speed > 1210)
{
_speed = 1200;
}
}
return;
}
if (msg.Contains("STA"))
{
lock (_locker)
{
_timer.Restart();
string pumpStatus1 = GetCharsFormFromBinaryString("00101011");
string pumpStatus2 = GetCharsFormFromBinaryString("00000111");
string pumpStatus = iStep % 10 <5 ? pumpStatus2 : pumpStatus1;
string pumpTemp = iStep % 10 < 5 ? "025" : "030";
OnWriteMessage($"#001,s{pumpStatus}s,rrrrr,vvv,www,xxx,yyy,zzz,aa,bbbbb,{pumpTemp},ddd,ggggggggggggggggggggggggg\r");
}
return;
}
if (msg.Contains("SEL10"))
{
lock (_locker)
{
_timer.Restart();
string r = iStep % 10 < 5 ? "1" : "0";
OnWriteMessage($"#001,a,u,1,b,{r}\r");
}
return;
}
}
}
}
}
@@ -0,0 +1,225 @@
using MECF.Framework.Simulator.Core.Driver;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace MECF.Framework.Simulator.Core.Pumps.PfeifferHipace
{
class SimHipaceTurboPump : SerialPortDeviceSimulator
{
public bool Failed { get; set; }
public bool IsPumpOn { get; set; }
public bool IsOverTemp { get; set; }
public bool IsAtSpeed { get; set; }
Stopwatch _timer = new Stopwatch();
private System.Timers.Timer _tick;
Random _rd = new Random();
private object _locker = new object();
public string ResultValue { get; set; }
public SimHipaceTurboPump(string port)
: base(port, -1, "\r", ' ')
{
ResultValue = "";
_tick = new System.Timers.Timer();
_tick.Interval = 200;
_tick.Elapsed += _tick_Elapsed;
_tick.Start();
IsAtSpeed = true;
}
private void _tick_Elapsed(object sender, ElapsedEventArgs e)
{
lock (_locker)
{
if (_timer.IsRunning && _timer.Elapsed > TimeSpan.FromSeconds(10))
{
_timer.Stop();
IsAtSpeed = true;
}
}
}
protected override void ProcessUnsplitMessage(string msg)
{
//if (!Failed)
{
if (msg == "0010030302=?101\r")
{
//OnWriteMessage("0011030306000000014" + "\r" );
string text = "0011030306" + (Failed ? _rd.Next(0, 999).ToString("D6") : "000000");
int sum = 0;
foreach (var item in text)
{
sum += (int)item;
}
sum = sum % 256;
OnWriteMessage(text + sum.ToString("D3") + "\r");
return;
}
if (msg == "0010039802=?115\r")
{
string text = "0011039806" + _rd.Next(0, 999999).ToString("D6");
int sum = 0;
foreach (var item in text)
{
sum += (int)item;
}
sum = sum % 256;
OnWriteMessage(text + sum.ToString("D3") + "\r");
return;
}
if (msg == "0010034602=?108\r")
{
string text = "0011034606" + _rd.Next(0, 999999).ToString("D6");
int sum = 0;
foreach (var item in text)
{
sum += (int)item;
}
sum = sum % 256;
OnWriteMessage(text + sum.ToString("D3") + "\r");
return;
}
if (msg == "0011001006111111015\r")
{
lock (_locker)
{
if (!IsPumpOn)
{
IsPumpOn = true;
IsAtSpeed = false;
_timer.Restart();
}
}
OnWriteMessage("0011001006111111015" + "\r");
return;
}
if (msg == "0011001006000000009\r")
{
lock (_locker)
{
if (IsPumpOn)
{
IsPumpOn = false;
IsAtSpeed = false;
_timer.Restart();
}
}
OnWriteMessage("0011001006000000009" + "\r");
return;
}
if (msg == "0010001002=?096\r")
{
string text = "0011001006" + (IsPumpOn ? "111111" : "000000");
int sum = 0;
foreach (var item in text)
{
sum += (int)item;
}
sum = sum % 256;
OnWriteMessage(text + sum.ToString("D3") + "\r");
return;
}
if (msg == "0010030502=?103\r")
{
string text = "0011030506" + (IsOverTemp ? "111111" : "000000");
int sum = 0;
foreach (var item in text)
{
sum += (int)item;
}
sum = sum % 256;
OnWriteMessage(text + sum.ToString("D3") + "\r");
return;
}
if (msg == "0010030602=?104\r")
{
string text = "0011030606" + (IsAtSpeed ? "111111" : "000000");
int sum = 0;
foreach (var item in text)
{
sum += (int)item;
}
sum = sum % 256;
OnWriteMessage(text + sum.ToString("D3") + "\r");
return;
}
if (msg == "0010030702=?105\r")
{
string text = "0011030706" + (IsAtSpeed ? "000000" : "111111");
int sum = 0;
foreach (var item in text)
{
sum += (int)item;
}
sum = sum % 256;
OnWriteMessage(text + sum.ToString("D3") + "\r");
return;
}
}
}
}
}
@@ -0,0 +1,76 @@
<UserControl x:Class="MECF.Framework.Simulator.Core.Pumps.PfeifferHipace.SimHipaceTurboPumpView"
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:MECF.Framework.Simulator.Core.Pumps.PfeifferHipace"
mc:Ignorable="d"
xmlns:commons="clr-namespace:MECF.Framework.Simulator.Core.Commons"
Height="900" Width="900">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="150"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="400"></RowDefinition>
</Grid.RowDefinitions>
<commons:SocketTitleView Grid.Row="0"></commons:SocketTitleView>
<Canvas Grid.Row="1" Width="900">
<CheckBox Content="Fail" IsChecked="{Binding IsFailed}" Canvas.Left="36" Canvas.Top="31"></CheckBox>
<CheckBox Content="Pump on" IsChecked="{Binding IsPumpOn}" Canvas.Left="36" Canvas.Top="86"></CheckBox>
<CheckBox Content="OverTemp" IsChecked="{Binding IsOverTemp}" Canvas.Left="172" Canvas.Top="86"></CheckBox>
<CheckBox Content="At Speed" IsChecked="{Binding IsAtSpeed}" Canvas.Left="324" Canvas.Top="90"></CheckBox>
<TextBlock Text="Result:" Canvas.Left="155" Canvas.Top="42"></TextBlock>
<TextBox Text="{Binding ResultValue, UpdateSourceTrigger=PropertyChanged }" Canvas.Left="212" Canvas.Top="31" Margin="0" Width="284"></TextBox>
</Canvas>
<StackPanel Grid.Row="2" Orientation="Horizontal" Width="900">
<Button Content="Clear Log" Width="100" Height="35" Command="{Binding ClearLogCommand}"></Button>
</StackPanel>
<DataGrid Grid.Row="3" FontSize="16" AutoGenerateColumns="False" CanUserAddRows="False" CanUserResizeRows="False" CanUserSortColumns="False" ItemsSource="{Binding TransactionLogItems}"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
Width="900" Height="380" VerticalAlignment="Top">
<DataGrid.Columns>
<DataGridTextColumn Header="Time" Width="160" IsReadOnly="True" Binding="{Binding OccurTime, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn Header="Incoming" Width="350" IsReadOnly="True" Binding="{Binding Incoming, UpdateSourceTrigger=PropertyChanged}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="Height" Value="auto"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="Outgoing" Width="350" IsReadOnly="True" Binding="{Binding Outgoing, UpdateSourceTrigger=PropertyChanged}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="Height" Value="auto"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</UserControl>
@@ -0,0 +1,129 @@
using Aitex.Core.UI.MVVM;
using Aitex.Core.Utilities;
using MECF.Framework.Simulator.Core.Commons;
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 MECF.Framework.Simulator.Core.Pumps.PfeifferHipace
{
public partial class SimHipaceTurboPumpView : UserControl
{
//public static readonly DependencyProperty PortProperty = DependencyProperty.Register(
// "Port", typeof(string), typeof(SimHipaceTurboPumpView),
// new FrameworkPropertyMetadata("COM11", FrameworkPropertyMetadataOptions.AffectsRender));
//public string Port
//{
// get { return (string) this.GetValue(PortProperty); }
// set { this.SetValue(PortProperty, value); }
//}
public SimHipaceTurboPumpView()
{
InitializeComponent();
DataContext = new SimHipaceTurboPumpViewModel("COM11");
(DataContext as TimerViewModelBase).Start();
this.Loaded += OnViewLoaded;
}
private void OnViewLoaded(object sender, RoutedEventArgs e)
{
if (DataContext == null)
{
}
}
}
class SimHipaceTurboPumpViewModel : SerialPortDeviceViewModel
{
public string Title
{
get { return "Sim Hipace Turbo Pump Simulator"; }
}
private SimHipaceTurboPump _reader;
public bool IsFailed
{
get
{
return _reader.Failed;
}
set
{
_reader.Failed = value;
}
}
public bool IsPumpOn
{
get
{
return _reader.IsPumpOn;
}
set
{
_reader.IsPumpOn = value;
}
}
public bool IsOverTemp
{
get
{
return _reader.IsOverTemp;
}
set
{
_reader.IsOverTemp = value;
}
}
public bool IsAtSpeed
{
get
{
return _reader.IsAtSpeed;
}
set
{
_reader.IsAtSpeed = value;
}
}
//private string _value;
[IgnorePropertyChange]
public string ResultValue
{
get
{
return _reader.ResultValue;
}
set
{
_reader.ResultValue = value;
}
}
public SimHipaceTurboPumpViewModel(string port) : base("SimHipaceTurboPumpViewModel")
{
_reader = new SimHipaceTurboPump(port);
Init(_reader);
}
}
}