1- # -*- coding: utf-8 -*-
21
3- # Copyright 2023 Regulus
42
5- # Permission is hereby granted, free of charge, to any person obtaining a
6- # copy of this software and associated documentation files (the "Software"),
7- # to deal in the Software without restriction, including without limitation
8- # the rights to use, copy, modify, merge, publish, distribute, sublicense,
9- # and/or sell copies of the Software, and to permit persons to whom the
10- # Software is furnished to do so, subject to the following conditions:
3+ """main.py
114
12- # The above copyright notice and this permission notice shall be included in
13- # all copies or substantial portions of the Software.
5+ Communication Middleware for TRNSYS Simulation
146
15- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21- # THE SOFTWARE.
7+ This module provides functionality to interface with Modbus servers as part of a TRNSYS simulation.
8+ It defines a `ModbusServer` class for handling connections, reading, and writing to Modbus servers.
9+ The module is designed to work with TRNSYS, a transient system simulation program, by providing
10+ custom functions for different stages of the simulation process such as initialization, time step
11+ processing, and simulation end.
2212
23- # --------------------------------------------------------------------------
13+ The module also includes helper functions for initializing Modbus server connections based on
14+ configured settings and for handling various TRNSYS simulation stages like start time, iteration,
15+ end of time step, and last call of the simulation.
16+
17+ Classes
18+ -------
19+ ModbusServer
20+ A class representing a Modbus server, providing methods for connecting, reading, and writing data.
21+
22+ Functions
23+ ---------
24+ define_servers(server_configs)
25+ Initializes Modbus servers based on provided configuration.
26+
27+ Initialization(TRNData)
28+ Initializes the global variable 'servers' and connects to servers for the TRNSYS simulation.
29+
30+ StartTime(TRNData)
31+ Handles actions to be performed at the start time of TRNSYS simulation.
32+
33+ Iteration(TRNData)
34+ Handles actions for each TRNSYS iteration within a time step.
35+
36+ EndOfTimeStep(TRNData)
37+ Handles end-of-time-step actions for the connected servers based on TRNData.
2438
25- __author__ = "Erika Langerová, Michal Broum"
26- __copyright__ = "<2023> <Regulus>"
39+ LastCallOfSimulation(TRNData)
40+ Closes connections and performs cleanup at the end of the TRNSYS simulation.
41+
42+ Notes
43+ -----
44+ - The module uses global variables and relies on specific configuration files (`server_config`
45+ and `middleware_config`).
46+ - It is tailored to work with the TRNSYS simulation environment, specifically with its data handling.
47+
48+
49+ See Also
50+ --------
51+ server_config : Module providing server configurations.
52+ middleware_config : Module providing middleware configurations for the simulation.
53+
54+ """
55+
56+ __author__ = "Erika Langerová"
57+ __copyright__ = "<2023> <UCEEB CVUT>"
2758__credits__ = [" " , " " ]
2859
2960__license__ = "MIT (X11)"
3061__version__ = "1.2.1"
3162__maintainer__ = ["Erika Langerová" ]
32- __email__ = ["erika.langerova@regulus .cz" ]
33- __status__ = "Alfa "
63+ __email__ = ["erika.langerova@cvut .cz" ]
64+ __status__ = "Released "
3465
3566__python__ = "3.10.5"
3667__TRNSYS__ = "18.04.0000"
3768
3869# --------------------------------------------------------------------------
3970
40- import logging
71+ # Standard library imports
4172import time as osTime
4273from typing import Dict , List , Union , Optional
74+
75+ # Third party imports
76+ import logging
4377from pymodbus .client import ModbusTcpClient
4478from pymodbus .payload import BinaryPayloadBuilder , Endian
79+
80+ # Local imports
4581from server_config import SERVER_CONFIGS
4682from middleware_config import SIM_SLEEP , SIMULATION_MODEL , LOGGING_FILENAME
4783
48-
4984# --------------------------------------------------------------------------
5085
5186class ModbusServer :
@@ -238,12 +273,10 @@ def define_servers(server_configs: List[Dict[str, Union[str, int, List[int], int
238273 servers .append (server )
239274 return servers
240275
241-
242-
243276# --------------------------------------------------------------------------------
244277# START
245278# --------------------------------------------------------------------------------
246- # ...
279+
247280
248281def Initialization (TRNData : Dict [str , Dict [str , List [Union [int , float ]]]]) -> None :
249282 """
@@ -292,8 +325,6 @@ def Initialization(TRNData: Dict[str, Dict[str, List[Union[int, float]]]]) -> No
292325 server .close_connection ()
293326
294327
295- # --------------------------------------------------------------------------------
296-
297328def StartTime (TRNData : Dict [str , Dict [str , List [Union [int , float ]]]]) -> None :
298329 """
299330 Function called at TRNSYS starting time (not an actual time step,
@@ -313,8 +344,6 @@ def StartTime(TRNData: Dict[str, Dict[str, List[Union[int, float]]]]) -> None:
313344
314345 return
315346
316-
317- # --------------------------------------------------------------------------------
318347
319348def Iteration (TRNData : Dict [str , Dict [str , List [Union [int , float ]]]]) -> None :
320349 """
@@ -334,7 +363,6 @@ def Iteration(TRNData: Dict[str, Dict[str, List[Union[int, float]]]]) -> None:
334363
335364 return
336365
337- # --------------------------------------------------------------------------------
338366
339367def EndOfTimeStep (TRNData : Dict [str , Dict [str , List [Union [int , float ]]]]) -> None :
340368 """
@@ -391,7 +419,6 @@ def EndOfTimeStep(TRNData: Dict[str, Dict[str, List[Union[int, float]]]]) -> Non
391419
392420 osTime .sleep (SIM_SLEEP )
393421
394- # --------------------------------------------------------------------------------
395422
396423def LastCallOfSimulation (TRNData : Dict [str , Dict [str , List [Union [int , float ]]]]) -> None :
397424 """
0 commit comments