-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_med_file.py
More file actions
222 lines (174 loc) · 6.29 KB
/
get_med_file.py
File metadata and controls
222 lines (174 loc) · 6.29 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
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
import pylogix
from pylogix.lgx_response import Response
from struct import pack, unpack_from
"""
The goal here is to get the name of the MED file saved on a PanelView's
\\Temp\\~MER.00 directory.
This is accomplished by calling a DLL file that will list all files in the
requested directory. We'll put the results in an output file, request the
results of the file, then delete it.
"""
def pv_test(plc, s, c, data):
conn = plc.conn.connect(False)
if not conn[0]:
return Response(None, None, conn[1])
cip_service = s
cip_service_size = 0x03
cip_class_type = 0x21
cip_class = c
cip_instance_type = 0x24
cip_instance = 0x00
data = [ord(c) for c in data]
request = pack('<BBHHBB{}B'.format(len(data)),
cip_service,
cip_service_size,
cip_class_type,
cip_class,
cip_instance_type,
cip_instance,
*data)
status, ret_data = plc.conn.send(request, False)
return Response(None, None, status)
def get_platform_version(plc):
""" Get the terminal firmware revision
"""
conn = plc.conn.connect(False)
if not conn[0]:
return Response(None, None, conn[1])
cip_service = 0x51
cip_service_size = 0x03
cip_class_type = 0x21
cip_class = 0x04fe
cip_instance_type = 0x24
cip_instance = 0x00
data = "HKEY_LOCAL_MACHINE\\Software\\Rockwell Software\\RSView Enterprise\\MEVersion\0"
data = [ord(c) for c in data]
request = pack('<BBHHBB{}B'.format(len(data)),
cip_service,
cip_service_size,
cip_class_type,
cip_class,
cip_instance_type,
cip_instance,
*data)
status, ret_data = plc.conn.send(request, False)
if status == 0:
value = ret_data[52:-1]
test = [str(chr(v)) for v in value]
version = "".join(test).strip()
version = version.split(".")
version = [int(v) for v in version]
else:
version = [0, 0, 0, 0]
return Response(None, version, status)
def create_file(plc, entry):
"""
Create result file
returns Response class (.TagName, .Value, .Status)
"""
conn = plc.conn.connect(False)
if not conn[0]:
return Response(None, None, conn[1])
cip_service = 0x08
cip_service_size = 0x03
cip_class_type = 0x21
cip_class = 0x04ff
cip_instance_type = 0x24
cip_instance = 0x00
data = [ord(c) for c in entry]
request = pack('<BBHHBBHH{}B'.format(len(data)),
cip_service,
cip_service_size,
cip_class_type,
cip_class,
cip_instance_type,
cip_instance,
0x00, 0x07c2,
*data)
status, ret_data = plc.conn.send(request, False)
return Response(None, None, status)
def delete_file(plc):
"""
Delete result file
returns Response class (.TagName, .Value, .Status)
"""
conn = plc.conn.connect(False)
if not conn[0]:
return Response(None, None, conn[1])
cip_service = 0x09
cip_service_size = 0x03
cip_class_type = 0x21
cip_class = 0x04ff
cip_instance_type = 0x24
cip_instance = 0x01
request = pack('<BBHHBB',
cip_service,
cip_service_size,
cip_class_type,
cip_class,
cip_instance_type,
cip_instance)
status, ret_data = plc.conn.send(request, False)
return Response(None, None, status)
def get_med(plc):
"""
Request the results of the file that was generated
returns Response class (.TagName, .Value, .Status)
"""
conn = plc.conn.connect(False)
if not conn[0]:
return Response(None, None, conn[1])
cip_service = 0x53
cip_service_size = 0x03
cip_class_type = 0x21
cip_class = 0x04ff
cip_instance_type = 0x24
cip_instance = 0x01
request = pack('<BBHHBBI',
cip_service,
cip_service_size,
cip_class_type,
cip_class,
cip_instance_type,
cip_instance,
0x01)
status, ret_data = plc.conn.send(request, False)
if status == 0:
value = ret_data[52:]
byte_count = unpack_from("<H", value, 0)[0]
name_bytes = value[-byte_count:]
count = int(byte_count/2)
stuff = [unpack_from("<H", name_bytes, i*2)[0] for i in range(count)]
file_name = [str(chr(c)) for c in stuff]
file_name = "".join(file_name).strip()
file_name.replace(".med", ".mer")
else:
file_name = ""
return Response(None, file_name, status)
with pylogix.PLC("192.168.1.12") as comm:
# request the version from the registry
response = get_platform_version(comm)
if response.Value[0] == 5:
helper = "\\Storage Card\\Rockwell Software\\RSViewME\\RemoteHelper.DLL\0"
output_location = "\\Storage Card\\Rockwell Software\\RSViewME\\Runtime\\DillyDilly.txt\0"
else:
helper = "\\Windows\\RemoteHelper.DLL\0"
output_location = "\\Application Data\\Rockwell Software\\RSViewME\\Runtime\\DillyDilly.txt\0"
# command to get the file contents of a directory
init_file = helper + "FileBrowse\0\\Temp\\~MER.00\\*.med::" + output_location
# command to delete the file
uninit_file = helper + "DeleteRemFile\0" + output_location
# generate a file with directory information
response = pv_test(comm, 0x50, 0x04fd, init_file)
if response.Status == "Success":
# create file on disk
response = create_file(comm, output_location)
if response.Status == "Success":
# request the contents of the file
response = get_med(comm)
print(response)
# delete file
if response.Status == "Success":
response = delete_file(comm)
if response.Status == "Success":
response = pv_test(comm, 0x50, 0x04fd, uninit_file)