File: test_device.py

package info (click to toggle)
pymodbus 3.8.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,720 kB
  • sloc: python: 14,867; makefile: 27; sh: 17
file content (364 lines) | stat: -rw-r--r-- 13,319 bytes parent folder | download
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
"""Test device."""
from pymodbus.constants import DeviceInformation
from pymodbus.device import (
    DeviceInformationFactory,
    ModbusControlBlock,
    ModbusDeviceIdentification,
    ModbusPlusStatistics,
)
from pymodbus.events import RemoteReceiveEvent


# ---------------------------------------------------------------------------#
#  Fixture
# ---------------------------------------------------------------------------#


class TestDataStore:
    """Unittest for the pymodbus.device module."""

    # -----------------------------------------------------------------------#
    #  Setup/TearDown
    # -----------------------------------------------------------------------#

    info: dict
    ident: ModbusDeviceIdentification
    control: ModbusControlBlock

    def setup_method(self):
        """Do setup."""
        self.info = {
            0x00: "Bashwork",  # VendorName
            0x01: "PTM",  # ProductCode
            0x02: "1.0",  # MajorMinorRevision
            0x03: "http://internets.com",  # VendorUrl
            0x04: "pymodbus",  # ProductName
            0x05: "bashwork",  # ModelName
            0x06: "pytest",  # UserApplicationName
            0x07: "x",  # reserved
            0x08: "x",  # reserved
            0x10: "reserved",  # reserved
            0x80: "custom1",  # device specific start
            0x82: "custom2",  # device specific
            0xFF: "customlast",  # device specific last
        }
        self.ident = ModbusDeviceIdentification(self.info)
        self.control = ModbusControlBlock()
        self.control.reset()

    def test_update_identity(self):
        """Test device identification reading."""
        self.control.Identity.update(self.ident)
        assert self.control.Identity.VendorName == "Bashwork"
        assert self.control.Identity.ProductCode == "PTM"
        assert self.control.Identity.MajorMinorRevision == "1.0"
        assert self.control.Identity.VendorUrl == "http://internets.com"
        assert self.control.Identity.ProductName == "pymodbus"
        assert self.control.Identity.ModelName == "bashwork"
        assert self.control.Identity.UserApplicationName == "pytest"

    def test_device_identification_factory(self):
        """Test device identification reading."""
        self.control.Identity.update(self.ident)
        result = DeviceInformationFactory.get(
            self.control, DeviceInformation.SPECIFIC, 0x00
        )
        assert result[0x00] == "Bashwork"

        result = DeviceInformationFactory.get(
            self.control, DeviceInformation.BASIC, 0x00
        )
        assert result[0x00] == "Bashwork"
        assert result[0x01] == "PTM"
        assert result[0x02] == "1.0"

        result = DeviceInformationFactory.get(
            self.control, DeviceInformation.REGULAR, 0x00
        )
        assert result[0x00] == "Bashwork"
        assert result[0x01] == "PTM"
        assert result[0x02] == "1.0"
        assert result[0x03] == "http://internets.com"
        assert result[0x04] == "pymodbus"
        assert result[0x05] == "bashwork"
        assert result[0x06] == "pytest"

    def test_device_identification_factory_lookup(self):
        """Test device identification factory lookup."""
        result = DeviceInformationFactory.get(
            self.control, DeviceInformation.BASIC, 0x00
        )
        assert sorted(result.keys()) == [0x00, 0x01, 0x02]
        result = DeviceInformationFactory.get(
            self.control, DeviceInformation.BASIC, 0x02
        )
        assert sorted(result.keys()) == [0x02]
        result = DeviceInformationFactory.get(
            self.control, DeviceInformation.REGULAR, 0x00
        )
        assert sorted(result.keys()) == [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06]
        result = DeviceInformationFactory.get(
            self.control, DeviceInformation.REGULAR, 0x01
        )
        assert sorted(result.keys()) == [0x01, 0x02, 0x03, 0x04, 0x05, 0x06]
        result = DeviceInformationFactory.get(
            self.control, DeviceInformation.REGULAR, 0x05
        )
        assert sorted(result.keys()) == [0x05, 0x06]
        result = DeviceInformationFactory.get(
            self.control, DeviceInformation.EXTENDED, 0x00
        )
        assert sorted(result.keys()) == [
            0x00,
            0x01,
            0x02,
            0x03,
            0x04,
            0x05,
            0x06,
            0x80,
            0x82,
            0xFF,
        ]
        result = DeviceInformationFactory.get(
            self.control, DeviceInformation.EXTENDED, 0x02
        )
        assert sorted(result.keys()) == [0x02, 0x03, 0x04, 0x05, 0x06, 0x80, 0x82, 0xFF]
        result = DeviceInformationFactory.get(
            self.control, DeviceInformation.EXTENDED, 0x06
        )
        assert sorted(result.keys()) == [0x06, 0x80, 0x82, 0xFF]
        result = DeviceInformationFactory.get(
            self.control, DeviceInformation.EXTENDED, 0x80
        )
        assert sorted(result.keys()) == [0x80, 0x82, 0xFF]
        result = DeviceInformationFactory.get(
            self.control, DeviceInformation.EXTENDED, 0x82
        )
        assert sorted(result.keys()) == [0x82, 0xFF]
        result = DeviceInformationFactory.get(
            self.control, DeviceInformation.EXTENDED, 0x81
        )
        assert sorted(result.keys()) == [
            0x00,
            0x01,
            0x02,
            0x03,
            0x04,
            0x05,
            0x06,
            0x80,
            0x82,
            0xFF,
        ]

    def test_basic_commands(self):
        """Test device identification reading."""
        assert str(self.ident) == "DeviceIdentity"
        assert str(self.control) == "ModbusControl"

    def test_modbus_device_identification_get(self):
        """Test device identification reading."""
        assert self.ident[0x00] == "Bashwork"
        assert self.ident[0x01] == "PTM"
        assert self.ident[0x02] == "1.0"
        assert self.ident[0x03] == "http://internets.com"
        assert self.ident[0x04] == "pymodbus"
        assert self.ident[0x05] == "bashwork"
        assert self.ident[0x06] == "pytest"
        assert self.ident[0x07] != "x"
        assert self.ident[0x08] != "x"
        assert self.ident[0x10] != "reserved"
        assert not self.ident[0x54]

    def test_modbus_device_identification_summary(self):
        """Test device identification summary creation."""
        summary = sorted(self.ident.summary().values())
        expected = sorted(list(self.info.values())[:0x07])  # remove private
        assert summary == expected

    def test_modbus_device_identification_set(self):
        """Test a device identification writing."""
        self.ident[0x07] = "y"
        self.ident[0x08] = "y"
        self.ident[0x10] = "public"
        self.ident[0x54] = "testing"

        assert self.ident[0x07] != "y"
        assert self.ident[0x08] != "y"
        assert self.ident[0x10] == "public"
        assert self.ident[0x54] == "testing"

    def test_modbus_control_block_ascii_modes(self):
        """Test a server control block ascii mode."""
        assert id(self.control) == id(ModbusControlBlock())
        self.control.Mode = "RTU"
        assert self.control.Mode == "RTU"
        self.control.Mode = "FAKE"
        assert self.control.Mode != "FAKE"

    def test_modbus_control_block_counters(self):
        """Tests the MCB counters methods."""
        assert not self.control.Counter.BusMessage
        for _ in range(10):
            self.control.Counter.BusMessage += 1
            self.control.Counter.SlaveMessage += 1
        assert self.control.Counter.BusMessage == 10
        self.control.Counter.BusMessage = 0x00
        assert not self.control.Counter.BusMessage
        assert self.control.Counter.SlaveMessage == 10
        self.control.Counter.reset()
        assert not self.control.Counter.SlaveMessage

    def test_modbus_control_block_update(self):
        """Tests the MCB counters update methods."""
        values = {"SlaveMessage": 5, "BusMessage": 5}
        self.control.Counter.BusMessage += 1
        self.control.Counter.SlaveMessage += 1
        self.control.Counter.update(values)
        assert self.control.Counter.SlaveMessage == 6
        assert self.control.Counter.BusMessage == 6

    def test_modbus_control_block_iterator(self):
        """Tests the MCB counters iterator."""
        self.control.Counter.reset()
        for _, count in self.control:
            assert not count

    def test_modbus_counters_handler_iterator(self):
        """Tests the MCB counters iterator."""
        self.control.Counter.reset()
        for _, count in self.control.Counter:
            assert not count

    def test_modbus_control_block_counter_summary(self):
        """Tests retrieving the current counter summary."""
        assert not self.control.Counter.summary()
        for _ in range(10):
            self.control.Counter.BusMessage += 1
            self.control.Counter.SlaveMessage += 1
            self.control.Counter.SlaveNAK += 1
            self.control.Counter.BusCharacterOverrun += 1
        assert self.control.Counter.summary() == 0xA9
        self.control.Counter.reset()
        assert not self.control.Counter.summary()

    def test_modbus_control_block_listen(self):
        """Test the MCB listen flag methods."""
        self.control.ListenOnly = False
        assert not self.control.ListenOnly
        self.control.ListenOnly = not self.control.ListenOnly
        assert self.control.ListenOnly

    def test_modbus_control_block_delimiter(self):
        """Tests the MCB delimiter setting methods."""
        self.control.Delimiter = b"\r"
        assert self.control.Delimiter == b"\r"
        self.control.Delimiter = "="
        assert self.control.Delimiter == b"="  # type: ignore[comparison-overlap]
        self.control.Delimiter = 61
        assert self.control.Delimiter == b"="  # type: ignore[comparison-overlap]

    def test_modbus_control_block_diagnostic(self):
        """Tests the MCB delimiter setting methods."""
        assert self.control.getDiagnosticRegister() == [False] * 16
        for i in (1, 3, 4, 6):
            self.control.setDiagnostic({i: True})
        assert self.control.getDiagnostic(1)
        assert not self.control.getDiagnostic(2)
        actual = [False, True, False, True, True, False, True] + [False] * 9
        assert actual == self.control.getDiagnosticRegister()
        for i in range(16):
            self.control.setDiagnostic({i: False})

    def test_modbus_control_block_invalid_diagnostic(self):
        """Tests querying invalid MCB counters methods."""
        assert not self.control.getDiagnostic(-1)
        assert not self.control.getDiagnostic(17)
        assert not self.control.getDiagnostic(None)
        assert not self.control.getDiagnostic([1, 2, 3])

    def test_clearing_control_events(self):
        """Test adding and clearing modbus events."""
        assert self.control.Events == []
        event = RemoteReceiveEvent()
        self.control.addEvent(event)
        assert self.control.Events == [event]
        assert self.control.Counter.Event == 1
        self.control.clearEvents()
        assert self.control.Events == []
        assert self.control.Counter.Event == 1

    def test_retrieving_control_events(self):
        """Test adding and removing a host."""
        assert self.control.Events == []
        event = RemoteReceiveEvent()
        self.control.addEvent(event)
        assert self.control.Events == [event]
        packet = self.control.getEvents()
        assert packet == b"\x40"

    def test_modbus_plus_statistics(self):
        """Test device identification reading."""
        default = [0x0000] * 55
        statistics = ModbusPlusStatistics()
        assert default == statistics.encode()
        statistics.reset()
        assert default == statistics.encode()
        assert default == self.control.Plus.encode()

    def test_modbus_plus_statistics_helpers(self):
        """Test modbus plus statistics helper methods."""
        statistics = ModbusPlusStatistics()
        summary = [
            [0],
            [0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0],
            [0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0],
            [0],
            [0],
            [0],
            [0, 0],
            [0],
            [0],
            [0],
            [0],
            [0],
            [0],
            [0],
            [0, 0],
            [0],
            [0],
            [0],
            [0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0],
            [0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0],
            [0],
            [0, 0],
            [0],
            [0],
            [0],
            [0],
            [0, 0],
            [0],
            [0],
            [0],
            [0],
            [0],
            [0, 0],
            [0],
            [0, 0, 0, 0, 0, 0, 0, 0],
        ]
        stats_summary = list(statistics.summary())
        assert sorted(summary) == sorted(stats_summary)
        assert not sum(sum(value[1]) for value in statistics)