首次上传
This commit is contained in:
@@ -0,0 +1,424 @@
|
||||
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.FFUs
|
||||
{
|
||||
class Ffu : 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();
|
||||
|
||||
public event Action<string> receiveMsg;
|
||||
|
||||
private object _locker = new object();
|
||||
|
||||
public string ResultValue { get; set; }
|
||||
|
||||
public Ffu(string port)
|
||||
: base(port, -1, "\r", ' ', false)
|
||||
{
|
||||
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 GetSpeedValue()
|
||||
{
|
||||
Random random = new Random();
|
||||
return (byte)random.Next(0, 255);
|
||||
}
|
||||
|
||||
protected override void ProcessUnsplitMessage(byte[] message)
|
||||
{
|
||||
if (CheckModRTU_CRC(message) != 0xff)
|
||||
{ return; }
|
||||
base.ProcessUnsplitMessage(message);
|
||||
byte prefix = message[0];
|
||||
byte command = (byte)(message[1] / 16 * 16);
|
||||
byte deviceAddress = (byte)(message[1] % 16);
|
||||
byte groupAddress = message[2];
|
||||
switch (message.Length)
|
||||
{
|
||||
case 4:
|
||||
switch (prefix)
|
||||
{
|
||||
case 0x15:
|
||||
if (command == 0x20)
|
||||
{
|
||||
byte speed = GetSpeedValue();
|
||||
byte[] speedmsg = new byte[] { 0x31, (byte)(deviceAddress + 0x20), groupAddress, speed, 0x00 };
|
||||
speedmsg[4] = ModRTU_CRC(speedmsg);
|
||||
ProcessWriteMessage(speedmsg);
|
||||
receiveMsg($"Query Speed!{speed * 1440 / 250}");
|
||||
}
|
||||
if (command == 0x60)
|
||||
{
|
||||
if (receiveMsg != null)
|
||||
{
|
||||
receiveMsg("Receive restart!");
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 5:
|
||||
switch (prefix)
|
||||
{
|
||||
case 0x35:
|
||||
QueryMsg(message, prefix, command, deviceAddress, groupAddress);
|
||||
break;
|
||||
case 0x55:
|
||||
SetMsg(message, prefix, command, deviceAddress, groupAddress);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 6:
|
||||
switch (prefix)
|
||||
{
|
||||
case 0x55:
|
||||
SetMsg(message, prefix, command, deviceAddress, groupAddress);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 查询参数,含风速设置
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <param name="prefix"></param>
|
||||
/// <param name="command"></param>
|
||||
/// <param name="deviceAddress"></param>
|
||||
/// <param name="groupAddress"></param>
|
||||
private void QueryMsg(byte[] message, byte prefix, byte command, byte deviceAddress, byte groupAddress)
|
||||
{
|
||||
switch (command)
|
||||
{
|
||||
case 0x00:
|
||||
byte[] errormsg = new byte[] { 0x51, (byte)(deviceAddress + 0x00), groupAddress, 0x00, 0x00, 0x00 };
|
||||
errormsg[5] = ModRTU_CRC(errormsg);
|
||||
ProcessWriteMessage(errormsg);
|
||||
receiveMsg("Query Error!");//查询故障状态
|
||||
break;
|
||||
case 0xE0:
|
||||
if (message[3] == 0)
|
||||
{
|
||||
byte[] queryGroupymsg = new byte[] { 0x51, (byte)(deviceAddress + 0xE0), groupAddress, 0x05, 0x00, 0x00 };
|
||||
queryGroupymsg[5] = ModRTU_CRC(queryGroupymsg);
|
||||
ProcessWriteMessage(queryGroupymsg);
|
||||
receiveMsg("Query Group ddress!");//查询组地址
|
||||
}
|
||||
else if (message[3] == 1)
|
||||
{
|
||||
byte[] queryGroupymsg = new byte[] { 0x51, (byte)(deviceAddress + 0xE0), groupAddress, 0x05, 0x01, 0x00 };
|
||||
queryGroupymsg[5] = ModRTU_CRC(queryGroupymsg);
|
||||
ProcessWriteMessage(queryGroupymsg);
|
||||
receiveMsg("Query Device Address!");//查询机地址
|
||||
}
|
||||
else if (message[3] == 0x08)
|
||||
{
|
||||
byte[] queryMaxSpeed1msg = new byte[] { 0x51, (byte)(deviceAddress + 0xE0), groupAddress, 0x05, 0x08, 0x00 };
|
||||
queryMaxSpeed1msg[5] = ModRTU_CRC(queryMaxSpeed1msg);
|
||||
ProcessWriteMessage(queryMaxSpeed1msg);
|
||||
receiveMsg("Query MaxSpeed1!");//查询最高转速 1
|
||||
}
|
||||
else if (message[3] == 0x09)
|
||||
{
|
||||
byte[] queryMaxSpeed2msg = new byte[] { 0x51, (byte)(deviceAddress + 0xE0), groupAddress, 0x05, 0x09, 0x00 };
|
||||
queryMaxSpeed2msg[5] = ModRTU_CRC(queryMaxSpeed2msg);
|
||||
ProcessWriteMessage(queryMaxSpeed2msg);
|
||||
receiveMsg("Query MaxSpeed2!");//查询最高转速 2
|
||||
}
|
||||
else if (message[3] == 0x0A)
|
||||
{
|
||||
byte[] queryMaxSpeed3msg = new byte[] { 0x51, (byte)(deviceAddress + 0xE0), groupAddress, 0x05, 0x0a, 0x00 };
|
||||
queryMaxSpeed3msg[5] = ModRTU_CRC(queryMaxSpeed3msg);
|
||||
ProcessWriteMessage(queryMaxSpeed3msg);
|
||||
receiveMsg("Query MaxSpeed3!");//查询最高转速 3
|
||||
}
|
||||
else if (message[3] == 0x11)
|
||||
{
|
||||
byte[] querySpeedFactormsg = new byte[] { 0x51, (byte)(deviceAddress + 0xE0), groupAddress, 0x05, 0x11, 0x00 };
|
||||
querySpeedFactormsg[5] = ModRTU_CRC(querySpeedFactormsg);
|
||||
ProcessWriteMessage(querySpeedFactormsg);
|
||||
receiveMsg("Query Speed Factor!");//查询转速因子
|
||||
}
|
||||
break;
|
||||
case 0x40:
|
||||
byte[] setSpeedmsg = new byte[] { 0x11, (byte)(deviceAddress + 0x40), groupAddress, 0x00 };
|
||||
setSpeedmsg[3] = ModRTU_CRC(setSpeedmsg);
|
||||
ProcessWriteMessage(setSpeedmsg);
|
||||
receiveMsg($"Set Speed {message[3]}!");//设置风速
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void SetMsg(byte[] message, byte prefix, byte command, byte deviceAddress, byte groupAddress)
|
||||
{
|
||||
switch (message[3])
|
||||
{
|
||||
case 0x00://设置组地址
|
||||
byte[] returnGroupAddressMsg = new byte[] { 0x11, (byte)(deviceAddress + 0xC0), groupAddress, 00};
|
||||
returnGroupAddressMsg[3] = ModRTU_CRC(returnGroupAddressMsg);
|
||||
ProcessWriteMessage(returnGroupAddressMsg);
|
||||
receiveMsg($"Set Group Address{message[4]}!");
|
||||
break;
|
||||
case 0x01://设置机地址
|
||||
byte[] returnDeviceAddressMsg = new byte[] { 0x11, (byte)(deviceAddress + 0xC0), groupAddress, 00 };
|
||||
returnDeviceAddressMsg[3] = ModRTU_CRC(returnDeviceAddressMsg);
|
||||
ProcessWriteMessage(returnDeviceAddressMsg);
|
||||
receiveMsg($"Set Device Address{message[4]}!");
|
||||
break;
|
||||
case 0x11://设置转速因子
|
||||
byte[] returnSpeedFactorMsg = new byte[] { 0x11, (byte)(deviceAddress + 0xC0), groupAddress, 00 };
|
||||
returnSpeedFactorMsg[3] = ModRTU_CRC(returnSpeedFactorMsg);
|
||||
ProcessWriteMessage(returnSpeedFactorMsg);
|
||||
receiveMsg($"Set Device Address{message[4]}!");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static byte ModRTU_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;
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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,105 @@
|
||||
<UserControl x:Class="MECF.Framework.Simulator.Core.FFUs.FfuView"
|
||||
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="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,61 @@
|
||||
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.FFUs
|
||||
{
|
||||
/// <summary>
|
||||
/// FfuView.xaml 的交互逻辑
|
||||
/// </summary>
|
||||
public partial class FfuView : UserControl
|
||||
{
|
||||
public static int CurrentTick = 0;
|
||||
public FfuView()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
|
||||
DataContext = new FfuViewModel((CurrentTick++==0) ? "COM32":"COM34");
|
||||
|
||||
(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,30 @@
|
||||
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.FFUs
|
||||
{
|
||||
class FfuViewModel : SerialPortDeviceViewModel
|
||||
{
|
||||
private Ffu _reader;
|
||||
|
||||
public string ResultValue { get; set; }
|
||||
|
||||
public FfuViewModel(string port) : base("FfuViewModel")
|
||||
{
|
||||
_reader = new Ffu(port);
|
||||
Init(_reader, true);
|
||||
_reader.receiveMsg += _reader_receiveMsg;
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void _reader_receiveMsg(string obj)
|
||||
{
|
||||
ResultValue = obj;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Timers;
|
||||
using MECF.Framework.Simulator.Core.Driver;
|
||||
|
||||
namespace MECF.Framework.Simulator.Core.FFUs.MayAir
|
||||
{
|
||||
class SimFfuMayAir : 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 SimFfuMayAir(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 (IsAtSpeed)
|
||||
{
|
||||
//if (_timer.Elapsed > TimeSpan.FromSeconds(10))
|
||||
//{
|
||||
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");
|
||||
}
|
||||
//}
|
||||
//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,75 @@
|
||||
<UserControl x:Class="MECF.Framework.Simulator.Core.FFUs.MayAir.SimFfuMayAirView"
|
||||
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: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,119 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using Aitex.Core.UI.MVVM;
|
||||
using Aitex.Core.Utilities;
|
||||
using MECF.Framework.Simulator.Core.Commons;
|
||||
|
||||
namespace MECF.Framework.Simulator.Core.FFUs.MayAir
|
||||
{
|
||||
|
||||
public partial class SimFfuMayAirView : 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 SimFfuMayAirView()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
DataContext = new SimFfuMayAirViewModel("COM22");
|
||||
|
||||
(DataContext as TimerViewModelBase).Start();
|
||||
|
||||
this.Loaded += OnViewLoaded;
|
||||
}
|
||||
|
||||
private void OnViewLoaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (DataContext == null)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class SimFfuMayAirViewModel : SerialPortDeviceViewModel
|
||||
{
|
||||
public string Title
|
||||
{
|
||||
get { return "MayAir FFU Simulator"; }
|
||||
}
|
||||
|
||||
private SimFfuMayAir _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 SimFfuMayAirViewModel(string port) : base("SimFfuMayAirViewModel")
|
||||
{
|
||||
_reader = new SimFfuMayAir(port);
|
||||
Init(_reader, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user