2021-09-04 WPF上位机通用框架平台实战-全局监控对象
【摘要】
在程序启动的时候就实时监控设备
public partial class App : Application
{
protected override void OnStartup(Startu...
在程序启动的时候就实时监控设备
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
GlobalMonitor.Start();
if (new LoginWindow().ShowDialog() == true)
{
new MainWindow().ShowDialog();
}
Application.Current.Shutdown();
}
protected override void OnExit(ExitEventArgs e)
{
GlobalMonitor.Stop();
base.OnExit(e);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
public class GlobalMonitor
{
public static ObservableCollection<DeviceModel> DeviceList { get; set; } = new ObservableCollection<DeviceModel>();
static bool isRunning = true;
static Task mainTask = null;
public static void Start()
{
mainTask = Task.Run(async () =>
{
// 获取设备信息
//DeviceList.Add(new DeviceModel { Name = "#1 Master device info" });
//DeviceList.Add(new DeviceModel { Name = "#2 Master device info" });
//DeviceList.Add(new DeviceModel { Name = "#3 Master device info" });
//DeviceList.Add(new DeviceModel { Name = "#4 Master device info" });
DeviceService deviceService = new DeviceService();
var list = deviceService.GetDevices();
if (list != null)
foreach (var item in list)
{
DeviceList.Add(item);
}
//DeviceList = new ObservableCollection<DeviceModel>(list);
while (isRunning)
{
await Task.Delay(100);
foreach (var item in DeviceList)
{
if (item.CommType == 2 && item.S7 != null)// S7通信
{
Zhaoxi.Communication.Siemens.S7Net s7Net = new Communication.Siemens.S7Net(item.S7.IP, item.S7.Port, (byte)item.S7.Rock, (byte)item.S7.Slot);
List<string> addrList = item.MonitorValueList.Select(v => v.Address).ToList();
var result = s7Net.Read<ushort>(addrList);
if (result.IsSuccessed)
{
for (int i = 0; i < item.MonitorValueList.Count; i++)
{
item.MonitorValueList[i].Value = result.Datas[i];
}
}
s7Net.Close();
}
}
}
});
}
public static void Stop()
{
isRunning = false;
mainTask.ConfigureAwait(true);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。
原文链接:codeboy.blog.csdn.net/article/details/120093498
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)