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

1. 添加基于线程的 ModbusClientTCP

parent c21045c8
...@@ -68,6 +68,7 @@ ...@@ -68,6 +68,7 @@
<Compile Include="OBJ_INTERFACE\HEATCELL_OBJ_INTERFACE.cs" /> <Compile Include="OBJ_INTERFACE\HEATCELL_OBJ_INTERFACE.cs" />
<Compile Include="OBJ_INTERFACE\OBJ_INTERFACE_ID.cs" /> <Compile Include="OBJ_INTERFACE\OBJ_INTERFACE_ID.cs" />
<Compile Include="OBJ_INTERFACE\SNAPSHOT_OBJ_INTERFACE.cs" /> <Compile Include="OBJ_INTERFACE\SNAPSHOT_OBJ_INTERFACE.cs" />
<Compile Include="PLC.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Server.OBJProxy\HeatBuf_OBJProxy.cs" /> <Compile Include="Server.OBJProxy\HeatBuf_OBJProxy.cs" />
<Compile Include="Server.OBJProxy\HeatCell_OBJProxy.cs" /> <Compile Include="Server.OBJProxy\HeatCell_OBJProxy.cs" />
...@@ -95,6 +96,10 @@ ...@@ -95,6 +96,10 @@
<Project>{5ee61ac6-5269-4f0f-b8fa-4334fe4a678f}</Project> <Project>{5ee61ac6-5269-4f0f-b8fa-4334fe4a678f}</Project>
<Name>Misc</Name> <Name>Misc</Name>
</ProjectReference> </ProjectReference>
<ProjectReference Include="..\..\Project.FLY.ModbusMapper\FLY.ModbusMapper\FLY.ModbusMapper.csproj">
<Project>{6d4b9bda-2a66-4583-b244-758bc4213d9f}</Project>
<Name>FLY.ModbusMapper</Name>
</ProjectReference>
<ProjectReference Include="..\..\Project.FLY.ModbusModule\FLY.ModbusModule\FLY.ModbusModule.csproj"> <ProjectReference Include="..\..\Project.FLY.ModbusModule\FLY.ModbusModule\FLY.ModbusModule.csproj">
<Project>{8e19c40f-ce7f-4982-bd90-4eb4e9e04e34}</Project> <Project>{8e19c40f-ce7f-4982-bd90-4eb4e9e04e34}</Project>
<Name>FLY.ModbusModule</Name> <Name>FLY.ModbusModule</Name>
......
...@@ -22,10 +22,8 @@ namespace FLY.FeedbackRenZiJia.Server ...@@ -22,10 +22,8 @@ namespace FLY.FeedbackRenZiJia.Server
/// <summary> /// <summary>
/// 设置加热量 /// 设置加热量
/// </summary> /// </summary>
/// <param name="idx"></param> /// <param name="values"></param>
/// <param name="value"></param> void SetHeat(IEnumerable<UInt16> values);
void SetHeat(int idx, UInt16 value);
#endregion #endregion
...@@ -45,14 +43,12 @@ namespace FLY.FeedbackRenZiJia.Server ...@@ -45,14 +43,12 @@ namespace FLY.FeedbackRenZiJia.Server
/// <summary> /// <summary>
/// 获取当前值 /// 获取当前值
/// </summary> /// </summary>
/// <param name="idx"></param>
/// <returns></returns> /// <returns></returns>
UInt16 GetCurr(int idx); IEnumerable<UInt16> GetCurr();
#endregion #endregion
#region 状态 #region 状态
int Errno { get; set; } int Errno { get; set; }
int RetCode { get; set; }
#endregion #endregion
} }
......
using FLY.FeedbackRenZiJia.Server;
using FLY.Modbus;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
namespace FLY.FeedbackRenZiJia
{
public class PLC:IPLCLink
{
#region 输出
/// <summary>
/// 加热通道数
/// </summary>
public UInt16 ChannelCnt { get; set; }
/// <summary>
/// 加热量更新
/// </summary>
public UInt16 HeatUpdate { get; set; }
/// <summary>
/// 设置加热量
/// </summary>
/// <param name="values"></param>
public void SetHeat(IEnumerable<UInt16> values)
{
}
#endregion
#region 当前值
/// <summary>
/// 当前电流 有没?
/// </summary>
public bool HasElectricity { get; private set; }
/// <summary>
/// 风机是否启动?
/// </summary>
public bool HasFan { get; private set; }
/// <summary>
/// 当前值更新
/// </summary>
public UInt16 CurrUpdate { get; private set; }
/// <summary>
/// 获取当前值
/// </summary>
/// <returns></returns>
public IEnumerable<UInt16> GetCurr()
{
return null;
}
#endregion
#region 状态
public int Errno { get; set; }
#endregion
public event PropertyChangedEventHandler PropertyChanged;
ClientTCP client;
List<Action> actions = new List<Action>();
class DRMap
{
public string PropertyName { get; set; }
public int MBAddr { get; set; }
}
public PLC( ClientTCP client )
{
//创建定时计划读取写入数据
FObjBase.PollModule.Current.Poll_Config(FObjBase.PollModule.POLL_CONFIG.ADD, OnPoll, TimeSpan.FromMilliseconds(10));
actions.Add(() =>
{
client.Do_01(3, 2, (bool[] val, object obj) =>
{
HasFan = val[0];
HasElectricity = val[1];
NextAction();
}, null);
});
//actions.Add(() =>
//{
// client.Do_03(202, 1, (UInt16[] val, object obj) =>
// {
// HasFan = val[0];
// HasElectricity = val[1];
// NextAction();
// }, null);
//});
this.PropertyChanged += PLC_PropertyChanged;
}
private void PLC_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "ChannelCnt")
{
}
else if (e.PropertyName == "HeatUpdate")
{
}
}
void NextAction()
{
}
void OnPoll()
{
}
class RegWrite
{
public PLCAddressArea dataArea;
public int addr;
public object val;
public RegWrite(PLCAddressArea dataArea, int addr, object val)
{
this.dataArea = dataArea;
this.addr = addr;
this.val = val;
}
}
//List<RegWrite>
}
}
...@@ -392,12 +392,13 @@ namespace FLY.FeedbackRenZiJia.Server ...@@ -392,12 +392,13 @@ namespace FLY.FeedbackRenZiJia.Server
{ {
CurrsUpdate = hmi.CurrUpdate; CurrsUpdate = hmi.CurrUpdate;
bool isupdate = false; bool isupdate = false;
IEnumerable<UInt16> list = hmi.GetCurr();
//更新了 //更新了
for (int i = 0; i < ChannelCnt; i++) for (int i = 0; i < ChannelCnt && i< list.Count(); i++)
{ {
if (Currs[i] != hmi.GetCurr(i)) if (Currs[i] != list.ElementAt(i))
{ {
Currs[i] = hmi.GetCurr(i); Currs[i] = list.ElementAt(i);
isupdate = true; isupdate = true;
} }
} }
...@@ -644,16 +645,18 @@ namespace FLY.FeedbackRenZiJia.Server ...@@ -644,16 +645,18 @@ namespace FLY.FeedbackRenZiJia.Server
void Output(int[] heats) void Output(int[] heats)
{ {
List<UInt16> list = new List<UInt16>();
for (int i = 0; i < ChannelCnt; i++) for (int i = 0; i < ChannelCnt; i++)
{ {
int heat = heats[i]; UInt16 heat = (UInt16)heats[i];
if (heat < 0) if (heat < 0)
heat = 0; heat = 0;
else if (heat > 100) else if (heat > 100)
heat = 100; heat = 100;
list.Add(heat);
hmi.SetHeat(i, (UInt16)heat);
} }
hmi.SetHeat(list);
hmi.HeatUpdate++; hmi.HeatUpdate++;
if (hmi.HeatUpdate == 0) if (hmi.HeatUpdate == 0)
hmi.HeatUpdate = 1; hmi.HeatUpdate = 1;
......
...@@ -178,13 +178,21 @@ namespace FLY.FeedbackRenZiJia.Server ...@@ -178,13 +178,21 @@ namespace FLY.FeedbackRenZiJia.Server
} }
} }
public void SetHeat(int idx, UInt16 value) public void SetHeat(IEnumerable<UInt16> value)
{ {
mPLCRegs.SetUInt16(ADDR_D_Heats+idx, value); for (int i = 0; i < value.Count(); i++)
{
mPLCRegs.SetUInt16(ADDR_D_Heats + i, value.ElementAt(i));
}
} }
public UInt16 GetCurr(int idx) public IEnumerable<UInt16> GetCurr()
{ {
return mPLCRegs.GetUInt16(ADDR_D_Currs + idx); List<UInt16> list = new List<UInt16>();
for (int i = 0; i < ChannelCnt; i++)
{
list.Add(mPLCRegs.GetUInt16(ADDR_D_Currs + i));
}
return list;
} }
#endregion #endregion
......
...@@ -55,6 +55,8 @@ ...@@ -55,6 +55,8 @@
<Compile Include="Pack_Proto.cs" /> <Compile Include="Pack_Proto.cs" />
<Compile Include="PLCGroup.cs" /> <Compile Include="PLCGroup.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WithThread\ClientTCP.cs" />
<Compile Include="WithThread\IModbusClient.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\Project.FLY.FObjSys\FObjSys\FObjBase.csproj"> <ProjectReference Include="..\..\Project.FLY.FObjSys\FObjSys\FObjBase.csproj">
......
...@@ -40,21 +40,12 @@ namespace FLY.Modbus ...@@ -40,21 +40,12 @@ namespace FLY.Modbus
/// </summary> /// </summary>
public int UpdateInterval = 200; public int UpdateInterval = 200;
TimeSpan actUpdateInterval;
/// <summary> /// <summary>
/// 实际更新间隔 /// 实际更新间隔
/// </summary> /// </summary>
public TimeSpan ActUpdateInterval { get; protected set; } public TimeSpan ActUpdateInterval { get; protected set; }
//{
// get { return actUpdateInterval; }
// protected set {
// if (actUpdateInterval != value)
// {
// actUpdateInterval = value;
// NotifyPropertyChanged("ActUpdateInterval");
// }
// }
//}
private Stopwatch mStopwatch = new Stopwatch(); private Stopwatch mStopwatch = new Stopwatch();
class RegWrite class RegWrite
{ {
...@@ -216,7 +207,6 @@ namespace FLY.Modbus ...@@ -216,7 +207,6 @@ namespace FLY.Modbus
{ {
isInCommunication = false; isInCommunication = false;
NextPlan(); NextPlan();
} }
/// <summary> /// <summary>
/// 正在通讯中 /// 正在通讯中
......
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace FLY.Modbus.WithThread
{
/// <summary>
/// 基于线程
/// </summary>
public class ClientTCP:INotifyPropertyChanged,IModbusClient
{
/// <summary>
/// 通信超时, 默认是 1s
/// </summary>
public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(1);
/// <summary>
/// socket 错误信息,带时间
/// </summary>
public string ErrMsg { get; set; }
/// <summary>
/// 连接成功
/// </summary>
public bool IsConnected { get; set; }
/// <summary>
/// 标识,modbus tcp的参数
/// </summary>
public byte UnitID = 1;
UInt16 tranid = 12;
UInt16 GetFreeTranID()
{
tranid++;
return tranid;
}
/// <summary>
/// 远端地址
/// </summary>
public IPEndPoint RemoteEP { get; set; }
Socket sock;
public event PropertyChangedEventHandler PropertyChanged;
public ClientTCP(IPEndPoint ep)
{
RemoteEP = ep;
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Connect(true);
}
bool isconnecting = false;
void Connect(bool isImmediately)
{
if (isconnecting)
return;
isconnecting = true;
Task task = new Task((state) =>
{
bool isImme = (bool)state;
if (!isImme)//不需要立刻连接,等待3秒后重试
Thread.Sleep(3000);
while (!IsConnected)
{
lock (this)
{
try
{
sock.Connect(RemoteEP);
IsConnected = true;
}
catch(Exception e)
{
ErrMsg = e.Message;
}
}
if (!IsConnected)
Thread.Sleep(3000);
}
isconnecting = false;
}, isImmediately);
task.Start();
}
bool TranExecute(Pack_Proto request, out Pack_Proto reponse)
{
reponse = new Pack_Proto();
lock (this)
{
if (!IsConnected)
goto _end;
try
{
sock.Send(request.ToBytes());
}
catch (Exception e)
{
ErrMsg = e.Message;
IsConnected = false;
goto _end;
}
//读取数据
while (true)
{
byte[] recvBytes = new byte[2048];//回复的指令不可能大于2K
sock.ReceiveTimeout = (int)Timeout.TotalMilliseconds;
int offset = 0;
try
{
int size = recvBytes.Length - offset;
if (size <= 0)
{
//异常
ErrMsg = "TimeOut";
IsConnected = false;
goto _end;
}
int bytes = sock.Receive(recvBytes, offset, recvBytes.Length, 0);//从客户端接受信息
//可能会分了多次接收
if (!reponse.TryParse(recvBytes, 0, out int rlen))
{
//处理失败,数据不够,继续接收
offset = bytes;
}
else
{
break;
}
}
catch (SocketException e)
{
//异常
ErrMsg = e.Message;
IsConnected = false;
goto _end;
}
}
}
_end:
if (!IsConnected)
{
Connect(false);
return false;
}
else
{
return true;
}
}
/// <summary>
/// 读多个 COIL
/// </summary>
/// <param name="addr"></param>
/// <param name="cnt"></param>
/// <param name="values"></param>
/// <returns></returns>
public bool Do_01(ushort addr, ushort cnt, out IEnumerable<bool> values)
{
values = null;
List<byte> data = new List<byte>();
data.AddRange(addr.GetBytes_Big_endian());
data.AddRange(cnt.GetBytes_Big_endian());
Pack_Proto request = new Pack_Proto()
{
tranid = GetFreeTranID(),
unitid = UnitID,
func = 0x01,
buf = data.ToArray()
};
if (!TranExecute(request, out Pack_Proto reponse))
return false;
if (reponse.func != 0x01)
{
//有问题!!!触发异常事件
ErrMsg = "返回信息 reponse.func != 0x01";
return false;
}
List<bool> blist = new List<bool>();
int byte_len = reponse.buf[0];
int index = 1;
for (int i = 0; i < byte_len; i++)
{
for (int j = 0; j < 8; j++)
{
if (Misc.MyBase.CHECKBIT(reponse.buf[index], j))
blist.Add(true);
else
blist.Add(false);
if (blist.Count() >= cnt)
{
//完成
i = byte_len;
break;
}
}
index++;
}
values = blist;
return true;
}
/// <summary>
/// 读多个REG
/// </summary>
/// <param name="addr"></param>
/// <param name="cnt"></param>
/// <param name="values"></param>
/// <returns></returns>
public bool Do_03(ushort addr, ushort cnt, out IEnumerable<ushort> values)
{
values = null;
List<byte> data = new List<byte>();
data.AddRange(addr.GetBytes_Big_endian());
data.AddRange(cnt.GetBytes_Big_endian());
Pack_Proto request = new Pack_Proto()
{
tranid = GetFreeTranID(),
unitid = UnitID,
func = 0x03,
buf = data.ToArray()
};
if (!TranExecute(request, out Pack_Proto reponse))
return false;
if (reponse.func != 0x03)
{
//有问题!!!触发异常事件
ErrMsg = "返回信息 reponse.func != 0x03";
return false;
}
List<UInt16> blist = new List<UInt16>();
int index = 1;
while (index < reponse.buf.Count())
{
blist.Add(COMMON.ToUInt16_Big_endian(reponse.buf, index));
index += 2;
}
values = blist;
return true;
}
/// <summary>
/// Write Single Coil
/// </summary>
/// <param name="addr"></param>
/// <param name="dat"></param>
/// <returns></returns>
public bool Do_05(ushort addr, bool dat)
{
List<byte> data = new List<byte>();
data.AddRange(addr.GetBytes_Big_endian());
if (dat)
data.Add(0xff);
else
data.Add(0);
data.Add(0);
Pack_Proto request = new Pack_Proto()
{
tranid = GetFreeTranID(),
unitid = UnitID,
func = 0x05,
buf = data.ToArray()
};
if (!TranExecute(request, out Pack_Proto reponse))
return false;
if (reponse.func != 0x05)
{
//有问题!!!触发异常事件
ErrMsg = "返回信息 reponse.func != 0x05";
return false;
}
return true;
}
/// <summary>
/// 写多个coil
/// </summary>
/// <param name="addr"></param>
/// <param name="datas"></param>
/// <returns></returns>
public bool Do_0F(ushort addr, IEnumerable<bool> datas)
{
List<byte> data = new List<byte>();
data.AddRange(addr.GetBytes_Big_endian());
data.AddRange(((UInt16)datas.Count()).GetBytes_Big_endian());
data.Add((byte)(Math.Ceiling(datas.Count() / 8.0)));
byte b = 0;
int j = 0;
for (int i = 0; i < datas.Count(); i++)
{
if (datas.ElementAt(i))
Misc.MyBase.SIGNBIT(ref b, j);
j++;
if (j == 8)
{
data.Add(b);
b = 0;
j = 0;
}
}
if (j != 0)
data.Add(b);
Pack_Proto request = new Pack_Proto()
{
tranid = GetFreeTranID(),
unitid = UnitID,
func = 0x0F,
buf = data.ToArray()
};
if (!TranExecute(request, out Pack_Proto reponse))
return false;
if (reponse.func != 0x0F)
{
//有问题!!!触发异常事件
ErrMsg = "返回信息 reponse.func != 0x0F";
return false;
}
return true;
}
public bool Do_10(ushort addr, IEnumerable<ushort> datas)
{
List<byte> data = new List<byte>();
data.AddRange(addr.GetBytes_Big_endian());
data.AddRange(((UInt16)datas.Count()).GetBytes_Big_endian());
data.Add((byte)(datas.Count() * 2));
for (int i = 0; i < datas.Count(); i++)
{
data.AddRange(datas.ElementAt(i).GetBytes_Big_endian());
}
Pack_Proto request = new Pack_Proto()
{
tranid = GetFreeTranID(),
unitid = UnitID,
func = 0x10,
buf = data.ToArray()
};
if (!TranExecute(request, out Pack_Proto reponse))
return false;
if (reponse.func != 0x10)
{
//有问题!!!触发异常事件
ErrMsg = "返回信息 reponse.func != 0x10";
return false;
}
return true;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FLY.Modbus.WithThread
{
/// <summary>
/// 同一时刻只能有一个通信,当同时执行 Do_xx, 这些指令会被hold住
/// 如果连接断开,或者异常,这些 Do_xx,全部返回 false
/// </summary>
public interface IModbusClient
{
/// <summary>
/// 通信超时
/// </summary>
TimeSpan Timeout { get; set; }
/// <summary>
/// 读多个 COIL
/// </summary>
/// <param name="addr"></param>
/// <param name="cnt"></param>
/// <param name="values"></param>
/// <returns></returns>
bool Do_01(UInt16 addr, UInt16 cnt, out IEnumerable<bool> values);
/// <summary>
/// 读多个Holding REGs
/// </summary>
/// <param name="addr"></param>
/// <param name="cnt"></param>
/// <param name="values"></param>
/// <returns></returns>
bool Do_03(UInt16 addr, UInt16 cnt, out IEnumerable<UInt16> values);
/// <summary>
/// Write Single Coil
/// </summary>
/// <param name="addr"></param>
/// <param name="data"></param>
/// <returns></returns>
bool Do_05(UInt16 addr, bool data);
/// <summary>
/// 写多个coil
/// </summary>
/// <param name="addr"></param>
/// <param name="datas"></param>
/// <returns></returns>
bool Do_0F(UInt16 addr, IEnumerable<bool> datas);
/// <summary>
/// 写多个REG
/// </summary>
/// <param name="addr"></param>
/// <param name="datas"></param>
/// <returns></returns>
bool Do_10(UInt16 addr, IEnumerable<UInt16> datas);
}
}
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1" xmlns:local="clr-namespace:WpfApplication1"
StartupUri="MainWindow.xaml"> StartupUri="Window1.xaml">
<Application.Resources> <Application.Resources>
</Application.Resources> </Application.Resources>
......
<?xml version="1.0" encoding="utf-8" ?>
<Weavers>
<PropertyChanged2 />
</Weavers>
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="Window1" Height="450" Width="800">
<Grid>
<StackPanel Orientation="Vertical" Margin="10" >
<StackPanel.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="20"/>
</Style>
</StackPanel.Resources>
<StackPanel Orientation="Horizontal">
<TextBlock >
<Run Text="Elapsed="/>
<Run Text="{Binding Elapsed}"/>
</TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock >
<Run Text="风机="/>
<Run Text="{Binding IsFanOn}"/>
</TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock >
<Run Text="电流="/>
<Run Text="{Binding IsPowerOn}"/>
</TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock >
<Run Text="D800=["/>
<Run Text="{Binding D800[0],StringFormat={}{0:D3}}"/>
<Run Text="."/>
<Run Text="{Binding D800[1],StringFormat={}{0:D3}}"/>
<Run Text="]"/>
</TextBlock>
</StackPanel>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Top" Width="75"/>
</StackPanel>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
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.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
PLC pLC;
public Window1()
{
InitializeComponent();
pLC = new PLC();
pLC.Init();
pLC.Do();
this.DataContext = pLC;
}
}
public class PLC:INotifyPropertyChanged
{
public bool IsFanOn { get; set; }
public bool IsPowerOn { get; set; }
public TimeSpan Elapsed { get; set; }
public ObservableCollection<UInt16> D800 { get; } = new ObservableCollection<ushort>();
FLY.Modbus.WithThread.ClientTCP client;
public PLC()
{
}
public void Init()
{
client = new FLY.Modbus.WithThread.ClientTCP(new System.Net.IPEndPoint(System.Net.IPAddress.Parse("192.168.1.1"), 502));
}
public void Do()
{
Task task = new Task(() =>
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
while (true)
{
if (client.Do_01(3, 2, out IEnumerable<bool> values))
{
IsFanOn = values.ElementAt(0);
IsPowerOn = values.ElementAt(1);
}
IEnumerable<UInt16> values2;
int read_cnt = 30;
int offset = 0;
int cnt = 100;
while(offset<cnt)
{
if (offset + read_cnt > cnt)
{
read_cnt = 100 - offset;
}
if (client.Do_03((UInt16)(800 + offset), (UInt16)(read_cnt), out values2))
{
for (int i = 0; i < read_cnt; i++)
{
if (i + offset < D800.Count)
D800[i + offset] = values2.ElementAt(i);
else
D800.Add(values2.ElementAt(i));
}
offset += read_cnt;
}
else
{
break;
}
}
Elapsed = stopwatch.Elapsed;
stopwatch.Restart();
Thread.Sleep(0);
}
});
task.Start();
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\PropertyChanged2.Fody.2.5.13\build\PropertyChanged2.Fody.props" Condition="Exists('..\packages\PropertyChanged2.Fody.2.5.13\build\PropertyChanged2.Fody.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
...@@ -15,6 +16,8 @@ ...@@ -15,6 +16,8 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFrameworkProfile /> <TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget> <PlatformTarget>AnyCPU</PlatformTarget>
...@@ -39,6 +42,9 @@ ...@@ -39,6 +42,9 @@
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> <Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.1\lib\net40\Newtonsoft.Json.dll</HintPath> <HintPath>..\packages\Newtonsoft.Json.12.0.1\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference> </Reference>
<Reference Include="PropertyChanged2, Version=2.5.13.0, Culture=neutral, PublicKeyToken=ee3ee20bcf148ddd, processorArchitecture=MSIL">
<HintPath>..\packages\PropertyChanged2.Fody.2.5.13\lib\net40\PropertyChanged2.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Drawing" /> <Reference Include="System.Drawing" />
...@@ -65,6 +71,9 @@ ...@@ -65,6 +71,9 @@
<Compile Include="Page_Main.xaml.cs"> <Compile Include="Page_Main.xaml.cs">
<DependentUpon>Page_Main.xaml</DependentUpon> <DependentUpon>Page_Main.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="Window1.xaml.cs">
<DependentUpon>Window1.xaml</DependentUpon>
</Compile>
<Page Include="MainWindow.xaml"> <Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType> <SubType>Designer</SubType>
...@@ -81,6 +90,10 @@ ...@@ -81,6 +90,10 @@
<SubType>Designer</SubType> <SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
</Page> </Page>
<Page Include="Window1.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs"> <Compile Include="Properties\AssemblyInfo.cs">
...@@ -152,7 +165,20 @@ ...@@ -152,7 +165,20 @@
<Name>FLY.Winder.UI.Client</Name> <Name>FLY.Winder.UI.Client</Name>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Resource Include="FodyWeavers.xml">
<SubType>Designer</SubType>
</Resource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\Fody.3.2.13\build\Fody.targets" Condition="Exists('..\packages\Fody.3.2.13\build\Fody.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Fody.3.2.13\build\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Fody.3.2.13\build\Fody.targets'))" />
<Error Condition="!Exists('..\packages\PropertyChanged2.Fody.2.5.13\build\PropertyChanged2.Fody.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\PropertyChanged2.Fody.2.5.13\build\PropertyChanged2.Fody.props'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild"> <Target Name="BeforeBuild">
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="Fody" version="3.2.13" targetFramework="net40" developmentDependency="true" />
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="net40" /> <package id="Newtonsoft.Json" version="12.0.1" targetFramework="net40" />
<package id="PropertyChanged2.Fody" version="2.5.13" targetFramework="net40" />
</packages> </packages>
\ No newline at end of file
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