-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodbusread.py
More file actions
39 lines (31 loc) · 1.03 KB
/
modbusread.py
File metadata and controls
39 lines (31 loc) · 1.03 KB
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
#python3
from time import sleep
from random import uniform
from pymodbus3 import exceptions
from pymodbus3.client.sync import ModbusTcpClient
import json
def sync_client_read(registerNumber):
try:
#client.write_coil(1, False)
#result = client.read_coils(1,1)
#print(result.bits[0])
result = client.read_holding_registers(registerNumber,1)
return result.registers
#print(result.bits)
# except exceptions.ConnectionException:
except:
print("Connection Error Handled")
output=False
return output
#client = ModbusTcpClient('127.0.0.1')
client = ModbusTcpClient('192.168.1.21')
while 1==1:
sleep(5)
registersPerPage = 256
pagenumber = 3
startOffset = 0
endOffset = 6 + 1
for register in range(registersPerPage * pagenumber + startOffset,registersPerPage * pagenumber + endOffset):
registers = sync_client_read(register)
print("register" + str(register))
print("msg sent: register value " + str(registers))