Skip to content

Commit

Permalink
Merge pull request #4 from Helevn/LReal
Browse files Browse the repository at this point in the history
模拟器增加 双精度LReal 读写的功能
  • Loading branch information
newbienewbie authored Dec 25, 2024
2 parents b4f7198 + 74e36ab commit ec25a17
Show file tree
Hide file tree
Showing 12 changed files with 222 additions and 35 deletions.
76 changes: 41 additions & 35 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,56 +128,62 @@ DB.WriteShort(200, 2062, 1)
Python可以使用的`DB`其实是一个`IS7DataBlockService`接口对象:

```C#
public interface IS7DataBlockService
{
bool ReadBit(int dbNumber, int offset, byte bit);
void WriteBit(int dbNumber, int offset, byte bit, bool flag);
public interface IS7DataBlockService
{
bool ReadBit(int dbNumber, int offset, byte bit);
void WriteBit(int dbNumber, int offset, byte bit, bool flag);

byte ReadByte(int dbNumber, int pos);
void WriteByte(int dbNumber, int pos, byte value);
byte ReadByte(int dbNumber, int pos);
void WriteByte(int dbNumber, int pos, byte value);

short ReadShort(int dbNumber, int pos);
void WriteShort(int dbNumber, int pos, short value);
short ReadShort(int dbNumber, int pos);
void WriteShort(int dbNumber, int pos, short value);

uint ReadUInt32(int dbNumber, int pos);
void WriteUInt32(int dbNumber, int pos, uint value);
uint ReadUInt32(int dbNumber, int pos);
void WriteUInt32(int dbNumber, int pos, uint value);

ulong ReadULong(int dbNumber, int pos);
void WriteULong(int dbNumber, int pos, ulong value);

float ReadReal(int dbNumber, int pos);
void WriteReal(int dbNumber, int pos, float real);
ulong ReadULong(int dbNumber, int pos);
void WriteULong(int dbNumber, int pos, ulong value);

string ReadString(int dbNumber, int offset);
void WriteString(int dbNumber, int offset, int maxlen, string str);
}
float ReadReal(int dbNumber, int pos);
void WriteReal(int dbNumber, int pos, float real);

double ReadLReal(int dbNumber, int pos);
void WriteLReal(int dbNumber, int pos, double real);

string ReadString(int dbNumber, int offset);
void WriteString(int dbNumber, int offset, int maxlen, string str);
}
```

`MB`则是一个`IS7MBService`:
```C#
public interface IS7MBService
{
bool ReadBit(int offset, byte bit);
void WriteBit(int offset, byte bit, bool flag);
public interface IS7MBService
{
bool ReadBit(int offset, byte bit);
void WriteBit(int offset, byte bit, bool flag);

byte ReadByte(int pos);
void WriteByte(int pos, byte value);

byte ReadByte(int pos);
void WriteByte(int pos, byte value);
short ReadShort(int pos);
void WriteShort(int pos, short value);

short ReadShort(int pos);
void WriteShort(int pos, short value);
uint ReadUInt32(int pos);
void WriteUInt32(int pos, uint value);

uint ReadUInt32(int pos);
void WriteUInt32(int pos, uint value);
ulong ReadULong(int pos);
void WriteULong(int pos, ulong value);

ulong ReadULong(int pos);
void WriteULong(int pos, ulong value);
float ReadReal(int pos);
void WriteReal(int pos, float real);

float ReadReal(int pos);
void WriteReal(int pos, float real);
double ReadLReal(int pos);
void WriteLReal(int pos, double real);

string ReadString(int offset);
void WriteString(int offset, int maxlen, string str);
}
string ReadString(int offset);
void WriteString(int offset, int maxlen, string str);
}
```

#### `Logger`
Expand Down
2 changes: 2 additions & 0 deletions src/S7SvrSim/Exts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public static void RegisterViews(this IServiceProvider sp)
Locator.CurrentMutable.RegisterLazySingletonEx<RwUInt32VM>(sp);
Locator.CurrentMutable.RegisterLazySingletonEx<RwUInt64VM>(sp);
Locator.CurrentMutable.RegisterLazySingletonEx<RwRealVM>(sp);
Locator.CurrentMutable.RegisterLazySingletonEx<RwLRealVM>(sp);
Locator.CurrentMutable.RegisterLazySingletonEx<RwStringVM>(sp);

Locator.CurrentMutable.RegisterLazySingletonEx<ScriptTaskWindowVM>(sp);
Expand All @@ -61,6 +62,7 @@ public static void RegisterViews(this IServiceProvider sp)
Locator.CurrentMutable.Register<IViewFor<RwUInt32VM>, UInt32OpsView>();
Locator.CurrentMutable.Register<IViewFor<RwUInt64VM>, UInt64OpsView>();
Locator.CurrentMutable.Register<IViewFor<RwRealVM>, RealOpsView>();
Locator.CurrentMutable.Register<IViewFor<RwLRealVM>, LRealOpsView>();
Locator.CurrentMutable.Register<IViewFor<RwStringVM>, StringOpsView>();
//Locator.CurrentMutable.Register<IViewFor<ScriptTaskWindowVM>, ScriptTaskWindow>();

Expand Down
3 changes: 3 additions & 0 deletions src/S7SvrSim/Services/DB/IS7DataBlockService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public interface IS7DataBlockService
float ReadReal(int dbNumber, int pos);
void WriteReal(int dbNumber, int pos, float real);

double ReadLReal(int dbNumber, int pos);
void WriteLReal(int dbNumber, int pos, double real);

string ReadString(int dbNumber, int offset);
void WriteString(int dbNumber, int offset, int maxlen, string str);
}
Expand Down
28 changes: 28 additions & 0 deletions src/S7SvrSim/Services/DB/S7DataBlockService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,34 @@ public float ReadReal(int dbNumber, int pos)

#endregion

#region LReal
public void WriteLReal(int dbNumber, int pos, double real)
{
var config = _runningVM.RunningsItems.Where(i => i.AreaKind == AreaKind.DB && i.BlockNumber == dbNumber).FirstOrDefault();
if (config == null)
{
this._mediator.Publish(new MessageNotification { Message = $"DBNumber={dbNumber} 不存在!" });
return;
}
var buffer = config.Bytes;
S7.SetLRealAt(buffer, pos, real);
}

public double ReadLReal(int dbNumber, int pos)
{
var config = _runningVM.RunningsItems.Where(i => i.AreaKind == AreaKind.DB && i.BlockNumber == dbNumber).FirstOrDefault();
if (config == null)
{
this._mediator.Publish(new MessageNotification { Message = $"DBNumber={dbNumber} 不存在!" });
return default;
}
var buffer = config.Bytes;
var real = S7.GetLRealAt(buffer, pos);
return real;
}

#endregion

#region ulong
public ulong ReadULong(int dbNumber, int pos)
{
Expand Down
3 changes: 3 additions & 0 deletions src/S7SvrSim/Services/MB/IS7MBService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public interface IS7MBService
float ReadReal(int pos);
void WriteReal(int pos, float real);

double ReadLReal(int pos);
void WriteLReal(int pos, double real);

string ReadString(int offset);
void WriteString(int offset, int maxlen, string str);
}
Expand Down
16 changes: 16 additions & 0 deletions src/S7SvrSim/Services/MB/S7MBService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ public float ReadReal(int pos)

#endregion

#region LReal
public void WriteLReal(int pos, double real)
{
var buffer = GetBuffer();
S7.SetLRealAt(buffer, pos, real);
}

public double ReadLReal(int pos)
{
var buffer = GetBuffer();
var real = S7.GetLRealAt(buffer, pos);
return real;
}

#endregion

#region ulong
public ulong ReadULong(int pos)
{
Expand Down
6 changes: 6 additions & 0 deletions src/S7SvrSim/UserControls/OperationsCtrl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@
HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"/>
</TabItem>

<TabItem Header="LReal">
<rxui:ViewModelViewHost Name="lRealOps"
MinHeight="150" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"/>
</TabItem>

<TabItem Header="String">
<rxui:ViewModelViewHost Name="stringOps"
MinHeight="150" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Expand Down
1 change: 1 addition & 0 deletions src/S7SvrSim/UserControls/OperationsCtrl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public OperationsCtrl()
this.OneWayBind(this.ViewModel, vm => vm.RwUInt32VM, v => v.uintOps.ViewModel).DisposeWith(d);
this.OneWayBind(this.ViewModel, vm => vm.RwUInt64VM, v => v.ulongOps.ViewModel).DisposeWith(d);
this.OneWayBind(this.ViewModel, vm => vm.RwRealVM, v => v.realOps.ViewModel).DisposeWith(d);
this.OneWayBind(this.ViewModel, vm => vm.RwLRealVM, v => v.lRealOps.ViewModel).DisposeWith(d);
this.OneWayBind(this.ViewModel, vm => vm.RwStringVM, v => v.stringOps.ViewModel).DisposeWith(d);
//this.OneWayBind(this.ViewModel, vm => vm.TaskViewModle, v => v.Task.ViewModel).DisposeWith(d);

Expand Down
40 changes: 40 additions & 0 deletions src/S7SvrSim/UserControls/Rws/LRealOpsView.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<UserControl x:Class="S7SvrSim.UserControls.Rws.LRealOpsView"
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:S7SvrSim.UserControls.Rws"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">


<Grid Margin="16, 2, 16, 2">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>

<Grid.Resources>
<Style TargetType="Button" BasedOn="{StaticResource MaterialDesignOutlinedButton}">
<Setter Property="VerticalAlignment" Value="Center"></Setter>
</Style>
<Style TargetType="TextBlock" BasedOn="{StaticResource MaterialDesignTextBlock}">
<Setter Property="VerticalAlignment" Value="Center"></Setter>
</Style>
<Style TargetType="TextBox" BasedOn="{StaticResource MaterialDesignTextBox}">
<Setter Property="VerticalAlignment" Value="Center"></Setter>
</Style>
</Grid.Resources>

<Button Name="btnRead" Content="读取" HorizontalAlignment="Left" Grid.Row="0" Grid.Column="0"></Button>
<TextBox Name="txtValueRead" HorizontalAlignment="Stretch" IsEnabled="False" Grid.Row="0" Grid.Column="1"></TextBox>

<TextBox Name="txtValueWritten" Grid.Row="1" Grid.Column="0"></TextBox>
<Button Name="btnWrite" Content="写入" HorizontalAlignment="Right" Grid.Row="1" Grid.Column="1"></Button>
</Grid>

</UserControl>
57 changes: 57 additions & 0 deletions src/S7SvrSim/UserControls/Rws/LRealOpsView.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using S7SvrSim.ViewModels.Rw;
using Splat;
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 S7SvrSim.UserControls.Rws
{
/// <summary>
/// Interaction logic for LRealOpsView.xaml
/// </summary>
public partial class LRealOpsView : UserControl, IViewFor<RwLRealVM>
{

public LRealOpsView()
{
InitializeComponent();
this.WhenActivated(d => {
this.Bind(this.ViewModel, vm => vm.ValueRead, v => v.txtValueRead.Text).DisposeWith(d);
this.Bind(this.ViewModel, vm => vm.ToBeWritten, v => v.txtValueWritten.Text).DisposeWith(d);
this.BindCommand(this.ViewModel, vm => vm.CmdRead, v => v.btnRead).DisposeWith(d);
this.BindCommand(this.ViewModel, vm => vm.CmdWrite, v => v.btnWrite).DisposeWith(d);

this.ViewModel.CmdRead.ThrownExceptions
.Subscribe(e => MessageBox.Show(e.Message));
this.ViewModel.CmdWrite.ThrownExceptions
.Subscribe(e => MessageBox.Show(e.Message));
});
}

#region
public RwLRealVM ViewModel
{
get { return (RwLRealVM)GetValue(ViewModelProperty); }
set { SetValue(ViewModelProperty, value); }
}

object IViewFor.ViewModel { get => this.ViewModel; set => this.ViewModel = (RwLRealVM)value; }

// Using a DependencyProperty as the backing store for ViewModel. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ViewModelProperty =
DependencyProperty.Register("ViewModel", typeof(RwLRealVM), typeof(LRealOpsView), new PropertyMetadata(null));
#endregion

}
}
1 change: 1 addition & 0 deletions src/S7SvrSim/ViewModels/Rws/OperationsVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public OperationVM(IS7DataBlockService s7ServerService, PyScriptRunner scriptRun
public ReactiveObject RwUInt32VM = Locator.Current.GetRequiredService<RwUInt32VM>();
public ReactiveObject RwUInt64VM = Locator.Current.GetRequiredService<RwUInt64VM>();
public ReactiveObject RwRealVM = Locator.Current.GetRequiredService<RwRealVM>();
public ReactiveObject RwLRealVM = Locator.Current.GetRequiredService<RwLRealVM>();
public ReactiveObject RwStringVM = Locator.Current.GetRequiredService<RwStringVM>();


Expand Down
24 changes: 24 additions & 0 deletions src/S7SvrSim/ViewModels/Rws/RwUnits/RwLRealVM.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using S7Svr.Simulator.ViewModels;

namespace S7SvrSim.ViewModels.Rw;

public class RwLRealVM : RwVMBase<double>
{
public RwLRealVM(IS7DataBlockService s7ServerService) : base(s7ServerService)
{
}


protected override void CmdWrite_Impl()
{
this._s7ServerService.WriteLReal(this.RwVM.TargetDBNumber, this.RwVM.TargetPos, this.ToBeWritten);
}

protected override double CmdRead_Impl()
{
var val = this._s7ServerService.ReadLReal(this.RwVM.TargetDBNumber, this.RwVM.TargetPos);
this.ValueRead = val;
return val;
}

}

0 comments on commit ec25a17

Please sign in to comment.