Skip to content

Commit c0a33ed

Browse files
committed
pyModbusTCP.client: use a logger as in server
1 parent 80e0fc3 commit c0a33ed

20 files changed

+149
-112
lines changed

CHANGES

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
Revision history for pyModbusTCP
22

3+
0.3.0.dev0 xxxx-xx-xx
4+
5+
- pyModbusTCP.client: now use the standard logging method as in the server part.
6+
- ModbusClient: debug flag is removed (see examples/client_debug.py).
7+
38
0.2.2 2024-07-31
49

510
- fix ModbusServer: wrong check of discrete inputs length in DataBank (thanks to OTnetproj).

docs/quickstart/index.rst

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -129,30 +129,29 @@ See http://en.wikipedia.org/wiki/Modbus for full table.
129129
| | Read Device Identification | 43 | :py:meth:`~pyModbusTCP.client.ModbusClient.read_device_identification` |
130130
+------------+------------------------------+---------------+--------------------------------------------------------------------------+
131131

132-
ModbusClient: debug mode
133-
------------------------
132+
ModbusClient: how-to debug
133+
--------------------------
134134

135-
If need, you can enable a debug mode for ModbusClient like this::
135+
If need, you can enable debug log for ModbusClient like this::
136+
137+
import logging
136138

137139
from pyModbusTCP.client import ModbusClient
138-
c = ModbusClient(host="localhost", port=502, debug=True)
139140

140-
or::
141+
# set debug level for pyModbusTCP.client to see frame exchanges
142+
logging.basicConfig()
143+
logging.getLogger('pyModbusTCP.client').setLevel(logging.DEBUG)
141144

142-
c.debug = True
145+
c = ModbusClient(host="localhost", port=502)
143146

144-
when debug is enable all debug message is print on console and you can see
145-
modbus frame::
147+
when debug level is set, all debug messages are displayed on the console::
146148

147-
c.read_holding_registers(0, 4)
149+
c.read_coils(0)
148150

149-
print::
151+
will give us the following result::
150152

151-
Tx
152-
[E7 53 00 00 00 06 01] 03 00 00 00 04
153-
Rx
154-
[E7 53 00 00 00 0B 01] 03 08 00 00 00 6F 00 00 00 00
155-
[0, 111, 0, 0]
153+
DEBUG:pyModbusTCP.client:(localhost:502:1) Tx [8F 8A 00 00 00 06 01] 01 00 00 00 01
154+
DEBUG:pyModbusTCP.client:(localhost:502:1) Rx [8F 8A 00 00 00 04 01] 01 01 00
156155

157156

158157
utils module: Modbus data mangling

examples/client_debug.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python3
2+
3+
""" An example of basic logging for ModbusClient debugging purposes. """
4+
5+
import logging
6+
import time
7+
8+
from pyModbusTCP.server import ModbusServer
9+
from pyModbusTCP.client import ModbusClient
10+
11+
# a logger for this script
12+
logger = logging.getLogger(__name__)
13+
14+
# global log conf: sets a default format and level for all loggers in this application (include pyModbusTCP)
15+
logging.basicConfig(format='%(asctime)s - %(name)-20s - %(levelname)-8s - %(message)s', level=logging.INFO)
16+
# set debug level for pyModbusTCP.client to see frame exchanges
17+
logging.getLogger('pyModbusTCP.client').setLevel(logging.DEBUG)
18+
19+
# run a modbus server at localhost:5020
20+
ModbusServer(host='localhost', port=5020, no_block=True).start()
21+
22+
# this message is show
23+
logger.info(f'app startup')
24+
25+
# init modbus client to connect to localhost:5020
26+
c = ModbusClient(port=5020)
27+
28+
# main loop
29+
for i in range(100):
30+
# this message is not not show (global log level is set to INFO)
31+
logger.debug(f'run loop #{i}')
32+
# modbus i/o
33+
c.read_coils(0)
34+
time.sleep(2)

examples/client_float.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
""" How-to add float support to ModbusClient. """
44

55
from pyModbusTCP.client import ModbusClient
6-
from pyModbusTCP.utils import encode_ieee, decode_ieee, \
7-
long_list_to_word, word_list_to_long
6+
from pyModbusTCP.utils import (decode_ieee, encode_ieee, long_list_to_word,
7+
word_list_to_long)
88

99

1010
class FloatModbusClient(ModbusClient):

examples/client_read_coils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
""" Read 10 coils and print result on stdout. """
44

55
import time
6+
67
from pyModbusTCP.client import ModbusClient
78

89
# init modbus client
9-
c = ModbusClient(host='localhost', port=502, auto_open=True, debug=False)
10+
c = ModbusClient(host='localhost', port=502, auto_open=True)
1011

1112
# main read loop
1213
while True:

examples/client_read_h_registers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
""" Read 10 holding registers and print result on stdout. """
44

55
import time
6-
from pyModbusTCP.client import ModbusClient
76

7+
from pyModbusTCP.client import ModbusClient
88

99
# init modbus client
10-
c = ModbusClient(debug=False, auto_open=True)
10+
c = ModbusClient(auto_open=True)
1111

1212
# main read loop
1313
while True:

examples/client_serial_gw.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
import argparse
1414
import logging
1515
import struct
16-
from pyModbusTCP.client import ModbusClient
17-
from pyModbusTCP.utils import crc16
18-
from pyModbusTCP.constants import EXP_GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND
16+
1917
# need sudo pip3 install pyserial==3.4
2018
from serial import Serial, serialutil
2119

20+
from pyModbusTCP.client import ModbusClient
21+
from pyModbusTCP.constants import EXP_GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND
22+
from pyModbusTCP.utils import crc16
23+
2224

2325
# some class
2426
class ModbusRTUFrame:

examples/client_thread.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"""
1010

1111
import time
12-
from threading import Thread, Lock
12+
from threading import Lock, Thread
13+
1314
from pyModbusTCP.client import ModbusClient
1415

1516
SERVER_HOST = "localhost"

examples/client_write_coils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
"""Write 4 coils to True, wait 2s, write False and redo it."""
44

55
import time
6+
67
from pyModbusTCP.client import ModbusClient
78

89
# init
9-
c = ModbusClient(host='localhost', port=502, auto_open=True, debug=False)
10+
c = ModbusClient(host='localhost', port=502, auto_open=True)
1011
bit = True
1112

1213
# main loop

examples/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
import argparse
1414
import logging
15-
from pyModbusTCP.server import ModbusServer
1615

16+
from pyModbusTCP.server import ModbusServer
1717

1818
# init logging
1919
logging.basicConfig()

0 commit comments

Comments
 (0)