-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCADA Online.aspx.cs
195 lines (186 loc) · 8 KB
/
SCADA Online.aspx.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using S7.Net;
using S7.Net.Types;
using MET_Station.App_Code;
using System.Configuration;
namespace MET_Station
{
public partial class SCADA_Online : System.Web.UI.Page
{
SQL _sql;
protected void Page_Load(object sender, EventArgs e)
{
if (Convert.ToInt32(Session["UserRole"]) != 1)
{
Response.Redirect("/All.aspx");
}
Plc _Plc = new Plc(CpuType.S71200, tbConnect.Text, 0, 0);
if (_Plc.Open() == ErrorCode.NoError)
{ //Xu li du lieu kieu real nhan tu PLC sang C# thanh dau cham dong(float)bang phan tach 4 byte bo nho roi ep kieu
byte[] temp = new byte[4];
string temp1 = _Plc.Read("MB10").ToString();
temp[3] = byte.Parse(temp1);
string temp2 = _Plc.Read("MB11").ToString();
temp[2] = byte.Parse(temp2);
string temp3 = _Plc.Read("MB12").ToString();
temp[1] = byte.Parse(temp3);
string temp4 = _Plc.Read("MB13").ToString();
temp[0] = byte.Parse(temp4);
float t = BitConverter.ToSingle(temp, 0);
tbNhietdo.Text = t.ToString("f");//Xong phan nhiet do
// vi minh muon xem tung byte du lieu nhan ve nhu the nao va thu tu cua chung, doan code nay co the duoc thay bang cau lenh sau
//double value = ((uint)plc.Read("vungnho")).ConvertToDouble();
byte[] humid = new byte[4];
string humid1 = _Plc.Read("MB14").ToString();
humid[3] = byte.Parse(humid1);
string humid2 = _Plc.Read("MB15").ToString();
humid[2] = byte.Parse(humid2);
string humid3 = _Plc.Read("MB16").ToString();
humid[1] = byte.Parse(humid3);
string humid4 = _Plc.Read("MB17").ToString();
humid[0] = byte.Parse(humid4);
float h = BitConverter.ToSingle(humid, 0);
tbDoam.Text = h.ToString();//Xong phan do am
byte[] speed = new byte[4];
string speed1 = _Plc.Read("MB18").ToString();
speed[3] = byte.Parse(speed1);
string speed2 = _Plc.Read("MB19").ToString();
speed[2] = byte.Parse(speed2);
string speed3 = _Plc.Read("MB20").ToString();
speed[1] = byte.Parse(speed3);
string speed4 = _Plc.Read("MB21").ToString();
speed[0] = byte.Parse(speed4);
float s = BitConverter.ToSingle(speed, 0);
tbTocdogio.Text = s.ToString();//Xong phan toc do gio
byte[] rain = new byte[4];
string rain1 = _Plc.Read("MB22").ToString();
rain[3] = byte.Parse(rain1);
string rain2 = _Plc.Read("MB23").ToString();
rain[2] = byte.Parse(rain2);
string rain3 = _Plc.Read("MB24").ToString();
rain[1] = byte.Parse(rain3);
string rain4 = _Plc.Read("MB25").ToString();
rain[0] = byte.Parse(rain4);
float r = BitConverter.ToSingle(rain, 0);
tbLuongmua.Text = r.ToString();//Xong phan luong mua
_Plc.Close();
}
else
{
tbStatus.Text = "Kết nối thất bại";
}
}
protected void bt_Connect_Click(object sender, EventArgs e)
{
int time = int.Parse(tbCycle.Text);// Ep kieu sang int
Timer1.Interval = time;
Timer1.Enabled = true;
Plc _Plc = new Plc(CpuType.S71200, tbConnect.Text, 0, 0);
if (_Plc.Open() == ErrorCode.NoError)
{
tbStatus.Text = "Kết nối thành công";
}
else
{
tbStatus.Text = "Kết nối thất bại";
}
}
protected void Bt_Start_Click(object sender, EventArgs e)
{
Plc _Plc = new Plc(CpuType.S71200,tbConnect.Text, 0, 0);
if (_Plc.Open() == ErrorCode.NoError)
{
_Plc.Write("M2.0", 1);
_Plc.Close();
}
else
{
tbStatus.Text = "Kết nối thất bại";
}
}
protected void Bt_Stop_Click(object sender, EventArgs e)
{
Plc _Plc = new Plc(CpuType.S71200, tbConnect.Text, 0, 0);
if (_Plc.Open() == ErrorCode.NoError)
{
_Plc.Write("M2.1", 1);
_Plc.Write("M2.1", 0);
_Plc.Close();
}
else
{
tbStatus.Text = "Kết nối thất bại";
}
}
protected void Bt_Reset_Click(object sender, EventArgs e)
{
Plc _Plc = new Plc(CpuType.S71200, tbConnect.Text, 0, 0);
if (_Plc.Open() == ErrorCode.NoError)
{
_Plc.Write("M2.2", 1);
_Plc.Write("M2.2", 0);
_Plc.Close();
}
else
{
tbStatus.Text = "Kết nối thất bại";
}
}
protected void Chart1_Load(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
_sql = new SQL(connectionString);
DataTable cb = _sql.GetSensor("CB 001");// Sử dụng hàm GetSensor ở SQL.cs
Chart1.ChartAreas["ChartArea1"].AxisX.Title = "Thời gian";//Đặt tên trục hoành là thời gian
Chart1.ChartAreas["ChartArea1"].AxisY.Title = "Nhiệt độ (Độ C)";//Đặt tên trục tung là nhiệt độ
for (int i = 0; i < cb.Rows.Count; i++)
{
Chart1.Series["Nhietdo"].Points.AddXY(cb.Rows[i]["DateTime"], cb.Rows[i]["Value"]);//Vẽ biểu đồ đơn giản,sau sẽ vẽ nhiều option hơn
}
}
protected void Chart2_Load(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
_sql = new SQL(connectionString);
DataTable cb = _sql.GetSensor("CB 002");
Chart2.ChartAreas["ChartArea1"].AxisX.Title = "Thời gian";
Chart2.ChartAreas["ChartArea1"].AxisY.Title = "Độ ẩm (%)";
for (int i = 0; i < cb.Rows.Count; i++)
{
Chart2.Series["Doam"].Points.AddXY(cb.Rows[i]["DateTime"], cb.Rows[i]["Value"]);
}
}
protected void Chart3_Load(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
_sql = new SQL(connectionString);
DataTable cb = _sql.GetSensor("CB 003");
Chart3.ChartAreas["ChartArea1"].AxisX.Title = "Thời gian";
Chart3.ChartAreas["ChartArea1"].AxisY.Title = "Tốc độ gió (m/s)";
for (int i = 0; i < cb.Rows.Count; i++)
{
Chart3.Series["Tocdogio"].Points.AddXY(cb.Rows[i]["DateTime"], cb.Rows[i]["Value"]);
}
}
protected void Chart4_Load(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
_sql = new SQL(connectionString);
DataTable cb = _sql.GetSensor("CB 004");
Chart4.ChartAreas["ChartArea1"].AxisX.Title = "Thời gian";
Chart4.ChartAreas["ChartArea1"].AxisY.Title = "Lượng mưa (mm)";
for (int i = 0; i < cb.Rows.Count; i++)
{
Chart4.Series["Luongmua"].Points.AddXY(cb.Rows[i]["Datetime"], cb.Rows[i]["Value"]);
}
}
}
}