Skip to content

Commit ae211b4

Browse files
committed
refactor conf file
1 parent a358d70 commit ae211b4

3 files changed

Lines changed: 38 additions & 47 deletions

File tree

README.md

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,36 @@ pip uninstall ser2tcp
5555
## Configuration file example
5656

5757
```json
58-
[
59-
{
60-
"serial": {
61-
"port": "/dev/ttyUSB0",
62-
"baudrate": 115200,
63-
"parity": "NONE",
64-
"stopbits": "ONE"
65-
},
66-
"servers": [
67-
{
68-
"address": "127.0.0.1",
69-
"port": 10001,
70-
"protocol": "tcp"
58+
{
59+
"ports": [
60+
{
61+
"serial": {
62+
"port": "/dev/ttyUSB0",
63+
"baudrate": 115200,
64+
"parity": "NONE",
65+
"stopbits": "ONE"
7166
},
72-
{
73-
"address": "0.0.0.0",
74-
"port": 10002,
75-
"protocol": "telnet",
76-
"send_timeout": 5.0,
77-
"buffer_limit": 65536
78-
}
79-
]
80-
}
81-
]
67+
"servers": [
68+
{
69+
"address": "127.0.0.1",
70+
"port": 10001,
71+
"protocol": "tcp"
72+
},
73+
{
74+
"address": "0.0.0.0",
75+
"port": 10002,
76+
"protocol": "telnet",
77+
"send_timeout": 5.0,
78+
"buffer_limit": 65536
79+
}
80+
]
81+
}
82+
]
83+
}
8284
```
8385

86+
Legacy format (JSON array at root level) is still supported for backward compatibility.
87+
8488
### Serial configuration
8589

8690
`serial` structure pass all parameters to [serial.Serial](https://pythonhosted.org/pyserial/pyserial_api.html#classes) constructor from pyserial library, this allows full control of the serial port.

ser2tcp.conf

Lines changed: 0 additions & 22 deletions
This file was deleted.

ser2tcp/main.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,21 @@ def main():
7676
log = _logging.getLogger('ser2tcp')
7777
log.setLevel((30, 20, 10)[min(2, args.verbose)])
7878

79-
configuration = []
8079
with open(args.config, "r", encoding='utf-8') as config_file:
8180
configuration = _json.load(config_file)
8281

82+
if isinstance(configuration, list):
83+
ports = configuration
84+
elif isinstance(configuration, dict):
85+
ports = configuration.get('ports', [])
86+
else:
87+
raise SystemExit("Invalid configuration format")
88+
89+
if not ports:
90+
raise SystemExit("No ports configured")
91+
8392
servers_manager = _server_manager.ServersManager()
84-
for config in configuration:
93+
for config in ports:
8594
servers_manager.add_server(_serial_proxy.SerialProxy(config, log))
8695

8796
_signal.signal(_signal.SIGTERM, servers_manager.stop)

0 commit comments

Comments
 (0)