質問編集履歴
1
コードをさらに詳細に書き出しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -69,3 +69,375 @@
|
|
69
69
|
* C#
|
70
70
|
|
71
71
|
=> ver.7.0
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
# 追記
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
```csharp
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
using System;
|
84
|
+
|
85
|
+
using System.Net;
|
86
|
+
|
87
|
+
using System.Threading;
|
88
|
+
|
89
|
+
using Framework.TCP;
|
90
|
+
|
91
|
+
using Common.Model;
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
namespace TcpIp.Model
|
96
|
+
|
97
|
+
{
|
98
|
+
|
99
|
+
public static class Tcp
|
100
|
+
|
101
|
+
{
|
102
|
+
|
103
|
+
private static MasterTCP _masterTcp;
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
private static IPAddress _ipAddress;
|
108
|
+
|
109
|
+
private static int _port;
|
110
|
+
|
111
|
+
private static ushort _address;
|
112
|
+
|
113
|
+
private static byte _unitId;
|
114
|
+
|
115
|
+
private static bool _updateFlag;
|
116
|
+
|
117
|
+
private static int _sleepTime;
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
public static void InitSettings(string ipAddr, int port, byte unitId, ushort address)
|
122
|
+
|
123
|
+
{
|
124
|
+
|
125
|
+
_ipAddress = IPAddress.Parse(ipAddr);
|
126
|
+
|
127
|
+
_port = port;
|
128
|
+
|
129
|
+
_address = address;
|
130
|
+
|
131
|
+
_unitId = unitId;
|
132
|
+
|
133
|
+
_updateFlag = true;
|
134
|
+
|
135
|
+
_sleepTime = 2500;
|
136
|
+
|
137
|
+
_masterTcp = new MasterTCP(_ipAddress.ToString(), _port);
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
public static void Connect()
|
144
|
+
|
145
|
+
{
|
146
|
+
|
147
|
+
if (_ipAddress == null && _port == 0)
|
148
|
+
|
149
|
+
throw new Exception("接続情報が初期化されていません。");
|
150
|
+
|
151
|
+
if (_masterTcp.Connected)
|
152
|
+
|
153
|
+
return;
|
154
|
+
|
155
|
+
_masterTcp.Connect();
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
public static void DisConnect()
|
162
|
+
|
163
|
+
{
|
164
|
+
|
165
|
+
if (!_masterTcp.Connected)
|
166
|
+
|
167
|
+
return;
|
168
|
+
|
169
|
+
_masterTcp.Disconnect();
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
public static void TryOpen()
|
176
|
+
|
177
|
+
{
|
178
|
+
|
179
|
+
try
|
180
|
+
|
181
|
+
{
|
182
|
+
|
183
|
+
_masterTcp.Connect();
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
catch (Exception ex)
|
188
|
+
|
189
|
+
{
|
190
|
+
|
191
|
+
throw new DeviceIoFailedException("");
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
private static void Execute(Command commandClass, TcpFunction function)
|
198
|
+
|
199
|
+
{
|
200
|
+
|
201
|
+
_updateFlag = !_updateFlag;
|
202
|
+
|
203
|
+
var command = new byte[2];
|
204
|
+
|
205
|
+
switch (function)
|
206
|
+
|
207
|
+
{
|
208
|
+
|
209
|
+
case TcpFunction.Read:
|
210
|
+
|
211
|
+
command = commandClass.CreateReadCommand();
|
212
|
+
|
213
|
+
break;
|
214
|
+
|
215
|
+
case TcpFunction.Write:
|
216
|
+
|
217
|
+
command = commandClass.CreateWriteCommand();
|
218
|
+
|
219
|
+
break;
|
220
|
+
|
221
|
+
default:
|
222
|
+
|
223
|
+
throw new Exception("[Tcp.Execute] 無効な操作です。");
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
var value = (ushort) ((_updateFlag ? 1 : 0 >> 15) + (command[0] >> 8) + command[1]);
|
228
|
+
|
229
|
+
_masterTcp.WriteSingleRegister(_unitId, (ushort)(1056 + _address), value);
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
Thread.Sleep(_sleepTime);
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
public static void Read(Command command)
|
240
|
+
|
241
|
+
{
|
242
|
+
|
243
|
+
Execute(command, TcpFunction.Read);
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
public static void Write(Command command)
|
250
|
+
|
251
|
+
{
|
252
|
+
|
253
|
+
Execute(command, TcpFunction.Write);
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
public enum TcpFunction
|
262
|
+
|
263
|
+
{
|
264
|
+
|
265
|
+
Read,
|
266
|
+
|
267
|
+
Write
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
}
|
272
|
+
|
273
|
+
```
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
こちらが実際のTcpクラスになります。
|
278
|
+
|
279
|
+
残念ながら、完全に同一ではなく、多少名前空間などを変えてあります。
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
```csharp
|
284
|
+
|
285
|
+
using System;
|
286
|
+
|
287
|
+
using System.Collections.ObjectModel;
|
288
|
+
|
289
|
+
using System.Windows.Media;
|
290
|
+
|
291
|
+
using Common.Model;
|
292
|
+
|
293
|
+
using TcpIp.Model;
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
namespace TcpIp.Ui.ViewModels
|
298
|
+
|
299
|
+
{
|
300
|
+
|
301
|
+
public class ItemViewModel<T> : ViewModel where T : Command , new()
|
302
|
+
|
303
|
+
{
|
304
|
+
|
305
|
+
protected Brush signal;
|
306
|
+
|
307
|
+
public string ItemName { get; protected set; }
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
public MainViewModel.Response Write()
|
312
|
+
|
313
|
+
{
|
314
|
+
|
315
|
+
var isWritten = false;
|
316
|
+
|
317
|
+
var isRead = false;
|
318
|
+
|
319
|
+
try
|
320
|
+
|
321
|
+
{
|
322
|
+
|
323
|
+
Tcp.Read(new T());
|
324
|
+
|
325
|
+
DoEvents();
|
326
|
+
|
327
|
+
isWritten = true;
|
328
|
+
|
329
|
+
}
|
330
|
+
|
331
|
+
catch (Exception)
|
332
|
+
|
333
|
+
{
|
334
|
+
|
335
|
+
DoEvents();
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
isWritten = false;
|
340
|
+
|
341
|
+
}
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
try
|
346
|
+
|
347
|
+
{
|
348
|
+
|
349
|
+
var bytes = Tcp.Read(new T());
|
350
|
+
|
351
|
+
Index = new T().CreateFromHid(bytes).UiValue;
|
352
|
+
|
353
|
+
Signal = new SolidColorBrush(Color.FromArgb(0xff, 0x00, 0xff, 0x00));
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
isRead = true;
|
358
|
+
|
359
|
+
}
|
360
|
+
|
361
|
+
catch (Exception)
|
362
|
+
|
363
|
+
{
|
364
|
+
|
365
|
+
DoEvents();
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
isRead = false;
|
370
|
+
|
371
|
+
}
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
if (!isWritten || !isRead)
|
376
|
+
|
377
|
+
{
|
378
|
+
|
379
|
+
Signal = Brushes.Red;
|
380
|
+
|
381
|
+
}
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
if (!isWritten && !isRead)
|
386
|
+
|
387
|
+
{
|
388
|
+
|
389
|
+
return new MainViewModel.Response()
|
390
|
+
|
391
|
+
{
|
392
|
+
|
393
|
+
ItemName = ItemName,
|
394
|
+
|
395
|
+
Exception = new ApiException("において、読み書きが失敗しました。")
|
396
|
+
|
397
|
+
};
|
398
|
+
|
399
|
+
}
|
400
|
+
|
401
|
+
if (isWritten && !isRead)
|
402
|
+
|
403
|
+
{
|
404
|
+
|
405
|
+
return new MainViewModel.Response()
|
406
|
+
|
407
|
+
{
|
408
|
+
|
409
|
+
ItemName = ItemName,
|
410
|
+
|
411
|
+
Exception = new ApiException("において、読込が失敗しました。")
|
412
|
+
|
413
|
+
};
|
414
|
+
|
415
|
+
}
|
416
|
+
|
417
|
+
if (!isWritten)
|
418
|
+
|
419
|
+
{
|
420
|
+
|
421
|
+
return new MainViewModel.Response()
|
422
|
+
|
423
|
+
{
|
424
|
+
|
425
|
+
ItemName = ItemName,
|
426
|
+
|
427
|
+
Exception = new ApiException("において、書き込みが失敗しました。")
|
428
|
+
|
429
|
+
};
|
430
|
+
|
431
|
+
}
|
432
|
+
|
433
|
+
return null;
|
434
|
+
|
435
|
+
}
|
436
|
+
|
437
|
+
}
|
438
|
+
|
439
|
+
```
|
440
|
+
|
441
|
+
|
442
|
+
|
443
|
+
こちらが、ItemViewModelクラスになります。
|