-
Notifications
You must be signed in to change notification settings - Fork 0
/
stm32.py
400 lines (325 loc) · 13.6 KB
/
stm32.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import SER
import USB0
import time
class STM32():
def __init__(self, bd='115200', sconf='8N1', usb_log=False):
self.__bd = bd
self.__sconf = sconf
self.__usb_log = usb_log
SER.set_speed(self.__bd, self.__sconf)
def __SER_receive(self, timeout):
try:
res = ''
start = time.time()
while (time.time() - start < timeout):
res = res + SER.read()
return res
except Exception as e:
USB0.send('FATAL ERROR: stm32.__SER_receive(): {}\r\n'.format(e))
def __parseRS485Read(self, resp):
try:
if resp.find('OK') != -1:
temp = resp.find(',')
x = resp[temp + 1:]
y = ''
idx = 0
for ch in x:
if ch != ',':
y += ch
idx += 1
else:
break
bytes_len = int(y)
x = resp[temp + 1 + idx + 1:]
y = ''
xc = 0
while xc < bytes_len:
y += x[xc]
xc += 1
bytes_val = y
x = resp[temp + 1 + idx + 1 + xc + 1:]
y = ''
for ch in x:
if ch != ',':
y += ch
else:
break
crc = y
return (1, bytes_len, bytes_val, crc)
else:
return (-1,)
except Exception as e:
if self.__usb_log: USB0.send('FATAL ERROR: {}\r\n'.format(e))
def __parseRS485Count(self, resp):
try:
if resp.find('OK') != -1:
temp = resp.find(',')
x = resp[temp + 1:]
y = ''
idx = 0
for ch in x:
if ch != ',':
y += ch
idx += 1
else:
break
bytes_count = int(y)
return (1, bytes_count)
else:
return (-1,)
except Exception as e:
if self.__usb_log: USB0.send('FATAL ERROR: {}\r\n'.format(e))
def __parseRS485Flush(self, resp):
try:
if resp.find('OK') != -1:
temp = resp.find(',')
x = resp[temp + 1:]
y = ''
idx = 0
for ch in x:
if ch != ',':
y += ch
idx += 1
else:
break
bytes_flushed = int(y)
return (1, bytes_flushed)
else:
return (-1,)
except Exception as e:
if self.__usb_log: USB0.send('FATAL ERROR: {}\r\n'.format(e))
def __parseRS485Send(self, resp):
try:
if resp.find('OK') != -1:
temp = resp.find(',')
x = resp[temp + 1:]
y = ''
idx = 0
for ch in x:
if ch != ',':
y += ch
idx += 1
else:
break
byte_size = int(y)
return (1, byte_size)
else:
return (-1,)
except Exception as e:
if self.__usb_log: USB0.send('FATAL ERROR: {}\r\n'.format(e))
def __sendRS485Read(self, size, timeout=1):
try:
cmd = 'AT+RS485=READ,{}\r'.format(size)
SER.send(cmd, 0)
res = self.__SER_receive(timeout)
return self.__parseRS485Read(res)
except Exception as e:
if self.__usb_log: USB0.send('FATAL ERROR: {}\r\n'.format(e))
def __sendRS485Send(self, len, val, crc=0, timeout=1):
try:
cmd = 'AT+RS485=SEND,{},{},{}\r'.format(len, val, crc)
SER.send(cmd, 0)
res = self.__SER_receive(timeout)
return self.__parseRS485Send(res)
except Exception as e:
if self.__usb_log: USB0.send('FATAL ERROR: {}\r\n'.format(e))
def __sendRS485Count(self, timeout=1):
try:
cmd = 'AT+RS485=COUNT\r'
SER.send(cmd, 0)
res = self.__SER_receive(timeout)
return self.__parseRS485Count(res)
except Exception as e:
if self.__usb_log: USB0.send('FATAL ERROR: {}\r\n'.format(e))
def __sendRS485Flush(self, size=6000, timeout=1):
try:
cmd = 'AT+RS485=FLUSH,{}\r'.format(size)
SER.send(cmd, 0)
res = self.__SER_receive(timeout)
return self.__parseRS485Flush(res)
except Exception as e:
if self.__usb_log: USB0.send('FATAL ERROR: {}\r\n'.format(e))
def flush_rx_buffer(self, f_size=6000, res_timeout=0.5, max_tries=5, t_timeout=0.5):
'''Function flushes n bytes in RS485 RX buffer
:param f_size (int): number of bytes to flush
:param res_timeout (float): response timeout
:param max_tries (int): maximum tries number
:param t_timeout (float): timeout between tries
:param usb_log (boolean): log to USB interface
:return (tuple):
'''
res_c = 0
tries = 1
try:
while (res_c < 1) and (tries <= max_tries):
res = self.__sendRS485Flush(f_size, res_timeout)
res_c = res[0]
if res[0] != -1:
if self.__usb_log: USB0.send('FLUSH OK: {} tries: {}\r\n'.format(res, tries))
return res + (tries,) # -> tuple(1, message, tries)
break
else:
time.sleep(t_timeout)
tries += 1
continue
else:
if self.__usb_log: USB0.send('FLUSH ERROR: max {} tries were made\r\n'.format(tries))
return (-1, tries) # -> tuple(-1, tries)
except Exception as e:
if self.__usb_log: USB0.send('FATAL ERROR flush_rx_buffer(): {}\r\n'.format(e))
def count_rx_buffer(self, res_timeout=0.5, max_tries=5, t_timeout=0.5, usb_log=True):
'''Function returns number of bytes in RS485 RX buffer
:param res_timeout (float): response timeout
:param max_tries (int): maxium tries number
:param t_timeout (float): timeout between tries
:param usb_log (boolean): log to USB interface
:return (tuple):
'''
res_c = 0
tries = 1
try:
while (res_c < 1) and (tries <= max_tries):
res = self.__sendRS485Count(res_timeout)
res_c = res[0]
if res[0] != -1:
if self.__usb_log: USB0.send('COUNT OK: {} tries: {}\r\n'.format(res, tries))
return res + (tries,) # -> tuple(1, count, tries)
break
else:
time.sleep(t_timeout)
tries += 1
continue
else:
if self.__usb_log: USB0.send('COUNT ERROR: max {} tries were made\r\n'.format(tries))
return(-1, tries) # -> tuple(-1, tries)
except Exception as e:
if self.__usb_log: USB0.send('FATAL ERROR count_rx_buffer(): {}\r\n'.format(e))
def send_totx_buffer(self, value, len, crc='0', res_timeout=0.5, max_tries=5, t_timeout=0.5, usb_log=True):
'''Function returns number of bytes in RS485 RX buffer
:param value (str): value to send in string format (bytes converted to string format)
:param value (int): number of chars in value string
:param crc (str): crc of value in string format (bytes converted to string format)
:param res_timeout (float): response timeout
:param max_tries (int): maxium tries number
:param t_timeout (float): timeout between tries
:param usb_log (boolean): log to USB interface
:return (tuple):
'''
res_c = 0
tries = 1
try:
while (res_c < 1) and (tries <= max_tries):
res = self.__sendRS485Send(len, value, crc, res_timeout)
res_c = res[0]
if res[0] != -1:
if self.__usb_log: USB0.send('SEND OK: {} tries: {}\r\n'.format(res, tries))
return res + (tries,) # -> tuple(1, message, tries)
break
else:
time.sleep(t_timeout)
tries += 1
continue
else:
if self.__usb_log: USB0.send('SEND ERROR: max {} tries were made\r\n'.format(tries))
return(-1, tries) # -> tuple(-1, tries)
except Exception as e:
if self.__usb_log: USB0.send('FATAL ERROR send_totx_buffer(): {}\r\n'.format(e))
def read_rx_buffer(self, size, res_timeout=0.5, max_tries=5, t_timeout=0.5, usb_log=True):
'''Function reads number of bytes from RS485 RX buffer
:param size (int): number of bytes to read
:param res_timeout (float): response timeout
:param max_tries (int): maxium tries number
:param t_timeout (float): timeout between tries
:param usb_log (boolean): log to USB interface
:return (tuple):
'''
res_c = 0
tries = 1
try:
while (res_c < 1) and (tries <= max_tries):
res = self.__sendRS485Read(size, res_timeout)
res_c = res[0]
if res[0] != -1:
if self.__usb_log: USB0.send('READ OK: {} tries: {}\r\n'.format(res, tries))
return res + (tries,) # -> tuple(1, message, tries)
break
else:
time.sleep(t_timeout)
tries += 1
continue
else:
if self.__usb_log: USB0.send('READ ERROR: max {} tries were made\r\n'.format(tries))
return(-1, tries) # -> tuple(-1, tries)
except Exception as e:
if self.__usb_log: USB0.send('FATAL ERROR read_rx_buffer(): {}\r\n'.format(e))
def set_led_toggle(self, color, intensity, time_on, time_off, timeout=1):
try:
cmd = 'AT+LED={},TOGGLE,{},{},{}\r'.format(color, intensity, time_on, time_off)
SER.send(cmd, 0)
res = self.__SER_receive(timeout)
if res.find('OK') != -1:
return (1,)
else:
return (0,)
except Exception as e:
if self.__usb_log: USB0.send('FATAL ERROR set_led_toggle(): {}\r\n'.format(e))
def set_led(self, color, state, intensity, timeout=1):
try:
cmd = 'AT+LED={},{},{}\r'.format(color, state, intensity)
SER.send(cmd, 0)
res = self.__SER_receive(timeout)
if res.find('OK') != -1:
return (1,)
else:
return (0,)
except Exception as e:
if self.__usb_log: USB0.send('FATAL ERROR: set_led(): {}\r\n'.format(e))
def set_wdg(self, interval, timeout=1):
'''Function sets internal STM32 watchdog
:param interval (int): value in seconds from 30 to 64800 (18 hours). Value -1 or 0 turns off the watchdog.
:param res_timeout (float): resposne timeout
:return (tuple):
'''
try:
cmd = 'AT+WDG=SET,{}\r'.format(interval)
SER.send(cmd, 0)
res = self.__SER_receive(timeout)
if res.find('OK') != -1:
return (1,)
else:
return (0,)
except Exception as e:
if self.__usb_log: USB0.send('FATAL ERROR: set_wdgt(): {}\r\n'.format(e))
def reset_wdg(self, timeout=1):
'''Function resets STM32 watchdog timer
:return (tuple):
'''
try:
cmd = 'AT+WDG\r'
SER.send(cmd, 0)
res = self.__SER_receive(timeout)
if res.find('OK') != -1:
return (1,)
else:
return (0,)
except Exception as e:
if self.__usb_log: USB0.send('FATAL ERROR: reset_wdg(): {}\r\n'.format(e))
def test(self, tries=10):
USB0.send('STM32 Library Test START\r\n')
x = 0
while x < tries:
res = self.flush_rx_buffer()
USB0.send('{}\r\n'.format(res))
res = self.send_totx_buffer('01030000000305CB',16)
USB0.send('{}\r\n'.format(res))
res = self.count_rx_buffer()
USB0.send('{}\r\n'.format(res))
count = int(res[1])
res = self.read_rx_buffer(count)
USB0.send('{}\r\n'.format(res))
x += 1
time.sleep(2)
USB0.send('STM32 Library Test STOP\r\n')
if __name__ == "__main__":
stm = STM32()
stm.test()