-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathExternalTests.cs
More file actions
137 lines (113 loc) · 3.67 KB
/
ExternalTests.cs
File metadata and controls
137 lines (113 loc) · 3.67 KB
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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections;
using System.Text;
using System.Threading.Tasks;
using Waher.Events;
using Waher.Events.Console;
using Waher.Networking.Sniffers;
using Waher.Persistence;
using Waher.Persistence.Files;
using Waher.Persistence.Serialization;
using Waher.Runtime.Console;
using Waher.Runtime.Inventory;
using Waher.Runtime.Settings;
namespace Waher.Networking.Modbus.Test
{
[TestClass]
public class ExternalTests
{
internal static ConsoleOutSniffer sniffer = null;
private static ConsoleEventSink consoleEventSink = null;
private static FilesProvider filesProvider = null;
private static string host;
private static int port;
[AssemblyInitialize]
public static async Task AssemblyInitialize(TestContext _)
{
Types.Initialize(
typeof(Database).Assembly,
typeof(FilesProvider).Assembly,
typeof(ObjectSerializer).Assembly,
typeof(RuntimeSettings).Assembly);
Log.Register(consoleEventSink = new ConsoleEventSink());
filesProvider = await FilesProvider.CreateAsync("Data", "Default", 8192, 10000, 8192, Encoding.UTF8, 10000);
Database.Register(filesProvider);
sniffer = new ConsoleOutSniffer(BinaryPresentationMethod.Hexadecimal, LineEnding.NewLine);
}
[AssemblyCleanup]
public static async Task AssemblyCleanup()
{
if (filesProvider is not null)
{
await filesProvider.DisposeAsync();
filesProvider = null;
}
if (consoleEventSink is not null)
{
Log.Unregister(consoleEventSink);
consoleEventSink = null;
}
if (sniffer is not null)
{
await sniffer.DisposeAsync();
sniffer = null;
}
}
[ClassInitialize]
public static async Task ClassInitialize(TestContext _)
{
//await RuntimeSettings.SetAsync("ModbusGateway.Host", ENTER IP ADDRESS HERE);
host = await RuntimeSettings.GetAsync("ModbusGateway.Host", "lab.tagroot.io");
port = (int)await RuntimeSettings.GetAsync("ModbusGateway.Port", ModbusTcpClient.DefaultPort);
}
[TestMethod]
public async Task Test_01_Connect()
{
using ModbusTcpClient Client = await ModbusTcpClient.Connect(host, port, sniffer);
Assert.IsTrue(Client.Connected);
}
[TestMethod]
public async Task Test_02_ReadRegisters()
{
using ModbusTcpClient Client = await ModbusTcpClient.Connect(host, port, sniffer);
Assert.IsTrue(Client.Connected);
ushort[] Words = await Client.ReadMultipleRegisters(1, 0, 16);
int i = 0;
foreach (ushort Word in Words)
ConsoleOut.WriteLine((i++).ToString("X2") + ": " + Word.ToString("X4"));
}
[TestMethod]
public async Task Test_03_ReadCoils()
{
using ModbusTcpClient Client = await ModbusTcpClient.Connect(host, port, sniffer);
Assert.IsTrue(Client.Connected);
BitArray Coils = await Client.ReadCoils(1, 0, 100);
int i = 0;
int c = Coils.Length;
for (i = 0; i < c; i++)
ConsoleOut.WriteLine(i.ToString() + ": " + Coils[i].ToString());
}
[TestMethod]
public async Task Test_04_ReadInputDiscretes()
{
using ModbusTcpClient Client = await ModbusTcpClient.Connect(host, port, sniffer);
Assert.IsTrue(Client.Connected);
BitArray Coils = await Client.ReadInputDiscretes(1, 0, 100);
int i = 0;
int c = Coils.Length;
for (i = 0; i < c; i++)
ConsoleOut.WriteLine(i.ToString() + ": " + Coils[i].ToString());
}
[TestMethod]
public async Task Test_05_ReadInputRegisters()
{
using ModbusTcpClient Client = await ModbusTcpClient.Connect(host, port, sniffer);
Assert.IsTrue(Client.Connected);
ushort[] Words = await Client.ReadInputRegisters(1, 0, 16);
int i = 0;
foreach (ushort Word in Words)
ConsoleOut.WriteLine((i++).ToString("X2") + ": " + Word.ToString("X4"));
}
}
}