-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormMenu.cs
158 lines (133 loc) · 4.91 KB
/
FormMenu.cs
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
using S7.Net;
using SymbolFactoryDotNet;
using test.MainForms;
using test.Database;
namespace test
{
public partial class FormMenu : Form
{
protected override void OnLoad(EventArgs e)
{
this.WindowState = FormWindowState.Normal;
base.OnLoad(e);
}
public static bool s_isAdmin = false;
public static bool s_powerState = false, s_autmanState = false;
public static ValveSwitch s_switchCount = new ValveSwitch();
private Plc _myPlc;
private readonly string _ip = "169.254.116.178";
public Plc MyPlc { get => _myPlc; private set => _myPlc = value; }
public PLCComm MyComm { get; set; }
private readonly string _powerAddressDB = "DB2.DBX0.0";
private readonly string _autmanAddressDB = "DB2.DBX0.1";
private readonly string _TankGaoLimitAddressDB = "DB2.DBW2";
public FormMenu()
{
InitializeComponent();
s_switchCount.PropertyChanged += S_switchCount_PropertyChanged;
}
private void FormMenu_Load(object sender, EventArgs e)
{
MyPlc = new Plc(CpuType.S71200, _ip, 0, 0);
MyComm = new PLCComm();
MyComm.SwitchCountUp += MyComm_SwitchCountUp;
MyPlc.Open();
// Get AUTMAN and POWER current state
Power_Light.DiscreteValue1 = (bool)MyPlc.Read(_powerAddressDB);
s_powerState = Power_Light.DiscreteValue1;
AutMan_Light.DiscreteValue1 = (bool)MyPlc.Read(_autmanAddressDB);
s_autmanState = AutMan_Light.DiscreteValue1;
if (MyPlc.IsConnected)
{
timerComm.Start();
timerComm.Tick += new System.EventHandler(this.timerComm_Tick);
}
GetSwitchCount();
}
private void MyComm_SwitchCountUp(object sender, EventArgs e)
{
PropertyInfo prop = s_switchCount.GetType().GetProperty(sender.ToString());
if (prop != null)
{
UInt16 currentCount = (UInt16)prop.GetValue(s_switchCount);
prop.SetValue(s_switchCount, (UInt16)(currentCount + 1));
}
}
public event EventHandler DangXuat;
private void logoutButton_Click(object sender, EventArgs e)
{
DangXuat?.Invoke(this, EventArgs.Empty);
}
private void quitButton_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void buttonCIP_Click(object sender, EventArgs e)
{
FormCIP cip = new FormCIP();
cip.ShowDialog();
}
private void buttonGao_Click(object sender, EventArgs e)
{
Noi_gao gao = new Noi_gao(MyComm);
gao.ThayDoi += HMI_ThayDoi;
Noi_gao.s_maxSwitchCount = (ushort)MyPlc.Read(_TankGaoLimitAddressDB);
Noi_gao.s_switchCountLimit = (int)Math.Floor(0.9 * Noi_gao.s_maxSwitchCount);
gao.gioiHan_ThayDoi += GhiGioiHanMoi;
gao.ShowDialog();
}
private void GetSwitchCount()
{
MyPlc.ReadClass(s_switchCount, 3);
}
private void timer1_Tick(object sender, EventArgs e)
{
dateLabel.Text = DateTime.Now.ToString("dddd, dd MMMM yyyy");
timeLabel.Text = DateTime.Now.ToString("hh:mm tt");
}
private void timerComm_Tick(object sender, EventArgs e)
{
MyPlc.ReadClass(MyComm, 1);
}
public void HMI_ThayDoi(object sender, EventArgs e)
{
timerComm.Stop();
MyPlc.WriteClass(MyComm, 1);
timerComm.Start();
}
public void GhiGioiHanMoi(object sender, EventArgs e)
{
timerComm.Stop();
MyPlc.Write(_TankGaoLimitAddressDB, Noi_gao.s_maxSwitchCount);
timerComm.Start();
}
private void Power_Light_Click(object sender, EventArgs e)
{
Power_Light.DiscreteValue1 = !(Power_Light.DiscreteValue1);
s_powerState = Power_Light.DiscreteValue1;
MyPlc.Write(_powerAddressDB, Power_Light.DiscreteValue1);
}
private void AutMan_Light_Click(object sender, EventArgs e)
{
AutMan_Light.DiscreteValue1 = !(AutMan_Light.DiscreteValue1);
s_autmanState = AutMan_Light.DiscreteValue1;
MyPlc.Write(_autmanAddressDB, AutMan_Light.DiscreteValue1);
}
private void S_switchCount_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
timerComm.Stop();
MyPlc.WriteClass(s_switchCount, 3);
timerComm.Start();
}
}
}