Skip to content

Commit ccdde7c

Browse files
authored
Allow integer values in register types 3 and 4 (#27)
1 parent 35f4b34 commit ccdde7c

File tree

4 files changed

+37
-36
lines changed

4 files changed

+37
-36
lines changed

Dockerfile

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
FROM python:3.10.9-alpine3.17
22

33
LABEL maintainer="Michael Oberdorf IT-Consulting <info@oberdorf-itc.de>"
4-
LABEL site.local.vendor="Michael Oberdorf IT-Consulting"
5-
LABEL site.local.os.main="Linux"
6-
LABEL site.local.os.dist="Alpine"
7-
LABEL site.local.os.version="3.17"
8-
LABEL site.local.runtime.name="Python"
9-
LABEL site.local.runtime.version="3.10.9"
10-
LABEL site.local.program.name="Python Modbus TCP Server"
11-
LABEL site.local.program.version="1.1.5"
4+
LABEL site.local.program.version="1.2.0"
125

136
COPY --chown=root:root /src /
147

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ Container image: [DockerHub](https://hub.docker.com/r/oitc/modbus-server)
88

99
# Supported tags and respective `Dockerfile` links
1010

11-
* [`latest`, `1.1.5`](https://github.com/cybcon/modbus-server/blob/v1.1.5/Dockerfile)
11+
* [`latest`, `1.2.0`](https://github.com/cybcon/modbus-server/blob/v1.2.0/Dockerfile)
12+
* [`1.1.5`](https://github.com/cybcon/modbus-server/blob/v1.1.5/Dockerfile)
1213
* [`1.1.4`](https://github.com/cybcon/modbus-server/blob/v1.1.4/Dockerfile)
1314
* [`1.1.3`](https://github.com/cybcon/modbus-server/blob/v1.1.3/Dockerfile)
1415
* [`1.1.2`](https://github.com/cybcon/modbus-server/blob/v1.1.2/Dockerfile)
@@ -132,6 +133,7 @@ Example configuration of pre-defined registers from type "Discrete Input" or "Co
132133
```
133134

134135
As by the modbus spec, the "Holding Registers" and "Input Registers" tables contains a 16-bit word. In the json configuration file, we use a hexadecimal notation, starting with `0x`, as register values.
136+
With v1.2.0 of the modbus-server, you can also use integer values (0-65535) instead.
135137

136138
Example configuration of pre-defined registers from type "Holding Registers" or "Input Registers":
137139
```

examples/test.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@
2929
"4": false
3030
},
3131
"holdingRegister": {
32-
"1": "0xCC01",
32+
"1": 52225,
3333
"2": "0xCC02",
34-
"3": "0xCC03",
34+
"3": 52227,
3535
"4": "0xCC04"
3636
},
3737
"inputRegister": {
3838
"1": "0xDD01",
39-
"2": "0xDD02",
39+
"2": 56578,
4040
"3": "0xDD03",
41-
"4": "0xDD04"
41+
"4": 56580
4242
}
4343
}
4444
}

src/app/modbus_server.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
Modbus TCP server script for debugging
44
Author: Michael Oberdorf IT-Consulting
55
Datum: 2020-03-30
6-
Last modified by: Hackergarden Meetup@Codecentric
7-
Last modified at: 2023-11-08
6+
Last modified by: Michael Oberdorf
7+
Last modified at: 2023-11-13
88
*************************************************************************** """
99
import sys
1010
import os
@@ -21,7 +21,7 @@
2121

2222
# default configuration file path
2323
config_file='/app/modbus_server.json'
24-
VERSION='1.1.5'
24+
VERSION='1.2.0'
2525
"""
2626
###############################################################################
2727
# F U N C T I O N S
@@ -123,7 +123,7 @@ def run_server(listener_address: str = '0.0.0.0', listener_port: int = 5020, tls
123123
identity.VendorUrl = 'http://github.com/riptideio/pymodbus/'
124124
identity.ProductName = 'Pymodbus Server'
125125
identity.ModelName = 'Pymodbus Server'
126-
identity.MajorMinorRevision = '2.3.0'
126+
identity.MajorMinorRevision = '2.5.3'
127127

128128
# ----------------------------------------------------------------------- #
129129
# run the server
@@ -156,27 +156,33 @@ def prepareRegister(register: dict, initType: str, initializeUndefinedRegisters:
156156
if len(register) == 0: return(outRegister)
157157

158158
for key in register:
159-
if isinstance(key, str):
160-
keyOut = int(key, 0)
161-
log.debug(' Transform register id: ' + str(key) + ' ('+ str(type(key)) + ') to: ' + str(keyOut) + ' (' + str(type(keyOut)) + ')')
162-
else: keyOut = key
159+
if isinstance(key, str):
160+
keyOut = int(key, 0)
161+
log.debug(' Transform register id: ' + str(key) + ' ('+ str(type(key)) + ') to: ' + str(keyOut) + ' (' + str(type(keyOut)) + ')')
162+
else: keyOut = key
163163

164-
val = register[key]
164+
val = register[key]
165+
valOut = val
166+
if initType == 'word' and isinstance(val, str) and str(val)[0:2] == '0x' and len(val) >= 3 and len(val) <= 6:
167+
valOut = int(val, 16)
168+
log.debug(' Transform value for register: ' + str(keyOut) + ' from: ' + str(val) + ' ('+ str(type(key)) + ') to: ' + str(valOut) + ' (' + str(type(valOut)) + ')')
169+
elif initType == 'word' and isinstance(val, int) and val >=0 and val <= 65535:
165170
valOut = val
166-
if initType == 'word' and isinstance(val, str) and str(val)[0:2] == '0x':
167-
valOut = int(val, 16)
168-
log.debug(' Transform value for register: ' + str(keyOut) + ' from: ' + str(val) + ' ('+ str(type(key)) + ') to: ' + str(valOut) + ' (' + str(type(valOut)) + ')')
169-
elif initType == 'boolean':
170-
if isinstance(val, bool):
171-
valOut = val
172-
log.debug(' Set register: ' + str(keyOut) + ' to: ' + str(valOut) + ' (' + str(type(valOut)) + ')')
173-
elif isinstance(val, int):
174-
if val == 0:
175-
valOut = False
176-
else:
177-
valOut = True
178-
log.debug(' Transform value for register: ' + str(keyOut) + ' from: ' + str(val) + ' ('+ str(type(key)) + ') to: ' + str(valOut) + ' (' + str(type(valOut)) + ')')
179-
outRegister[keyOut] = valOut
171+
log.debug(' Use value for register: {}: {}'.format(str(keyOut), str(valOut)))
172+
elif initType == 'boolean':
173+
if isinstance(val, bool):
174+
valOut = val
175+
log.debug(' Set register: ' + str(keyOut) + ' to: ' + str(valOut) + ' (' + str(type(valOut)) + ')')
176+
elif isinstance(val, int):
177+
if val == 0:
178+
valOut = False
179+
else:
180+
valOut = True
181+
log.debug(' Transform value for register: ' + str(keyOut) + ' from: ' + str(val) + ' ('+ str(type(key)) + ') to: ' + str(valOut) + ' (' + str(type(valOut)) + ')')
182+
else:
183+
log.error(' Malformed input or input is out of range for register: {} -> value is {} - skip this register initialization!'.format(str(keyOut), str(val)))
184+
continue
185+
outRegister[keyOut] = valOut
180186

181187
if initializeUndefinedRegisters:
182188
if initType == 'word':

0 commit comments

Comments
 (0)