-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodbus_address_scan.py
39 lines (32 loc) · 1.08 KB
/
modbus_address_scan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from pymodbus.constants import Endian
from pymodbus.client.sync import ModbusSerialClient
from pymodbus.payload import BinaryPayloadDecoder
import time
import logging
FORMAT = ('%(threadName)-15s '
'%(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
#logging.basicConfig(format=FORMAT)
log = logging.getLogger()
log.setLevel(0) # 0 = NONE ; 10 = DEBUG
def scanAddress():
client = ModbusSerialClient(method='rtu', port='COM3', timeout=1, baudrate=9600)
connected = client.connect()
log.debug("Connected: " + str(connected))
if connected:
log.debug("Reading registers..")
available = []
# (Target register, read length, device id)
# read holding register is command 0x03
#Change range to check more addresses
for i in range(0xffff):
result = client.read_holding_registers(4, 2, unit=i) #just put in random addresses. it'll answer with illegal addresses if found
if result != 0:
available.append(i)
#log.debug(result)
print(i)
print(result)
time.sleep(.25)
return available
if __name__ == "__main__":
slaves = scanAddress()
print(slaves)