首次上传
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
<UserControl x:Class="MECF.Framework.Simulator.Core.Vacuometer.VacuometerView"
|
||||
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>-->
|
||||
<CheckBox Content="IsSent" IsChecked="{Binding IsSent}" Canvas.Left="48" Canvas.Top="84" RenderTransformOrigin="1.288,-4.39"></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.Vacuometer
|
||||
{
|
||||
/// <summary>
|
||||
/// VacuometerView.xaml 的交互逻辑
|
||||
/// </summary>
|
||||
public partial class VacuometerView : UserControl
|
||||
{
|
||||
public static int CurrentTick = 0;
|
||||
public VacuometerView()
|
||||
{
|
||||
InitializeComponent();
|
||||
DataContext = new VacuometerViewModel((CurrentTick++ == 0) ? "COM52" : "COM54");
|
||||
|
||||
(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,40 @@
|
||||
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.Vacuometer
|
||||
{
|
||||
class VacuometerViewModel : SerialPortDeviceViewModel
|
||||
{
|
||||
private Vacuometers _reader;
|
||||
|
||||
public string ResultValue { get; set; }
|
||||
public bool IsSent
|
||||
{
|
||||
get
|
||||
{
|
||||
return _reader.IsSent;
|
||||
}
|
||||
set
|
||||
{
|
||||
_reader.IsSent = value;
|
||||
}
|
||||
}
|
||||
public VacuometerViewModel(string port) : base("VacuometerViewModel")
|
||||
{
|
||||
_reader = new Vacuometers(port);
|
||||
Init(_reader, true);
|
||||
_reader.receiveMsg += _reader_receiveMsg;
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void _reader_receiveMsg(string obj)
|
||||
{
|
||||
ResultValue = obj;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
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.Vacuometer
|
||||
{
|
||||
class Vacuometers : SerialPortDeviceSimulator
|
||||
{
|
||||
Stopwatch _timer = new Stopwatch();
|
||||
|
||||
private System.Timers.Timer _tick;
|
||||
Random _rd = new Random();
|
||||
|
||||
public event Action<string> receiveMsg;
|
||||
|
||||
private object _locker = new object();
|
||||
|
||||
public bool IsSent { get; set; }
|
||||
|
||||
public Vacuometers(string port)
|
||||
: base(port, -1, "\r", ' ', false)
|
||||
{
|
||||
// ResultValue = "";
|
||||
|
||||
_tick = new System.Timers.Timer();
|
||||
_tick.Interval = 30;
|
||||
|
||||
_tick.Elapsed += _tick_Elapsed;
|
||||
_tick.Start();
|
||||
}
|
||||
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;
|
||||
//}
|
||||
if (IsSent)
|
||||
{
|
||||
var buffer = new byte[] { 0x07, 0x02, 0x10, 0x00, 0x5A, 0xE9, 0x00, 0x06, 0x5B };
|
||||
OnWriteMessage(buffer);
|
||||
// Thread.Sleep(10);
|
||||
}
|
||||
}
|
||||
}
|
||||
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];
|
||||
byte command = (byte)message[1];
|
||||
byte AddressHigh = (byte)(message[2]);
|
||||
byte AddressLow = message[3];
|
||||
byte value = message[4];
|
||||
switch (command)
|
||||
{
|
||||
case 0x01:
|
||||
{
|
||||
switch (AddressLow)
|
||||
{
|
||||
case 0xA1:
|
||||
{
|
||||
var buffer = new byte[] { 0x01, 0x01, 0x01, 0x00 };
|
||||
var mess = ModRTU_CRC(buffer);
|
||||
OnWriteMessage(mess);
|
||||
}
|
||||
break;
|
||||
case 0xA2:
|
||||
{
|
||||
var buffer = new byte[] { 0x01, 0x01, 0x01, 0x01 };
|
||||
var mess = ModRTU_CRC(buffer);
|
||||
OnWriteMessage(mess);
|
||||
}
|
||||
break;
|
||||
case 0x40:
|
||||
{
|
||||
var buffer = new byte[] { 0x01, 0x01, 0x01, 0x00 };
|
||||
var mess = ModRTU_CRC(buffer);
|
||||
OnWriteMessage(mess);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 0x05:
|
||||
{
|
||||
switch (AddressLow)
|
||||
{
|
||||
case 0x60:
|
||||
{
|
||||
|
||||
OnWriteMessage(message);
|
||||
|
||||
receiveMsg("set Sevor " + (value == 0xff ? "ON" : "OFF"));
|
||||
}
|
||||
break;
|
||||
case 0x20:
|
||||
{
|
||||
OnWriteMessage(message);
|
||||
receiveMsg("set Stb " + (value == 0xff ? "ON" : "OFF"));
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 0x06:
|
||||
{
|
||||
OnWriteMessage(message);
|
||||
receiveMsg("set blocl no " + message[5]);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user