-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodbus-excel.py
307 lines (230 loc) · 10.4 KB
/
modbus-excel.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import os
import xlwings as xw
import sys
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
import time
import json
import xlsxwriter
def main():
print("Written by Furkan Ciylan \nContact me: [email protected]\nGithub: /HawkPhantom")
name = input("\nName this session:")
a = input("\nDo you want to configure the software? \n Default Values Are: \n method:rtu \n port:COM7 \n timeout:10ds \n baudrate:19200 \n stopbits:1 \n parity:Even \n bytesize:8 \n number of input registers:8 \n number of holding registers:8 \n number of coils:8 \n address:0x00 \nY/N:")
if (a == "n" or a == "N" or a == "no"):
conffile_test = input("\nDo you have a custom configuration file? Y/N:")
else:
conffile_test = ""
return(modbus(a,conffile_test,name))
def modbus(a,conffile_test,name):
if (conffile_test == "Y" or conffile_test == "y" or conffile_test =="yes"):
conffile = input("Please write the path of configuration file:")
else:
conffile = ("configuration.json")
read_selector = input("\nChoose what to read\n1)Holding Registers\n2)Input Registers\n3)Read Coils(Experimental)\nPlease press 1-2-3:")
try:
with open("{}".format(conffile), 'r') as database:
raw_data = json.load(database)
method = raw_data["method"]
port = raw_data["port"]
timeout = int(raw_data["timeout"])
baudrate = int(raw_data["baudrate"])
stopbits = int(raw_data["stopbits"])
parity = raw_data["parity"]
bytesize = int(raw_data["bytesize"])
number_of_input_registers = int(raw_data["number_of_input_registers"])
number_of_holding_registers = int(raw_data["number_of_holding_registers"])
number_of_coils = int(raw_data["number_of_coils"])
address = raw_data["address"]
database.close()
except:
print("Configuration File Cannot be Found")
try_again = input("Do you want to specify another location? Y/N:")
if(try_again == "y" or try_again == "Y" or try_again == "yes"):
modbus(a,conffile_test,name)
else:
return 0
if (a =="y" or a=="Y" or a=="yes"):
print("\nConfiguration process is starting\nNote that:If you left it empty it will stay as default")
method_1 = input("\nPlease enter the method (ascii or rtu):")
if (method_1 != ""):
method = method_1
raw_data["method"] = method
port_1 = input("Please specify the COM port (COM7):")
if (port_1 != ""):
port=port_1
raw_data["port"] = port
try:
timeout_1 = int(input("Please enter timeout value in decisecond:"))
timeout = timeout_1
raw_data["timeout"] = str(timeout_1)
except:
pass
try:
baudrate_1 = int(input("Please enter the baudrate (19200):"))
baudrate = baudrate_1
raw_data["baudrate"] = str(baudrate_1)
except:
pass
try:
stopbits_1 = int(input("Please enter the number of stopbits (1/2):"))
stopbits = stopbits_1
raw_data["stopbits"] = str(stopbits_1)
except:
pass
parity_1 = input("Please enter the parity (E for even/O for odd/N for none):")
if (parity_1 != ""):
parity_1 = parity_1.lower()
if(parity_1 == "even"):
parity_1 = "E"
elif(parity_1 == "odd"):
parity_1 = "O"
elif(parity_1=="none"):
parity_1 = "N"
elif(parity_1 == "e"):
parity_1="E"
elif(parity_1 == "o"):
parity_1="O"
elif(parity_1=="n"):
parity_1="N"
parity=parity_1
raw_data["parity"] = parity
try:
bytesize_1 = int(input("Please enter the bytesize:"))
raw_data["bytesize"] = bytesize_1
bytesize = str(bytesize_1)
except:
pass
if(read_selector == "1"):
try:
number_of_holding_registers_1 = int(input("Please enter the number of holding registers to read:"))
raw_data["number_of_holding_registers"] = str(number_of_holding_registers_1)
number_of_holding_registers = number_of_holding_registers_1
except:
pass
elif(read_selector == "2"):
try:
number_of_input_registers_1 = int(input("Please enter the number of input registers to read:"))
raw_data["number_of_input_registers"] = str(number_of_input_registers_1)
number_of_input_registers = number_of_input_registers_1
except:
pass
elif(read_selector == "3"):
try:
number_of_coils_1 = int(input("Please enter the number of coils to read:"))
raw_data["number_of_coils"] = str(number_of_coils_1)
number_of_coils = number_of_coils_1
except:
pass
address_1 = input("Please specify the starting address:")
if (address_1 != ""):
address = address_1
raw_data["address"] = address_1
save = input("\nDo you want to save this configuration? Y/N:")
if (save=="Y" or save=="y" or "yes"):
with open("{}.json".format(name), 'w') as file:
json.dump(raw_data, file, indent=2)
file.close()
print("\nCreating the Excel Document")
workbook = xlsxwriter.Workbook('{}.xlsx'.format(name))
worksheet = workbook.add_worksheet()
workbook.close()
address=(int(address,16))
if(read_selector == "1"):
holding_registers(method,port,timeout,baudrate,stopbits,parity,bytesize,number_of_holding_registers,name,address)
elif(read_selector == "2"):
input_registers(method,port,timeout,baudrate,stopbits,parity,bytesize,number_of_input_registers,name,address)
elif(read_selector == "3"):
coils(method,port,timeout,baudrate,stopbits,parity,bytesize,number_of_coils,name,address)
else:
print("\nWrong Function Selection")
def my_macro(counter,rr):
sht = xw.Book.caller().sheets[0]
for i in range (counter):
sht.range('A{}'.format(i+1)).value = rr[i]
def holding_registers(method,port,timeout,baudrate,stopbits,parity,bytesize,number_of_holding_registers,name,address):
i = 0
while True:
try:
if(i==0):
print("\nTrying to Connect")
client = ModbusClient(method=method, port=port, timeout=timeout, baudrate=baudrate, stopbits=stopbits, parity=parity, bytesize=bytesize)
client.connect()
rr = client.read_holding_registers(address, number_of_holding_registers, unit=1)
if(i==0):
print("\nConnection Successful")
print(rr)
rr = rr.registers
except:
print("\nUnable to Connect!")
return 0
try:
if(i==0):
print("\nWriting to Excel")
xw.Book('{}.xlsx'.format(name)).set_mock_caller()
my_macro(number_of_holding_registers,rr)
client.close()
time.sleep(1)
except:
client.close()
return 0
i = i + 1
if(i==1 or i==2):
i=1
def input_registers(method,port,timeout,baudrate,stopbits,parity,bytesize,number_of_input_registers,name,address):
i = 0
while True:
try:
if(i==0):
print("\nTrying to Connect")
client = ModbusClient(method=method, port=port, timeout=timeout, baudrate=baudrate, stopbits=stopbits, parity=parity, bytesize=bytesize)
client.connect()
rr = client.read_input_registers(address, number_of_input_registers, unit=1)
if(i==0):
print("\nConnection Successful")
print(rr)
rr = rr.registers
except:
print("\nUnable to Connect!")
return 0
try:
if(i==0):
print("\nWriting to Excel")
xw.Book('{}.xlsx'.format(name)).set_mock_caller()
my_macro(number_of_input_registers,rr)
client.close()
time.sleep(1)
except:
client.close()
return 0
i = i + 1
if(i==1 or i==2):
i=1
def coils(method,port,timeout,baudrate,stopbits,parity,bytesize,number_of_coils,name,address):
i=0
while True:
try:
if(i==0):
print("\nTrying to Connect")
client = ModbusClient(method=method, port=port, timeout=timeout, baudrate=baudrate, stopbits=stopbits, parity=parity, bytesize=bytesize)
client.connect()
rr = client.read_coils(address, number_of_coils, unit=1)
if(i==0):
print("\nConnection Successful")
print(rr)
rr=rr.registers()
except:
print("\nUnable to Connect!")
return 0
try:
if(i==0):
print("\nWriting to Excel")
xw.Book('{}.xlsx'.format(name)).set_mock_caller()
my_macro(number_of_coils,rr)
client.close()
time.sleep(1)
except:
client.close()
return 0
i = i + 1
if(i==1 or i==2):
i=1
main()