Commit ecf66e6c authored by 潘栩锋's avatar 潘栩锋 🚴

添加 测厚仪服务器,提供modbus Tcp 服务,供其它HMI读取

parent 9392ac86
......@@ -88,6 +88,7 @@
<Compile Include="Server\Model\OrgDbModel.cs" />
<Compile Include="Server\ScanWarning.cs" />
<Compile Include="Server\TDGage.cs" />
<Compile Include="Server\ThickPlcModbusTcpServer.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="FodyWeavers.xml">
......@@ -111,6 +112,10 @@
<Project>{5ee61ac6-5269-4f0f-b8fa-4334fe4a678f}</Project>
<Name>Misc</Name>
</ProjectReference>
<ProjectReference Include="..\..\thick_public\Project.FLY.ModbusMapper\FLY.ModbusMapper\FLY.ModbusMapper.csproj">
<Project>{6d4b9bda-2a66-4583-b244-758bc4213d9f}</Project>
<Name>FLY.ModbusMapper</Name>
</ProjectReference>
<ProjectReference Include="..\..\thick_public\Project.FLY.OBJComponents\OBJComponents\FLY.OBJComponents.csproj">
<Project>{a539505d-9ac0-426b-a9a0-197df50598b0}</Project>
<Name>FLY.OBJComponents</Name>
......
using FLY.Thick.Blowing.IService;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Misc;
using System.IO;
using Newtonsoft.Json;
namespace FLY.Thick.Blowing.Server
{
public class ThickPlcModbusTcpServer:INotifyPropertyChanged
{
const int ADDR_ID = 0;
const int ADDR_DateTimeYear = 1;
const int ADDR_DateTimeMonth = 2;
const int ADDR_DateTimeDay = 3;
const int ADDR_DateTimeHour = 4;
const int ADDR_DateTimeMinute = 5;
const int ADDR_DateTimeSecond = 6;
const int ADDR_Average = 7;
const int ADDR_Sigma2 = 8;
const int ADDR_Sigma2Percent = 9;
const int ADDR_Count = 10;
const int ADDR_Datas = 11;
const int DatasCap = 199 - 11 + 1;
FLY.Modbus.WithThread.ServerTCP server;
IBulkDbService bulkDbService;
public event PropertyChangedEventHandler PropertyChanged;
public string ServerAddr { get; set; } = "0.0.0.0:1502";
public FLY.Modbus.WithThread.ServerTCP ModbusTcpServer => server;
UInt16[] regs = new UInt16[200];
UInt16[] regs2 = new UInt16[200];
public ThickPlcModbusTcpServer()
{
if (!Load())
{
Save();
}
}
public void Init(IBulkDbService bulkDbService)
{
this.bulkDbService = bulkDbService;
server = new FLY.Modbus.WithThread.ServerTCP(Misc.StringConverter.ToIPEndPoint(ServerAddr),
GetValue, SetValue);
server.Start();
this.bulkDbService.PropertyChanged += BulkDbService_PropertyChanged;
updateDatas();
}
private void BulkDbService_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(bulkDbService.LastScanDataId))
{
updateDatas();
}
}
void updateDatas()
{
this.bulkDbService.GetFrame(new Pack_GetFrameRequest() { Id = 0, Mix = 1 },
(object asyncContext, object retData) =>
{
var reponse = retData as Pack_GetFrameReponse;
if (reponse.scanData == null) {
//没有数据
return;
}
var scanData = reponse.scanData;
regs2[ADDR_ID] = (UInt16)scanData.ID;
regs2[ADDR_DateTimeYear] = (UInt16)scanData.Time.Year;
regs2[ADDR_DateTimeMonth] = (UInt16)scanData.Time.Month;
regs2[ADDR_DateTimeDay] = (UInt16)scanData.Time.Day;
regs2[ADDR_DateTimeHour] = (UInt16)scanData.Time.Hour;
regs2[ADDR_DateTimeMinute] = (UInt16)scanData.Time.Minute;
regs2[ADDR_DateTimeSecond] = (UInt16)scanData.Time.Second;
if (scanData.Thicks != null)
{
double avg = scanData.Thicks.AverageNoNull();
if (!double.IsNaN(avg))
{
regs2[ADDR_Average] = (UInt16)(avg * 10);
double sigma2 = scanData.Thicks.Sigma() * 2;
regs2[ADDR_Sigma2] = (UInt16)(sigma2 * 10);
double sigma2Percent = sigma2 / avg * 100;
regs2[ADDR_Sigma2Percent] = (UInt16)(sigma2Percent * 10);
regs2[ADDR_Count] = (UInt16)scanData.Thicks.Count();
for (int i = 0; i < scanData.Thicks.Count() && i < DatasCap; i++)
{
var d = scanData.Thicks[i];
if (!double.IsNaN(avg))
{
regs2[ADDR_Datas + i] = (UInt16)(d * 10);
}
else
{
regs2[ADDR_Datas + i] = 0;
}
}
}
else
{
clearDatas(regs2);
}
}
else
{
clearDatas(regs2);
}
lock (regs)
{
Array.Copy(regs2, regs, regs.Count());
}
},
null);
}
void clearDatas(UInt16[] regs2)
{
for (int i = ADDR_Average; i < regs2.Count(); i++)
{
regs2[i] = 0;
}
}
void GetValue(int addr, object values)
{
if (addr < 0)
return;
if (values is UInt16[]) {
var uint16s = values as UInt16[];
int sourceIndex = addr;
int len = uint16s.Count();
if (sourceIndex >= regs.Count())
return;
if(sourceIndex +len > regs.Count())
{
len = regs.Count() - sourceIndex;
}
lock (regs)
{
Array.Copy(regs, addr, uint16s, 0, len);
}
}
}
void SetValue(int addr, object values)
{
}
string file_path = "thickplc.json";
bool Load()
{
try
{
if (File.Exists(file_path))
{
string json = File.ReadAllText(file_path);
var jsonDb = JsonConvert.DeserializeObject<ThickPlcModbusTcpServerJsonDb>(json);
this.ServerAddr = jsonDb.ServerAddr;
return true;
}
}
catch
{
//异常,没有json 解码失败
}
return false;
}
public bool Save()
{
try
{
var jsonDb = new ThickPlcModbusTcpServerJsonDb() { ServerAddr = ServerAddr };
string json = JsonConvert.SerializeObject(jsonDb, Formatting.Indented);
File.WriteAllText(file_path, json);
return true;
}
catch
{
//异常,没有json 编码失败
}
return false;
}
}
public class ThickPlcModbusTcpServerJsonDb
{
public string ServerAddr = "0.0.0.0:1502";
}
}
......@@ -165,6 +165,10 @@
<Project>{5ee61ac6-5269-4f0f-b8fa-4334fe4a678f}</Project>
<Name>Misc</Name>
</ProjectReference>
<ProjectReference Include="..\..\thick_public\Project.FLY.ModbusMapper\FLY.ModbusMapper\FLY.ModbusMapper.csproj">
<Project>{6d4b9bda-2a66-4583-b244-758bc4213d9f}</Project>
<Name>FLY.ModbusMapper</Name>
</ProjectReference>
<ProjectReference Include="..\..\thick_public\Project.FLY.Thick.Base\FLY.Thick.Base\FLY.Thick.Base.csproj">
<Project>{91a1aafe-a528-4c2e-a48a-9259e780255a}</Project>
<Name>FLY.Thick.Base</Name>
......
......@@ -10,7 +10,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Navigation;
using ThickPlcModbusTcpServer = FLY.Thick.Blowing.Server.ThickPlcModbusTcpServer;
namespace FLY.Thick.BlowingScan.UI.Server
{
/// <summary>
......@@ -21,6 +21,7 @@ namespace FLY.Thick.BlowingScan.UI.Server
FLY.AppHelper.WindowNotifyIconHelper notifyiconhelper;
TDGage mGage;
OBJProxys mOBJProxy;
ThickPlcModbusTcpServer thickPlcModbusTcpServer;
public MainWindow()
{
InitializeComponent();
......@@ -40,12 +41,15 @@ namespace FLY.Thick.BlowingScan.UI.Server
mOBJProxy = new OBJProxys(0, mGage);
thickPlcModbusTcpServer = new ThickPlcModbusTcpServer();
thickPlcModbusTcpServer.Init(mGage.bulkDb);
FObjBase.PollModule.Current.Start();
this.Hide();
PgMain p = new PgMain();
p.Init(mGage);
p.Init(mGage,thickPlcModbusTcpServer);
this.Navigate(p);
this.NavigationService.RemoveBackEntry();
......
......@@ -58,6 +58,34 @@
<TextBlock Style="{StaticResource Styles.Text.FieldContent}" Text="{Binding Position}"/>
</StackPanel>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="{StaticResource ControlMargin}" DataContext="{Binding ModbusTcpServer}">
<StackPanel Margin="{StaticResource ControlMargin}">
<TextBlock Style="{StaticResource Styles.Text.FieldHeader}" Text="ModbusTcp服务地址"/>
<TextBlock Style="{StaticResource Styles.Text.FieldContent}" Text="{Binding LocalEP}"/>
</StackPanel>
<StackPanel Margin="{StaticResource ControlMargin}">
<TextBlock Style="{StaticResource Styles.Text.FieldHeader}" Text="状态"/>
<TextBlock>
<TextBlock.Style>
<Style TargetType="TextBlock" BasedOn="{StaticResource Styles.Text.FieldContent}">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Text" Value="停止"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsRunning}" Value="True">
<Setter Property="Foreground" Value="Green"/>
<Setter Property="Text" Value="工作中"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</StackPanel>
<StackPanel Margin="{StaticResource ControlMargin}">
<TextBlock Style="{StaticResource Styles.Text.FieldHeader}" Text="当前连接数"/>
<TextBlock Style="{StaticResource Styles.Text.FieldContent}" Text="{Binding ConnectedCnt}"/>
</StackPanel>
</StackPanel>
</StackPanel>
</Grid>
</Page>
......@@ -13,6 +13,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;
using ThickPlcModbusTcpServer = FLY.Thick.Blowing.Server.ThickPlcModbusTcpServer;
namespace FLY.Thick.BlowingScan.UI.Server
{
......@@ -26,10 +27,10 @@ namespace FLY.Thick.BlowingScan.UI.Server
{
InitializeComponent();
}
public void Init(TDGage mGage)
public void Init(TDGage mGage, ThickPlcModbusTcpServer thickPlcModbusTcpServer)
{
viewModel = new PgMainVm();
viewModel.Init(mGage);
viewModel.Init(mGage, thickPlcModbusTcpServer);
this.DataContext = viewModel;
}
......@@ -46,7 +47,10 @@ namespace FLY.Thick.BlowingScan.UI.Server
public RelayCommand SaveDebugCmd { get; private set; }
public RelayCommand DebugCmd { get; private set; }
public FLY.Modbus.WithThread.ServerTCP ModbusTcpServer{get;set;}
TDGage mGage;
ThickPlcModbusTcpServer thickPlcModbusTcpServer;
public PgMainVm()
{
SetupCmd = new RelayCommand(Setup);
......@@ -56,11 +60,13 @@ namespace FLY.Thick.BlowingScan.UI.Server
public void Init(TDGage mGage)
public void Init(TDGage mGage, ThickPlcModbusTcpServer thickPlcModbusTcpServer)
{
this.mGage = mGage;
this.thickPlcModbusTcpServer = thickPlcModbusTcpServer;
SysParam = mGage.sysParam;
FlyAd = mGage.flyAd;
ModbusTcpServer = thickPlcModbusTcpServer.ModbusTcpServer;
}
private void OpenWdDebug()
......@@ -74,7 +80,7 @@ namespace FLY.Thick.BlowingScan.UI.Server
//打开设置页面
WdSetup w = new WdSetup();
w.Init(SysParam, FlyAd);
w.Init(SysParam, FlyAd, thickPlcModbusTcpServer);
w.Owner = App.Current.MainWindow;
w.ShowDialog();
}
......
......@@ -24,6 +24,10 @@
<TextBlock Style="{StaticResource Styles.Text.FieldHeader}" Text="AD盒地址"/>
<TextBox Style="{StaticResource Styles.Text.FieldContent.Input}" Text="{Binding FlyAdAddr}"/>
</StackPanel>
<StackPanel Margin="{StaticResource ControlMargin}">
<TextBlock Style="{StaticResource Styles.Text.FieldHeader}" Text="ModbusTcp地址"/>
<TextBox Style="{StaticResource Styles.Text.FieldContent.Input}" Text="{Binding PlcAddr}"/>
</StackPanel>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="{StaticResource ControlMargin}">
<Button Style="{StaticResource Styles.Button.Square.Accent2}"
......
using FLY.Thick.Base.Server;
using FLY.Thick.Blowing.Server;
using FlyADBase;
using GalaSoft.MvvmLight.Command;
using System;
......@@ -28,10 +29,10 @@ namespace FLY.Thick.BlowingScan.UI.Server
{
InitializeComponent();
}
public void Init(SysParam sysParam,FlyAD7 flyAd)
public void Init(SysParam sysParam,FlyAD7 flyAd, ThickPlcModbusTcpServer thickPlcModbusTcpServer)
{
viewModel = new WdSetupVm();
viewModel.Init(sysParam, flyAd);
viewModel.Init(sysParam, flyAd, thickPlcModbusTcpServer);
this.DataContext = viewModel;
}
}
......@@ -40,13 +41,13 @@ namespace FLY.Thick.BlowingScan.UI.Server
public event PropertyChangedEventHandler PropertyChanged;
public string Addr { get; set; }
public string FlyAdAddr { get; set; }
public string PlcAddr { get; set; }
public RelayCommand OkCmd { get; private set; }
SysParam sysParam;
FlyAD7 flyAd;
InitParam initParam;
ThickPlcModbusTcpServer thickPlcModbusTcpServer;
public WdSetupVm()
{
OkCmd = new RelayCommand(Ok);
......@@ -54,12 +55,15 @@ namespace FLY.Thick.BlowingScan.UI.Server
public void Init(SysParam sysParam, FlyAD7 flyAd)
public void Init(SysParam sysParam, FlyAD7 flyAd, ThickPlcModbusTcpServer thickPlcModbusTcpServer)
{
this.sysParam = sysParam;
this.flyAd = flyAd;
this.thickPlcModbusTcpServer = thickPlcModbusTcpServer;
Addr = sysParam.Addr;
FlyAdAddr = flyAd.Addr;
PlcAddr = thickPlcModbusTcpServer.ServerAddr;
}
private void Ok()
......@@ -75,6 +79,9 @@ namespace FLY.Thick.BlowingScan.UI.Server
flyAd.Addr = FlyAdAddr;
flyAd.Save();
thickPlcModbusTcpServer.ServerAddr = PlcAddr;
thickPlcModbusTcpServer.Save();
System.Environment.CurrentDirectory = "../";
AppHelper.AppJustOne.Restart();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment