/
mtest.py
169 lines (143 loc) · 4.86 KB
/
mtest.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from pymodbus.client.sync import ModbusTcpClient
from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadBuilder
from pymodbus.payload import BinaryPayloadDecoder
import pymssql
import time
import os
hostname = "192.168.30.1" # example
check = 0
def ping_reboot():
global check
response = os.system("ping -c 1 " + hostname)
print '--------------------- checking network ---------------------> ', check
# and then check the response...
if response == 0:
print hostname, 'is up!'
return check
else:
print hostname, 'is down!'
time.sleep(3)
if check > 10:
os.system("sudo reboot")
return check + 1
def read_holding_register(client, type, i):
time.sleep(0.01)
global b
print b[i]
d = None
d = int(b[i])
rr = client.read_holding_registers(d, 8)
assert (rr.function_code < 0x80)
print rr
print rr.registers
decoder = BinaryPayloadDecoder.fromRegisters(list(rr.registers), Endian.Big, wordorder=Endian.Little)
print 'decoder = ' + str(decoder)
if type in ['int64']:
frr = str("{0:.2f}".format(float(decoder.decode_64bit_int()) / 1000000))
elif type in ['int32']:
frr = str(decoder.decode_32bit_int())
elif type in ['uint32']:
frr = str(decoder.decode_32bit_uint())
elif type in ['float32']:
frr = str("{0:.2f}".format(decoder.decode_32bit_float()))
print 'frr = ' + frr
reg_data.append(frr)
with open("connect_env.txt", 'r') as fr:
buf_lines = fr.readlines()
row_server = buf_lines[0:1]
row_user = buf_lines[1:2]
row_password = buf_lines[2:3]
row_database = buf_lines[3:4]
row_machine = buf_lines[4:5]
server = row_server[0].replace('server = ', '')
user = row_user[0].replace('user = ', '')
password = row_password[0].replace('password = ', '')
database = row_database[0].replace('database = ', '')
mchcd = (row_machine[0].replace('machine = ', ''))[:-2]
proc_mchcd = str("'%s'" % mchcd)
proc_value_1 = ("'N'", "%s" % proc_mchcd)
today = time.strftime("%y-%m-%d")
today_dir_path = time.strftime("%y-%b")
today_log = today + 'db.txt'
try:
conn = pymssql.connect(server[:-2], user[:-2], password[:-2], database[:-2], timeout=3)
cursor = conn.cursor()
cursor.execute('usp_modset01t_s01 %s' % proc_mchcd)
time.sleep(0.01)
row = cursor.fetchone()
conn.commit()
conn.close()
time.sleep(0.01)
row_list = list(row)
address_list = row_list[3:]
print address_list
a = address_list.index(None)
b = address_list[0:a]
reg_no = []
host = '192.168.30.150'
port = '502'
client1 = ModbusTcpClient(host, port)
host2 = '192.168.30.177'
client2 = ModbusTcpClient(host2, port)
while True:
check = ping_reboot()
##LS-001
# host = '192.168.30.150'
# port = '502'
# client = ModbusTcpClient(host, port)
client1.connect()
reg_data = []
print 'client connection-------------------- ', client1.connect()
for i in range(0, a):
if i in [1, 27, 28, 29, 30]:
read_holding_register(client1,'int32', i)
elif i in [2, 3, 17, 26]:
read_holding_register(client1,'int64', i)
elif i in [31]:
read_holding_register(client1,'uint32', i)
else:
read_holding_register(client1,'float32', i)
reg_data.insert(0, mchcd)
print reg_data
prod_data = tuple(reg_data)
print prod_data
time.sleep(5)
conn = pymssql.connect(server[:-2], user[:-2], password[:-2], database[:-2], timeout=3)
cursor = conn.cursor()
cursor.callproc('usp_toSambaDBt', prod_data)
time.sleep(0.01)
conn.commit()
conn.close()
client1.close()
##LS-002
# host2 = '192.168.30.177'
# port = '502'
mchcd2 = '2053'
# client2 = ModbusTcpClient(host2, port)
client2.connect()
reg_data = []
for i in range(0, a):
if i in [1, 27, 28, 29, 30]:
read_holding_register(client1,'int32', i)
elif i in [2, 3, 17, 26]:
read_holding_register(client1,'int64', i)
elif i in [31]:
read_holding_register(client1,'uint32', i)
else:
read_holding_register(client1,'float32', i)
# reg_data.append(rr2.registers[0])
reg_data.insert(0, mchcd2)
print reg_data
prod_data = tuple(reg_data)
print prod_data
time.sleep(5)
conn = pymssql.connect(server[:-2], user[:-2], password[:-2], database[:-2], timeout=3)
cursor = conn.cursor()
cursor.callproc('usp_toSambaDBt', prod_data)
time.sleep(0.01)
conn.commit()
conn.close()
client2.close()
except ValueError:
print("Error")