-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathReenigne.py
282 lines (266 loc) · 8.21 KB
/
Reenigne.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
# Reenigne.py (c) Max Zuidberg MPL2.0
# Automatic reverse engineer (got it?) tool for nextion displays
# Adjust the following settings as you need and then run the script. no CLI.
import serial
import time
import struct
ser = serial.Serial()
# Adjust as needed
ser.port = "COM9"
ser.baudrate = 115200
ser.open()
# timeout in seconds
timeout = 2
# by default reading text from the screen is disabled because it's not possible
# to always read a string correctly with unknown hmi applications.
readText = False
# List of attributes that return a string (f.ex. .txt or file path attributes)
attTxt = ["txt"]
# Components and what to read from them
components = {
121: {
"name": "Page",
"attributes": ["vscope", "sta"],
},
52: {
"name": "Variable",
"attributes": ["vscope", "sta", "val", "txt_maxl", "txt"],
},
54: {
"name": "Number",
"attributes": ["vscope", "x", "y", "w", "h", "val"],
},
59: {
"name": "XFloat",
"attributes": ["vscope", "x", "y", "w", "h", "val"],
},
116: {
"name": "Text",
"attributes": ["vscope", "x", "y", "w", "h", "txt_maxl", "txt"],
},
55: {
"name": "Scrolling Text",
"attributes": ["vscope", "x", "y", "w", "h", "txt_maxl", "txt"],
},
112: {
"name": "Picture",
"attributes": ["vscope", "x", "y", "w", "h", "pic"],
},
113: {
"name": "Crop Picture",
"attributes": ["vscope", "x", "y", "w", "h", "pic"],
},
58: {
"name": "QR Code",
"attributes": ["vscope", "x", "y", "w", "h", "txt_maxl", "txt"],
},
106: {
"name": "Progress Bar",
"attributes": ["vscope", "x", "y", "w", "h", "val"],
},
122: {
"name": "Gauge",
"attributes": ["vscope", "x", "y", "w", "h", "val"],
},
0: {
"name": "Waveform",
"attributes": ["vscope", "x", "y", "w", "h"],
},
1: {
"name": "Slider",
"attributes": ["vscope", "x", "y", "w", "h", "minval", "maxval", "val"],
},
98: {
"name": "Button",
"attributes": ["vscope", "x", "y", "w", "h", "sta", "txt_maxl", "txt", "val"],
},
53: {
"name": "Dual-state Button",
"attributes": ["vscope", "x", "y", "w", "h", "sta", "txt_maxl", "txt", "val"],
},
56: {
"name": "Checkbox",
"attributes": ["vscope", "x", "y", "w", "h", "val"],
},
57: {
"name": "Radio",
"attributes": ["vscope", "x", "y", "w", "h", "val"],
},
67: {
"name": "Switch",
"attributes": ["vscope", "x", "y", "w", "h", "val"],
},
61: {
"name": "Combo Box",
"attributes": ["vscope", "x", "y", "w", "h", "val"],
},
68: {
"name": "Text Select",
"attributes": ["vscope", "x", "y", "w", "h"],
},
62: {
"name": "SLText",
"attributes": ["vscope", "x", "y", "w", "h"],
},
4: {
"name": "Audio",
"attributes": ["vscope", "x", "y", "w", "h"],
},
60: {
"name": "External Picture",
"attributes": ["vscope", "x", "y", "w", "h"],
},
2: {
"name": "Gmov",
"attributes": ["vscope", "x", "y", "w", "h"],
},
3: {
"name": "Video",
"attributes": ["vscope", "x", "y", "w", "h"],
},
66: {
"name": "Data Record",
"attributes": ["vscope", "x", "y", "w", "h"],
},
63: {
"name": "File Stream",
"attributes": ["vscope", "x", "y", "w", "h"],
},
65: {
"name": "File Browser",
"attributes": ["vscope", "x", "y", "w", "h"],
},
109: {
"name": "Hotspot",
"attributes": ["vscope", "x", "y", "w", "h"],
},
51: {
"name": "Timer",
"attributes": ["vscope", "tim", "en"],
},
-1: {
"name": "Unknown",
"attributes": ["vscope", ],
},
}
# Helper functions to interface with nextion
def acknowledge(code=0x01, timeout=2):
termination = 0
response = -1
t = time.time()
while termination < 3:
if time.time() - t > timeout:
return False
if ser.in_waiting:
temp = ser.read(1)[0]
if temp == 0xff:
termination += 1
else:
response = temp
return (response == code)
def sendCmd(cmd, ack=True, timeout=2):
if isinstance(cmd, str):
cmd = cmd.encode("ascii")
elif not isinstance(cmd, bytes):
cmd = bytes(cmd)
ser.reset_input_buffer()
ser.write(cmd)
ser.write(bytes(3 * [0xff]))
if ack:
return acknowledge(timeout=timeout)
else:
return True
def getVal(var:str, ext=".val", timeout=2):
ser.reset_input_buffer()
if ext and ext[0] != ".":
ext = "." + ext
sendCmd(("get " + var + ext).encode("ascii"), ack=False, timeout=timeout)
data = b""
done = False
t = time.time()
while not done:
if time.time() - t > timeout:
return None
if ser.in_waiting:
data += ser.read(1)
if len(data) > 8:
data = data[1:]
if len(data) == 8:
if data[0] == 0x71 and data[-1] == 0xff and data[-2] == 0xff and data[-3] == 0xff:
done = True
return struct.unpack_from("<I", data, 1)[0]
def getTxt(var:str, ext=".txt", encoding="utf-8", timeout=2):
ser.reset_input_buffer()
if ext and ext[0] != ".":
ext = "." + ext
sendCmd(("get " + var + ext).encode("ascii"), ack=False, timeout=timeout)
data = b""
start = False
end = 0
done = False
t = time.time()
while not done:
if time.time() - t > timeout:
return None
if ser.in_waiting:
if not start:
data = ser.read(1)
if data[0] == 0x70:
start = True
data = b""
else:
data += ser.read(1)
if data[-1] == 0xff:
end += 1
if end == 3:
done = True
data = data[:-1]
return data.decode(encoding=encoding)
def getType(id:int, timeout=2):
ser.reset_input_buffer()
sendCmd("prints b[{}].type,1".format(id), ack=False)
t = time.time()
while time.time() - t < timeout:
if ser.in_waiting:
return ser.read(1)[0]
break
return None
# Just in case, exit reparse mode
sendCmd("DRAKJHSUYDGBNCJHGJKSHBDN")
# Enable acknowledge on Nextion
if not sendCmd("bkcmd=3", timeout=timeout):
raise Exception("Could not enable acknowledge on nextion. ")
# Count number of pages and objects
for page in range(0,255):
if not sendCmd("page {}".format(page), timeout=timeout):
break
print("Page {: >3}:".format(page))
for component in range(1,255):
# check if we're still in range
if component != getVal("b[{}].id".format(component), ext="", timeout=timeout):
break
type = getType(component, timeout=timeout)
if type is None:
break
typeStr = "Unknown"
typeSafe = -1
if type in components:
typeStr = components[type]["name"]
typeSafe = type
print(4 * " " + "Component {: >3}: ".format(component))
padding = max([len("type")] + [len(e) for e in components[typeSafe]["attributes"]]) + 1
print(8 * " " + "{} {} ({})".format("Type:".ljust(padding), type, typeStr))
for att in components[typeSafe]["attributes"]:
val = None
if att in attTxt:
if readText:
val = getTxt("b[{}].{}".format(component, att), ext="", timeout=timeout)
if val:
val = "\"" + val + "\""
else:
val = getVal("b[{}].{}".format(component, att), ext="", timeout=timeout)
if val is None:
continue
print(8 * " " + "{} {}".format((att + ":").ljust(padding), val))
ser.close()
print("\nDone.")