-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
229 lines (220 loc) · 9.02 KB
/
Program.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
222
223
224
225
226
227
228
229
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.IO;
using System.Text.RegularExpressions;
namespace Termodat
{
class Program
{
static void Main(string[] args)
{
var termodat = new Termodat("COM4", 2, 1, 1, 1);
ShowTitle();
InitProgram(termodat);
}
private static void ShowTitle()
{
Console.WriteLine("Termodat control pannel\n\n" +
"Please input command:\n\n" +
//"- \"set-start-program\" to set startup program and step\n" +
"- \"set-program\" to configure regulatory programs\n" +
"- \"temp\" to show current temperature\n" +
"- \"setpoint\" to show current set-point\n" +
"- \"channel\" to show amount of termodat channels\n" +
"- \"process\" to show current state process\n" +
"- \"program\" to show current state program\n" +
"- \"start\" to start process\n" +
"- \"stop\" to stop termodat\n" +
"- \"pause\" to pause termodat\n" +
"- \"clear\" to clear screen\n" +
"- \"q or quit\" to exit\n"); ;
}
static void InitProgram(Termodat termodat)
{
while (true && termodat.IsConnect)
{
Console.Write("$ ");
string input = Console.ReadLine();
switch (input)
{
case "set-program":
SetStartProgram(termodat);
break;
case "temp":
Show(termodat.Temp);
break;
case "setpoint":
Show(termodat.SetPoint);
break;
case "channel":
Show(termodat.Channels);
break;
case "process":
Show(termodat.StateProcess.ToString());
break;
case "program":
Show(termodat.StateProgram.ToString());
break;
case "start":
if (termodat.StateProcess != StateProcess.Run)
{
termodat.StartProcess();
Show("Start OK");
}
else
Show("Process already running");
break;
case "stop":
if (termodat.StateProcess != StateProcess.Stop)
{
termodat.StopProcess();
Show("Stop OK");
}
else
Show("Process already stopped");
break;
case "pause":
if (termodat.StateProcess != StateProcess.Pause)
{
termodat.PauseProcess();
Show("Pause OK");
}
else
Show("Process already paused");
break;
case "clear":
Console.Clear();
ShowTitle();
break;
case "q":
case "quit":
Show("Bye...");
return;
default:
Console.WriteLine("Incorrect input, try again");
break;
}
}
}
static void SetStartProgram(Termodat termodat)
{
bool cicle = true;
Console.WriteLine("Editable program number: ");
int programNumber = int.Parse(Console.ReadLine());
Console.WriteLine("Editable step number: ");
int stepNumber = int.Parse(Console.ReadLine());
Console.WriteLine("What doing in this step, \"HeatingOrCooling\", \"Exposure\", \"Goto\", \"Stop\"?: ");
string step = Console.ReadLine();
StateProgram state = StateProgram.HeatingOrCooling;
while (cicle)
{
switch (step)
{
case "HeatingOrCooling":
state = StateProgram.HeatingOrCooling;
cicle = false;
break;
case "Exposure":
state = StateProgram.Exposure;
cicle = false;
break;
case "Goto":
state = StateProgram.Goto;
cicle = false;
break;
case "Stop":
state = StateProgram.Stop;
cicle = false;
break;
default:
Console.WriteLine("Incorrect input, try again");
break;
}
}
cicle = true;
Console.WriteLine("Параметр 1 (время выдержки, либо скорость (0,1ºC/ч), либо номер программы (если Goto)) Integer (100 = 10,0): ");
int param1 = int.Parse(Console.ReadLine());
Console.WriteLine("Параметр 2 (целевая уставка в 0,1ºC)] Integer: ");
int param2 = int.Parse(Console.ReadLine());
Console.WriteLine("Условие перехода на следующий шаг \"Tcalc\", \"ManualAccept\", \"Tmeasure\"");
TransitionCondition condition = TransitionCondition.Tmeasure;
string cond = Console.ReadLine();
while (cicle)
{
switch (cond)
{
case "Tcalc":
condition = TransitionCondition.Tcalc;
cicle = false;
break;
case "ManualAccept":
condition = TransitionCondition.ManualAccept;
cicle = false;
break;
case "Tmeasure":
condition = TransitionCondition.Tmeasure;
cicle = false;
break;
default:
Console.WriteLine("Incorrect input, try again");
break;
}
}
termodat.SetupProgram(programNumber, stepNumber, state, param1, param2, condition);
}
static void Show(object obj)
{
Console.WriteLine($">> {obj}\n");
}
#region Methods for working with bytes
public static byte[] ReadHoldingRegister(int id, int startAddress, int length)
{
byte[] data = new byte[8];
byte High, Low;
data[0] = Convert.ToByte(id);
data[1] = Convert.ToByte(3);
byte[] _adr = BitConverter.GetBytes(startAddress - 1);
data[2] = _adr[1];
data[3] = _adr[0];
byte[] _length = BitConverter.GetBytes(length);
data[4] = _length[1];
data[5] = _length[0];
myCRC(data, 6, out High, out Low);
data[6] = Low;
data[7] = High;
return data;
}
public static void myCRC(byte[] message, int length, out byte CRCHigh, out byte CRCLow)
{
ushort CRCFull = 0xFFFF;
for (int i = 0; i < length; i++)
{
CRCFull = (ushort)(CRCFull ^ message[i]);
for (int j = 0; j < 8; j++)
{
if ((CRCFull & 0x0001) == 0)
CRCFull = (ushort)(CRCFull >> 1);
else
{
CRCFull = (ushort)((CRCFull >> 1) ^ 0xA001);
}
}
}
CRCHigh = (byte)((CRCFull >> 8) & 0xFF);
CRCLow = (byte)(CRCFull & 0xFF);
}
static byte[] ReadExact(Stream s, int nbytes)
{
var buf = new byte[nbytes];
var readpos = 0;
while (readpos < nbytes)
readpos += s.Read(buf, readpos, nbytes - readpos);
return buf;
}
#endregion
}
}