-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplc_haussterung.cs
221 lines (206 loc) · 7.56 KB
/
plc_haussterung.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Timers;
using Unitronics.ComDriver.Messages.DataRequest;
namespace Unitronics.ComDriver
{
public class plc_haussterung
{
private Channel ch;
private PLC plc;
public static ReadWriteRequest[] rw;
private IEnumerable _enumerable;
public plc_haussterung(Channel ch)
{
init();
this.ch = ch;
plc = PLCFactory.GetPLC(ch, 0);
plc.SetExecuter(OperandsExecuterType.ExecuterPartialBinaryMix);
rw = init();
}
//Staticly type for needed Application
public ReadWriteRequest[] init()
{
//Hydr Vorlauf+Rück % Kom Vor+Rück --> Spliten
ReadWriteRequest r3 = new ReadOperands
{
NumberOfOperands = 4,
OperandType = OperandTypes.MI,
StartAddress = 1631,
};
//Hydr. --> Spliten
ReadWriteRequest r4 = new ReadOperands
{
NumberOfOperands = 2,
OperandType = OperandTypes.MI,
StartAddress = 29,
};
//Aussentemp
ReadWriteRequest r5 = new ReadOperands
{
NumberOfOperands = 1,
OperandType = OperandTypes.MI,
StartAddress = 1523,
};
//Fehler
ReadWriteRequest r6 = new ReadOperands
{
NumberOfOperands = 1,
OperandType = OperandTypes.MB,
StartAddress = 2300,
};
//Daten Kühlung aktiv
ReadWriteRequest r7 = new ReadOperands
{
NumberOfOperands = 1,
OperandType = OperandTypes.SB,
StartAddress = 3,
};
//Luft MO
ReadWriteRequest r8 = new ReadOperands
{
NumberOfOperands = 1,
OperandType = OperandTypes.MB,
StartAddress = 1807,
};
//Luft SP1
ReadWriteRequest r9 = new ReadOperands
{
NumberOfOperands = 1,
OperandType = OperandTypes.MB,
StartAddress = 1805,
};
//Luft SC
ReadWriteRequest r10 = new ReadOperands
{
NumberOfOperands = 1,
OperandType = OperandTypes.MB,
StartAddress = 1801,
};
//Luft Versorgung
ReadWriteRequest r11 = new ReadOperands
{
NumberOfOperands = 1,
OperandType = OperandTypes.MB,
StartAddress = 1811,
};
//Luft SP2
ReadWriteRequest r12 = new ReadOperands
{
NumberOfOperands = 1,
OperandType = OperandTypes.MB,
StartAddress = 1803,
};
//Luft FB
ReadWriteRequest r13 = new ReadOperands
{
NumberOfOperands = 1,
OperandType = OperandTypes.MB,
StartAddress = 1809,
};
//Temp Klebeplatz
ReadWriteRequest r14 = new ReadOperands
{
NumberOfOperands = 1,
OperandType = OperandTypes.MI,
StartAddress = 1561,
};
return new ReadWriteRequest[]
{
r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14
};
}
//Uses RWQuest and reads data Prints it or Puts it into DB
public void readData()
{
try
{
plc.ReadWrite(ref rw);
}
catch (Exception e)
{
Console.WriteLine(e.Message + "\t" + e.StackTrace);
throw;
}
int j = 0; // J is counter to specifiy decimal values conversion
IEnumerable ar = null;
double[] dar = new double[18];
DateTime RTC = plc.RTC;
Console.Write(RTC + "\t");
for (int i = 0; i < rw.Length; i++)
{
ar = rw[i].ResponseValues as IEnumerable;
foreach (var VARIABLE in ar)
{
double d = Convert.ToDouble(VARIABLE);
switch (j)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 15:
d /= 10;
d = Math.Round(d, 3);
break;
}
if (j < dar.Length)
{
dar[j] = d;
}
j++;
}
}
Console.Write(String.Join("\t",dar.Select(dd=>dd.ToString()).ToArray()));
String s = (@"Data Source=192.168.50.9;Initial Catalog=UnitronicsPLC;User ID=Unitronics;Password=Uni2020#");
SqlConnection conn;
SqlDataReader dataReader;
conn = new SqlConnection(s);
try
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
String query =
//"INSERT INTO T2020_07_24_Kühlanlage (date) VALUES ('24.07.2020')";
"INSERT INTO Table_2 (date,timestamp,hydrVor,hydrRück,hydrDruck,WerkzVor,WerkzRück,WerkzDruck,Aussentemp,Fehler,KuehlungAktiv,LuftM0,LufSP1,LuftSC,LuftVersorgung,LuftSP2,TempKlebeplatz) VALUES ('"+
DateTime.Today.ToShortDateString()+"','"+
RTC.ToLongTimeString()+"',"+
dar[0].ToString("F",CultureInfo.InvariantCulture)+","+
dar[1].ToString("F",CultureInfo.InvariantCulture)+","+
dar[2].ToString("F",CultureInfo.InvariantCulture)+","+
dar[3].ToString("F",CultureInfo.InvariantCulture)+","+
dar[4].ToString("F",CultureInfo.InvariantCulture)+","+
dar[5].ToString("F",CultureInfo.InvariantCulture)+","+
dar[6].ToString("F",CultureInfo.InvariantCulture)+","+
dar[7].ToString("F",CultureInfo.InvariantCulture)+","+
dar[8].ToString("F",CultureInfo.InvariantCulture)+","+
dar[9].ToString("F",CultureInfo.InvariantCulture)+","+
dar[10].ToString("F",CultureInfo.InvariantCulture)+","+
dar[11].ToString("F",CultureInfo.InvariantCulture)+","+
dar[12].ToString("F",CultureInfo.InvariantCulture)+","+
dar[13].ToString("F",CultureInfo.InvariantCulture)+","+
dar[15].ToString("F",CultureInfo.InvariantCulture)+");";
//Console.WriteLine(query);
SqlCommand command = new SqlCommand(query, conn);
dataReader = command.ExecuteReader();
Console.WriteLine("\t\t SQL-Query succesful");
}
catch (Exception e)
{
//Console.WriteLine("\tError occured");
Console.WriteLine("Error occured:\t"+e.Message);
}
}
}
}