forked from cskilbeck/modbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
267 lines (219 loc) · 8.09 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Threading;
using Args;
//////////////////////////////////////////////////////////////////////
namespace KP184
{
class Actions: Args.Handler
{
//////////////////////////////////////////////////////////////////////
kp184 device = new kp184();
//////////////////////////////////////////////////////////////////////
[Help("Set the com port")]
void port(string com_port)
{
Log.Verbose($"COM Port: {com_port}");
device.port.PortName = com_port;
}
//////////////////////////////////////////////////////////////////////
[Help("Set the baud rate")]
void baud(int baud_rate)
{
Log.Verbose($"BAUD rate: {baud_rate}");
device.port.BaudRate = baud_rate;
}
//////////////////////////////////////////////////////////////////////
[Help("Set the device address")]
void address(byte device_address)
{
if(device_address == 0)
{
throw new Args.Error($"Device address can't be 0");
}
Log.Verbose($"Device address: {device_address}");
device.address = device_address;
}
//////////////////////////////////////////////////////////////////////
[Help("Switch the load on or off")]
void @switch(kp184.load_switch on_or_off)
{
Log.Verbose($"Turning load switch {on_or_off}");
device.set_load_switch(on_or_off);
}
//////////////////////////////////////////////////////////////////////
[Help("Switch the load on")]
void on()
{
Log.Verbose($"Turning load switch {kp184.load_switch.on}");
device.set_load_switch(kp184.load_switch.on);
}
//////////////////////////////////////////////////////////////////////
[Help("Switch the load off")]
void off()
{
Log.Verbose($"Turning load switch {kp184.load_switch.off}");
device.set_load_switch(kp184.load_switch.off);
}
//////////////////////////////////////////////////////////////////////
[Help("Do nothing for N milliseconds")]
void delay(int ms)
{
Log.Verbose($"Sleeping for {ms}ms");
Thread.Sleep(ms);
}
//////////////////////////////////////////////////////////////////////
[Help("Set the load mode")]
void mode(kp184.load_mode load_mode)
{
Log.Verbose($"Setting load mode to {load_mode}");
device.set_mode(load_mode);
}
//////////////////////////////////////////////////////////////////////
[Help("Enable (true) or disable (false) the checksum check")]
void checksum(bool enable)
{
Log.Info($"Checksum checking: {enable}");
device.checksum_check = enable;
}
//////////////////////////////////////////////////////////////////////
[Help("Do a ramp")]
void ramp(int from, int to, int step, int interval_ms)
{
Log.Info($"Ramp: from {from} to {to} in steps of {step} at intervals of {interval_ms}");
if(Math.Sign(to - from) != Math.Sign(step))
{
step = -step;
Log.Warning($"Changing step from {-step} to {step} so it works");
}
int current = from;
var stop_watch = new System.Diagnostics.Stopwatch();
var total_time = new System.Diagnostics.Stopwatch();
var step_time = TimeSpan.FromMilliseconds(interval_ms);
total_time.Start();
while((step < 0 && current >= to) || (step > 0 && current <= to))
{
stop_watch.Restart();
device.set_current((uint)current);
Log.Info($"{current,9}mA, elapsed: {total_time.Elapsed.ToString()}");
current += step;
stop_watch.Stop();
double ms_remaining = (step_time - stop_watch.Elapsed).TotalMilliseconds;
if(ms_remaining > 0)
{
Thread.Sleep((int)ms_remaining);
}
}
}
//////////////////////////////////////////////////////////////////////
[Help("Set the current in milliamps")]
void current(uint current)
{
Log.Verbose($"Set current to: {current}");
device.set_current(current);
}
//////////////////////////////////////////////////////////////////////
[Help("Set the watts in deci Watts (0.01W)")]
void power(uint watts)
{
Log.Verbose($"Set watts to: {watts}");
device.set_power(watts);
}
//////////////////////////////////////////////////////////////////////
[Help("Set the resistance in 0.1 Ohm")]
void resistance(uint ohms)
{
Log.Verbose($"Set ohms to: {ohms}");
device.set_resistance(ohms);
}
//////////////////////////////////////////////////////////////////////
[Help("Returns the status of KP184")]
void get_status()
{
Log.Verbose($"Calling get_status from KP184");
device.get_status();
}
//////////////////////////////////////////////////////////////////////
[Help("Returns if device is On (=1) or Off (=0)")]
void get_switch()
{
Log.Verbose($"Calling get_OnOff from KP184");
int OnOff = device.get_load_switch();
Console.WriteLine(OnOff);
}
//////////////////////////////////////////////////////////////////////
[Help("Returns the mode (cv=0, cc=1, cr=2, cw=3) of the device")]
void get_mode()
{
Log.Verbose($"Calling get_Mode from KP184");
int Mode = device.get_mode();
Log.Info($"Mode is {Mode} ({kp184.load_mode_as_string(Mode)})");
}
//////////////////////////////////////////////////////////////////////
[Help("Reads the current in mA")]
void get_current()
{
Log.Verbose($"Calling get_Mode from KP184");
int current = device.get_current();
Log.Info($"Current is {current}mA");
}
//////////////////////////////////////////////////////////////////////
[Help("Reads the voltage in mV")]
void get_voltage()
{
Log.Verbose($"Calling get_Mode from KP184");
int volts = device.get_voltage();
Log.Info($"Voltage is {volts}mV");
}
//////////////////////////////////////////////////////////////////////
[Help("Set verbose mode")]
void verbose()
{
Log.level = Log.Level.Verbose;
}
//////////////////////////////////////////////////////////////////////
[Help("Set the log level")]
void loglevel(Log.Level level)
{
Log.level = level;
}
//////////////////////////////////////////////////////////////////////
[Default]
[Help("Show this help text")]
public void help()
{
show_help("KP184 Controller", "Control the KP184, specify port, baud, address before other commands");
throw new Args.SilentError();
}
}
//////////////////////////////////////////////////////////////////////
class Program
{
static void Main(string[] args)
{
Actions a = new Actions();
try
{
if(!a.execute(args))
{
a.help();
}
}
catch(Args.SilentError e)
{
}
catch(Args.FatalError e)
{
Log.Error($"{e.Message}");
}
catch(Exception e)
{
Log.Error($"{e.Message}");
}
#if DEBUG
Console.ReadLine();
#endif
}
}
}