Skip to content

Commit 6ee58c1

Browse files
author
Tom Softreck
committed
update
1 parent 09b5e76 commit 6ee58c1

3 files changed

Lines changed: 69 additions & 33 deletions

File tree

modapi/api/rtu/config.py

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ def get_function_codes():
6161
MODE_TOGGLE = modes.get("MODE_TOGGLE", 0x02)
6262
MODE_EDGE_TRIGGER = modes.get("MODE_EDGE_TRIGGER", 0x03)
6363

64+
# Analog input types
65+
def get_analog_input_types():
66+
constants = _load_constants()
67+
analog_types = constants.get('analog_input_types', {
68+
"TYPE_0_5V": 0,
69+
"TYPE_0_10V": 1,
70+
"TYPE_0_20MA": 2,
71+
"TYPE_4_20MA": 3
72+
})
73+
return analog_types
74+
75+
analog_types = get_analog_input_types()
76+
TYPE_0_5V = analog_types.get("TYPE_0_5V", 0x00)
77+
TYPE_0_10V = analog_types.get("TYPE_0_10V", 0x01)
78+
TYPE_0_20MA = analog_types.get("TYPE_0_20MA", 0x02)
79+
TYPE_4_20MA = analog_types.get("TYPE_4_20MA", 0x03)
80+
6481
function_codes = get_function_codes()
6582
FUNC_READ_COILS = function_codes.get("READ_COILS", 0x01)
6683
FUNC_READ_DISCRETE_INPUTS = function_codes.get("READ_DISCRETE_INPUTS", 0x02)
@@ -138,21 +155,49 @@ def get_config_value(key: str, default: Any = None) -> Any:
138155
if env_value is not None:
139156
return env_value
140157

141-
# Try JSON config
142-
# TODO: Implement JSON config lookup if needed
158+
# Try JSON config from constants.json
159+
constants = _load_constants()
160+
sections = key.split('.')
161+
162+
# Navigate through nested sections
163+
current = constants
164+
for section in sections[:-1]:
165+
if section in current:
166+
current = current[section]
167+
else:
168+
return default
169+
170+
# Get the final value
171+
if sections[-1] in current:
172+
return current[sections[-1]]
143173

144174
# Return default
145175
return default
146176

147177
# Auto-detection settings
148-
AUTO_DETECT_PORTS = get_config_value('AUTO_DETECT_PORTS',
149-
['/dev/ttyACM0', '/dev/ttyUSB0'])
150-
AUTO_DETECT_BAUDRATES = get_config_value('AUTO_DETECT_BAUDRATES',
151-
[9600, 115200, 19200, 4800, 38400, 57600])
152-
AUTO_DETECT_UNIT_IDS = get_config_value('AUTO_DETECT_UNIT_IDS',
153-
[1, 2, 3, 4])
178+
def get_auto_detect_settings():
179+
constants = _load_constants()
180+
return constants.get('auto_detect', {
181+
"ports": ["/dev/ttyACM0", "/dev/ttyUSB0"],
182+
"baudrates": [9600, 115200, 19200, 4800, 38400, 57600],
183+
"unit_ids": [1, 2, 3, 4]
184+
})
185+
186+
AUTO_DETECT = get_auto_detect_settings()
187+
AUTO_DETECT_PORTS = get_env_value('RTU_AUTO_DETECT_PORTS', AUTO_DETECT.get("ports"))
188+
AUTO_DETECT_BAUDRATES = get_env_value('RTU_AUTO_DETECT_BAUDRATES', AUTO_DETECT.get("baudrates"))
189+
AUTO_DETECT_UNIT_IDS = get_env_value('RTU_AUTO_DETECT_UNIT_IDS', AUTO_DETECT.get("unit_ids"))
154190

155191
# Mock mode settings
156-
MOCK_PORT = 'MOCK'
157-
MOCK_BAUDRATE = 9600
158-
MOCK_UNIT_ID = 1
192+
def get_mock_settings():
193+
constants = _load_constants()
194+
return constants.get('mock', {
195+
"port": "MOCK",
196+
"baudrate": 9600,
197+
"unit_id": 1
198+
})
199+
200+
MOCK_SETTINGS = get_mock_settings()
201+
MOCK_PORT = get_env_value('RTU_MOCK_PORT', MOCK_SETTINGS.get("port"))
202+
MOCK_BAUDRATE = get_env_value('RTU_MOCK_BAUDRATE', MOCK_SETTINGS.get("baudrate"))
203+
MOCK_UNIT_ID = get_env_value('RTU_MOCK_UNIT_ID', MOCK_SETTINGS.get("unit_id"))

modapi/api/rtu/devices.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
from typing import List, Optional, Dict, Any, Tuple
99

1010
from .base import ModbusRTU
11-
from .protocol import (
11+
from .config import (
1212
FUNC_READ_COILS, FUNC_READ_DISCRETE_INPUTS,
1313
FUNC_READ_HOLDING_REGISTERS, FUNC_READ_INPUT_REGISTERS,
1414
FUNC_WRITE_SINGLE_COIL, FUNC_WRITE_SINGLE_REGISTER,
1515
FUNC_WRITE_MULTIPLE_COILS, FUNC_WRITE_MULTIPLE_REGISTERS,
16+
MODE_NORMAL, MODE_LINKAGE, MODE_TOGGLE, MODE_EDGE_TRIGGER,
17+
TYPE_0_5V, TYPE_0_10V, TYPE_0_20MA, TYPE_4_20MA
18+
)
19+
from .protocol import (
1620
build_read_request, build_write_single_coil_request,
1721
build_write_single_register_request, build_write_multiple_coils_request,
1822
build_write_multiple_registers_request, parse_read_coils_response,
@@ -29,12 +33,6 @@ class WaveshareIO8CH(ModbusRTU):
2933
http://www.waveshare.com/wiki/Modbus_RTU_IO_8CH
3034
"""
3135

32-
# Control modes
33-
MODE_NORMAL = 0x00
34-
MODE_LINKAGE = 0x01
35-
MODE_TOGGLE = 0x02
36-
MODE_EDGE_TRIGGER = 0x03
37-
3836
def __init__(self, port: str = '/dev/ttyACM0', baudrate: int = 9600, timeout: float = 1.0):
3937
"""Initialize Waveshare IO 8CH module connection"""
4038
super().__init__(port=port, baudrate=baudrate, timeout=timeout)
@@ -300,11 +298,8 @@ class WaveshareAnalogInput8CH(ModbusRTU):
300298
https://www.waveshare.com/wiki/Modbus_RTU_Analog_Input_8CH
301299
"""
302300

303-
# Analog input types
304-
TYPE_0_5V = 0x00
305-
TYPE_0_10V = 0x01
306-
TYPE_0_20MA = 0x02
307-
TYPE_4_20MA = 0x03
301+
# Analog input types are loaded from config
302+
# These will be added to constants.json in the next update
308303

309304
def __init__(self, port: str = '/dev/ttyACM0', baudrate: int = 9600, timeout: float = 1.0):
310305
"""Initialize Waveshare Analog Input 8CH module connection"""

modapi/api/rtu/protocol.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,15 @@
88
from typing import Optional, List, Dict, Tuple
99

1010
from .crc import calculate_crc, try_alternative_crcs
11+
from .config import (
12+
FUNC_READ_COILS, FUNC_READ_DISCRETE_INPUTS,
13+
FUNC_READ_HOLDING_REGISTERS, FUNC_READ_INPUT_REGISTERS,
14+
FUNC_WRITE_SINGLE_COIL, FUNC_WRITE_SINGLE_REGISTER,
15+
FUNC_WRITE_MULTIPLE_COILS, FUNC_WRITE_MULTIPLE_REGISTERS
16+
)
1117

1218
logger = logging.getLogger(__name__)
1319

14-
# Standard Modbus function codes
15-
FUNC_READ_COILS = 0x01
16-
FUNC_READ_DISCRETE_INPUTS = 0x02
17-
FUNC_READ_HOLDING_REGISTERS = 0x03
18-
FUNC_READ_INPUT_REGISTERS = 0x04
19-
FUNC_WRITE_SINGLE_COIL = 0x05
20-
FUNC_WRITE_SINGLE_REGISTER = 0x06
21-
FUNC_WRITE_MULTIPLE_COILS = 0x0F
22-
FUNC_WRITE_MULTIPLE_REGISTERS = 0x10
23-
2420
# Waveshare-specific function codes
2521
WAVESHARE_FUNC_READ_COILS = 0x41 # Sometimes used instead of 0x01
2622
WAVESHARE_FUNC_FLASH_COIL = 0x05 # Same as write coil but with special register

0 commit comments

Comments
 (0)