米拉德尔
会员
- 已加入
- 2020年8月16日
- 留言内容
- 8
- 编程经验
- Beginner
亲爱的大家,
在我的项目中,我需要连接到S7 PLC,并以每100ms的周期读取数据,然后更新变量并将其放入数据库中。基于UWP的应用程序,我需要知道的是如何在所有代码背后使用PLC中的所有更新值,
我的问题最好的方法是什么?
这是代码的简单部分:
在代码中,我需要访问项目中所有页面中的value1并将其存储在SQL数据库中,而且有必要在应用程序在后台运行时保持连接处于活动状态。
提前致谢
在我的项目中,我需要连接到S7 PLC,并以每100ms的周期读取数据,然后更新变量并将其放入数据库中。基于UWP的应用程序,我需要知道的是如何在所有代码背后使用PLC中的所有更新值,
我的问题最好的方法是什么?
这是代码的简单部分:
code in page1:
Plc plc = new Plc(CpuType.S7300, "192.168.0.10", 0, 2); // connection to PLC
DispatcherTimer dispatcherTimer; // Timer to work on cycle
public void DispatcherTimerSetup()
{
dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += dispatcherTimer_Tick;
dispatcherTimer.Interval = new TimeSpan(1000000);
dispatcherTimer.Start();
}
void dispatcherTimer_Tick(object sender, object e)
{
updateDb5();
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
plc.Open(); // start connection to PLC it could be starts with page load or a button
}
bool value1; // the value which is necessary to access in all pages
private void updateDb5()
{
var db5 = new Db5(); // DB5 in PLC
plc.ReadClass(db5, 5);
value1 = db5.oneSecBlink; // An example bool in PLC DB
}
在代码中,我需要访问项目中所有页面中的value1并将其存储在SQL数据库中,而且有必要在应用程序在后台运行时保持连接处于活动状态。
提前致谢