refactor: 将Framework库和RT、UI放在同一个解决方案中。
This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
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.Temperature
|
||||
{
|
||||
class OmronTemp : SerialPortDeviceSimulator
|
||||
{
|
||||
Stopwatch _timer = new Stopwatch();
|
||||
|
||||
private System.Timers.Timer _tick;
|
||||
Random _rd = new Random();
|
||||
|
||||
public event Action<string> receiveMsg;
|
||||
|
||||
public bool IsSetting { get; set; }
|
||||
|
||||
public bool IsActual { get; set; }
|
||||
public int ActualTemp { get; set; }
|
||||
public int SettingTemp { get; set; }
|
||||
|
||||
private object _locker = new object();
|
||||
|
||||
public OmronTemp(string port)
|
||||
: base(port, -1, "\r", ' ', false)
|
||||
{
|
||||
// ResultValue = "";
|
||||
|
||||
_tick = new System.Timers.Timer();
|
||||
_tick.Interval = 200;
|
||||
|
||||
_tick.Elapsed += _tick_Elapsed;
|
||||
_tick.Start();
|
||||
|
||||
IsActual = false;
|
||||
IsSetting = false;
|
||||
SettingTemp = 50;
|
||||
ActualTemp = 60;
|
||||
|
||||
}
|
||||
private byte GetActualTempValue()
|
||||
{
|
||||
Random random = new Random();
|
||||
return (byte)random.Next(0, 10000);
|
||||
}
|
||||
private byte GetSettingTempValue()
|
||||
{
|
||||
Random random = new Random();
|
||||
return (byte)random.Next(0, 10000);
|
||||
}
|
||||
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 deviceId = message[0];
|
||||
byte command = (byte)message[1];
|
||||
byte AddressHigh = (byte)(message[2]);
|
||||
byte AddressLow = message[3];
|
||||
switch (command)
|
||||
{
|
||||
case 0x03:
|
||||
switch (AddressLow)
|
||||
{
|
||||
case 0x00:
|
||||
{
|
||||
|
||||
// if(IsActual)
|
||||
var index =(deviceId-1)*4+ (AddressHigh /2-1);
|
||||
ActualTemp = Act[index] +5;
|
||||
byte high = (byte)(ActualTemp >> 8 & 0xff);
|
||||
byte low = (byte)(ActualTemp & 0xff);
|
||||
byte[] speedmsg = new byte[] { deviceId, 0x03, 0x02, high, low};
|
||||
speedmsg = ModRTU_CRC(speedmsg);
|
||||
OnWriteMessage(speedmsg);
|
||||
}
|
||||
break;
|
||||
case 0x40:
|
||||
{
|
||||
var index = (deviceId - 1) * 4 + (AddressHigh / 2 - 1);
|
||||
SettingTemp = Setting[index]; ;
|
||||
byte high = (byte)(SettingTemp >> 8 & 0xff);
|
||||
byte low = (byte)(SettingTemp & 0xff);
|
||||
byte[] speedmsg = new byte[] { deviceId, 0x03, 0x02, high, low };
|
||||
speedmsg = ModRTU_CRC(speedmsg);
|
||||
OnWriteMessage(speedmsg);
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
break;
|
||||
case 0x10:
|
||||
{
|
||||
var index = (deviceId - 1) * 4 + (AddressHigh / 2 - 1);
|
||||
var value = BitConverter.ToInt16(new byte[] {message[8],message[7] },0);
|
||||
|
||||
Act[index]= value;
|
||||
Setting[index] = value;
|
||||
OnWriteMessage(ModRTU_CRC(message.Take(6).ToArray()));
|
||||
}
|
||||
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();
|
||||
}
|
||||
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;
|
||||
}
|
||||
public int[] Act = new int[12] {30,30,30,40,50,30,45,36,48,90,20,45};
|
||||
public int[] Setting= new int[12] { 30, 30, 30, 40, 50, 30, 45, 36, 48, 90, 20, 45 };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
<UserControl x:Class="MECF.Framework.Simulator.Core.Temperature.OmronTempView"
|
||||
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">
|
||||
|
||||
|
||||
<!--<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="ActualTemp:" Canvas.Left="155" Canvas.Top="28" ></TextBlock>
|
||||
<TextBox Text="{Binding ActualTemp, UpdateSourceTrigger=PropertyChanged }" Canvas.Left="250" Canvas.Top="19" Margin="0" Width="94"></TextBox>
|
||||
<CheckBox Content="ActualTemp" IsChecked="{Binding IsActual}" Canvas.Left="360" Canvas.Top="29" 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="SettingTemp" IsChecked="{Binding IsSetting}" Canvas.Left="360" Canvas.Top="88" 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.Temperature
|
||||
{
|
||||
/// <summary>
|
||||
/// OmronTemp.xaml 的交互逻辑
|
||||
/// </summary>
|
||||
public partial class OmronTempView : UserControl
|
||||
{
|
||||
public static int CurrentTick = 0;
|
||||
public OmronTempView()
|
||||
{
|
||||
InitializeComponent();
|
||||
DataContext = new OmronTempViewModel((CurrentTick++ == 0) ? "COM44" : "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,74 @@
|
||||
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.Temperature
|
||||
{
|
||||
class OmronTempViewModel
|
||||
: SerialPortDeviceViewModel
|
||||
{
|
||||
private OmronTemp _reader;
|
||||
|
||||
public string ResultValue { get; set; }
|
||||
public bool IsSetting
|
||||
{
|
||||
get
|
||||
{
|
||||
return _reader.IsSetting;
|
||||
}
|
||||
set
|
||||
{
|
||||
_reader.IsSetting = value;
|
||||
}
|
||||
}
|
||||
public bool IsActual
|
||||
{
|
||||
get
|
||||
{
|
||||
return _reader.IsActual;
|
||||
}
|
||||
set
|
||||
{
|
||||
_reader.IsActual = value;
|
||||
}
|
||||
}
|
||||
public int ActualTemp
|
||||
{
|
||||
get
|
||||
{
|
||||
return _reader.ActualTemp;
|
||||
}
|
||||
set
|
||||
{
|
||||
_reader.ActualTemp = value;
|
||||
}
|
||||
}
|
||||
public int SettingTemp
|
||||
{
|
||||
get
|
||||
{
|
||||
return _reader.SettingTemp;
|
||||
}
|
||||
set
|
||||
{
|
||||
_reader.SettingTemp = value;
|
||||
}
|
||||
}
|
||||
public OmronTempViewModel(string port) : base("OmronTempViewModel")
|
||||
{
|
||||
_reader = new OmronTemp(port);
|
||||
Init(_reader, true);
|
||||
_reader.receiveMsg += _reader_receiveMsg;
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void _reader_receiveMsg(string obj)
|
||||
{
|
||||
ResultValue = obj;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user