-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
110 lines (99 loc) · 2.62 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
using MBClient;
using System.IO.Ports;
//ModbusClient client = new ModbusTCPClient("localhost", 502);
ModbusClient client = new ModbusRTUClient("COM3", 9600, Parity.Even, 8, StopBits.One);
float[,] brightness = Bitmaps.GetBrightness("PURC0001.gif", 10, 10);
// GIF DATA
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
Console.Write($"{brightness[i, j]}\t");
Console.WriteLine();
}
Console.WriteLine("WMR");
// WMR
for (int i = 0; i < 10; i++)
{
var answer =
client.QA(new ModbusWMRRequest(
Enumerable.Range(0, 10).Select(x => (ushort)(brightness[i, x] < 0.6 ? 8888 : 1)).ToArray(),
(ushort)(10 * i)));
}
Console.ReadKey();
Console.WriteLine("RHR");
// RHR
for (int i = 0; i < 10; i++)
{
var readRequest = new ModbusRHRRequest((ushort)(10 * i), 10);
var readResponse = client.QA(readRequest);
if (readResponse.Function == 0x03)
{
int byteCount = readResponse.Data[1];
for (int j = 0; j < byteCount / 2; j++)
{
ushort value = readResponse.Data.GetTwoBytes(2 + j * 2);
Console.Write($"{value}\t");
}
Console.WriteLine();
}
else
{
Console.WriteLine("Failed to read registers.");
}
}
Console.WriteLine("WMC");
// WMC
bool[,] coilValues = new bool[10, 10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
coilValues[i, j] = brightness[i, j] < 0.6;
}
}
for (int i = 0; i < 10; i++)
{
bool[] values = new bool[10];
for (int j = 0; j < 10; j++)
{
values[j] = coilValues[i, j];
}
var writeRequest = new ModbusWMCRequest(values, (ushort)(10 * i));
var writeResponse = client.QA(writeRequest);
if (writeResponse.Function == 0x0F)
{
Console.WriteLine($"Wrote coils starting at {(10 * i)} successfully.");
}
else
{
Console.WriteLine("Failed to write coils.");
}
}
Console.WriteLine("RC");
// RC
for (int i = 0; i < 10; i++)
{
var readRequest = new ModbusRCRequest((ushort)(10 * i), 10);
var readResponse = client.QA(readRequest);
if (readResponse.Function == 0x01)
{
int byteCount = readResponse.Data[1];
for (int j = 0; j < byteCount; j++)
{
byte coilByte = readResponse.Data[2 + j];
for (int k = 0; k < 8; k++)
{
bool coilValue = (coilByte & (1 << k)) != 0;
Console.Write($"{(coilValue ? 1 : 0)} ");
}
}
Console.WriteLine();
}
else
{
Console.WriteLine("Failed to read coils.");
}
}
Console.WriteLine();
Console.ReadKey();
client.CloseClient();