-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodbus_client.py
38 lines (33 loc) · 1.17 KB
/
modbus_client.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
from pyModbusTCP.client import ModbusClient
from time import sleep
# import threading
client = ModbusClient('localhost', 550)
# def server_monitor():
# while True:
# # Check if server has responded
# if not client.is_open:
# print("Server not responding. Client closed.")
# client.close()
# break
# # Start the server monitor thread
# monitor_thread = threading.Thread(target=server_monitor)
# monitor_thread.daemon = True # Daemonize the thread so it terminates when the main thread terminates
# monitor_thread.start()
try:
client_state = client.open()
data = [0]
if client_state == True:
print("Client online")
while True:
if not client.is_open:
print("Server not responding. Client shutdown.")
client.close()
break
if data != client.read_holding_registers(0):
data = client.read_holding_registers(0)
print("The value in reg 0 of server is" +str(client.read_holding_registers(0)))
# sleep(0.5)
else:
print("Client can't connect to server")
except:
print("client shutown")