3
3
def reset_serial (ser ):
4
4
print ("Resetting Serial Port..." )
5
5
try :
6
+ ser .flush ()
6
7
ser .close ()
7
8
except :
8
9
pass
9
- #return serial.Serial('com20',9600,timeout=10,write_timeout=15)
10
- return serial .Serial ('com33' ,115200 ,timeout = 10 ,write_timeout = 15 )
10
+ return serial .Serial ('com52' ,9600 ,timeout = 10 ,write_timeout = 15 )
11
+ #return serial.Serial('com68',115200,timeout=10,write_timeout=15)
12
+ #return serial.Serial('com84',1200,timeout=10,write_timeout=15)
11
13
12
14
def create_writefile ():
13
15
writefile = [
14
- 'def wf(filename):\\ r\\ n' ,
16
+ 'def wf(filename,binary="" ):\\ r\\ n' ,
15
17
' if filename == "":\\ r\\ n' ,
16
18
' filename = input("Filename?: ")\\ r\\ n' ,
17
- ' file = open(filename,"w")\\ r\\ n' ,
19
+ ' file = open(filename,"w"+binary )\\ r\\ n' ,
18
20
' inp = ""\\ r\\ n' ,
19
21
' while inp != "*":\\ r\\ n' ,
20
22
' inp = input(".")\\ r\\ n' ,
21
- ' if inp != "*":\\ r\\ n' ,
23
+ ' if binary and inp != "*":\\ r\\ n' ,
24
+ ' inch = inp.split(",")\\ r\\ n' ,
25
+ ' for ch in inch:\\ r\\ n' ,
26
+ ' #file.write(bytes(chr(int(ch)),"utf-8"))\\ r\\ n' ,
27
+ ' file.write(int(ch).to_bytes(1,"big"))\\ r\\ n' ,
28
+ ' elif inp != "*":\\ r\\ n' ,
22
29
' file.write(inp+"\\ \\ r\\ \\ n")\\ r\\ n' ,
23
30
' file.close()\\ r\\ n' ]
24
31
@@ -60,7 +67,12 @@ def sendCharToRepl(ser,replCmd,prmpt=">>> "):
60
67
return retVal
61
68
62
69
def sendToRepl (ser ,replCmd ,delaytime = .01 ):
63
- ser .write (replCmd .encode ())
70
+ try :
71
+ ser .write (replCmd .encode ())
72
+ except :
73
+ print ("Error Writing to Serial" )
74
+ return - 1
75
+
64
76
wait_time = 5
65
77
if delaytime > .0001 :
66
78
time .sleep (delaytime * 5 )
@@ -76,33 +88,63 @@ def sendToRepl(ser,replCmd,delaytime=.01):
76
88
waiting = ser .inWaiting ()
77
89
78
90
if ser .inWaiting () and delaytime < .001 :
79
- return ser .read (ser .inWaiting ()).decode ()
91
+ try :
92
+ decodedRead = ser .read (ser .inWaiting ()).decode ()
93
+ except :
94
+ decodedRead = ""
95
+
96
+ return decodedRead
80
97
elif ser .inWaiting ():
81
98
retVal = ""
82
99
while ser .inWaiting ():
83
100
#print("@",end="")
84
- retVal += ser .read (ser .inWaiting ()).decode ()
101
+ try :
102
+ retVal += ser .read (ser .inWaiting ()).decode ()
103
+ except :
104
+ pass
85
105
#time.sleep(delaytime)
86
106
87
107
#retVal = ser.read(ser.inWaiting()).decode()
88
108
return retVal
89
109
else :
90
110
return ""
111
+
112
+ def multicpy (srcdir ,tardir ):
113
+ curDLst = os .listdir (srcdir )
114
+
115
+ print (safeStrToRepl (ser ,"os.mkdir('" + tardir + "')\r " ))
116
+
117
+ for _dir in curDLst :
118
+ if os .stat (srcdir + '\\ ' + _dir )[0 ] & (2 ** 15 ) != 0 :
119
+ copyToRemote (srcdir + '\\ ' + _dir ,tardir + '/' + _dir ,True ,True )
120
+ else :
121
+ multicpy (srcdir + '\\ ' + _dir ,tardir + '/' + _dir )
91
122
92
- def copyToRemote (hostfilename ,microfilename ,careful = False ):
123
+ def copyToRemote (hostfilename ,microfilename ,careful = False , binary = False ):
93
124
transErr = 0
94
125
if microfilename == "" or microfilename == "*" :
95
126
microfilename = hostfilename
96
- file = open (hostfilename )
97
- if careful :
127
+ if binary :
128
+ file = open (hostfilename ,"rb" )
129
+ else :
130
+ file = open (hostfilename )
131
+ if binary :
132
+ print (safeStrToRepl (ser ,"writefile.wf('" + microfilename + "','b')\r " ,"." ),end = "" )
133
+ elif careful :
98
134
print (safeStrToRepl (ser ,"writefile.wf('" + microfilename + "')\r " ,"." ),end = "" )
99
135
else :
100
136
print (sendToRepl (ser ,"writefile.wf('" + microfilename + "')\r \n " ),end = "" )
101
137
for line in file :
102
- cleanLine = line .replace ('\r ' ,'' ).replace ('\n ' ,'' ).replace ('\t ' ,' ' )
103
138
#print(line,end="")
104
139
tstline = ""
105
- if cleanLine != "" :
140
+ cleanLine = ""
141
+ if binary and line != b"" :
142
+ for i in line :
143
+ cleanLine += str (i )+ ","
144
+ cleanLine = cleanLine [:- 1 ]
145
+ tstline = safeStrToRepl (ser ,cleanLine )
146
+ elif not binary and line .replace ('\r ' ,'' ).replace ('\n ' ,'' ) != "" :
147
+ cleanLine = line .replace ('\r ' ,'' ).replace ('\n ' ,'' ).replace ('\t ' ,' ' )
106
148
if careful :
107
149
tstline = safeStrToRepl (ser ,cleanLine )
108
150
else :
@@ -136,7 +178,8 @@ def copyToRemote(hostfilename,microfilename,careful=False):
136
178
print (">" + cleanLine + "<" )
137
179
transErr += 1
138
180
139
- print (tstline ,end = "" )
181
+ #print(tstline,end="")
182
+ print ("#" ,end = "" )
140
183
if careful :
141
184
tstline = sendCharToRepl (ser ,"\r " ,"." )
142
185
else :
@@ -154,11 +197,13 @@ def copyToRemote(hostfilename,microfilename,careful=False):
154
197
tstline += ser .read (ser .inWaiting ()).decode ()
155
198
if padtstline :
156
199
tstline = tstline [1 :]
157
- print (tstline .replace ('\r \n ' ,'\n ' ),end = "" )
200
+ # print(tstline.replace('\r\n','\n'),end="")
158
201
159
202
if careful :
160
- print (sendCharToRepl (ser ,"*" ),end = "" )
161
- print (sendCharToRepl (ser ,"\r " ))
203
+ #print(sendCharToRepl(ser,"*"),end="")
204
+ #print(sendCharToRepl(ser,"\r"))
205
+ sendCharToRepl (ser ,"*" )
206
+ sendCharToRepl (ser ,"\r " )
162
207
else :
163
208
print (sendToRepl (ser ,"*\r \n " ),end = "" )
164
209
file .close ()
@@ -215,25 +260,58 @@ def print_directory(path, remote=False, tabs=0):
215
260
print (ser .read (ser .inWaiting ()).decode (),end = "" )
216
261
217
262
print ("Attempting to get board attention" )
218
- print (sendToRepl (ser ,"\x02 " ),end = "" )
219
- try :
220
- print (sendToRepl (ser ," " ),end = "" )
221
- except :
222
- print (sendToRepl (ser ,"\x04 " ),end = "" )
223
- if sendToRepl (ser ,"\r \n " ) == "" :
224
- ser = reset_serial (ser )
225
- time .sleep (5 )
226
- print (sendToRepl (ser ,"\r \n " ),end = "" )
227
- if sendToRepl (ser ,"\r \n " ) == "" :
228
- print (sendToRepl (ser ,"\x04 " ),end = "" )
229
- time .sleep (5 )
230
-
231
- print (sendToRepl (ser ,"\x03 " ),end = "" )
232
- print (sendToRepl (ser ,"\r \n " ),end = "" )
233
- print (sendToRepl (ser ,"\r \n " ),end = "" )
263
+ tmp = sendToRepl (ser ,"\r \n " )
264
+ if ">>>" not in str (tmp ):
265
+ print ('1 ' ,tmp ,end = "" )
266
+ if tmp == - 1 :
267
+ ser = reset_serial (ser )
268
+ elif tmp == "" :
269
+ print ('^D' ,sendToRepl (ser ,b"\x04 " ),end = "" )
270
+ time .sleep (3 )
271
+ tmp = sendToRepl (ser ," " )
272
+ print ('2 ' ,tmp ,end = "" )
273
+ if tmp == - 1 :
274
+ ser = reset_serial (ser )
275
+ time .sleep (1 )
276
+ tmp = sendToRepl (ser ,"\x03 " )
277
+ print ('^C' ,tmp ,end = "" )
278
+ if ">>>" not in str (tmp ):
279
+ if tmp == "" :
280
+ print ('^?' ,sendToRepl (ser ,"\x02 " ),end = "" )
281
+ print ('3 ' ,sendToRepl (ser ,"\r \n " ),end = "" )
282
+ time .sleep (3 )
283
+ print ('4 ' ,sendToRepl (ser ,"\r \n " ),end = "" )
284
+ time .sleep (1 )
285
+ tmp = sendToRepl (ser ," " )
286
+ print ('5b' ,tmp ,end = "" )
287
+ if tmp == - 1 :
288
+ ser = reset_serial (ser )
289
+ elif tmp == "" :
290
+ print ('^D' ,sendToRepl (ser ,"\x04 " ),end = "" )
291
+
292
+ tmp = sendToRepl (ser ,"\r \n " )
293
+ print ('6 ' ,tmp ,end = "" )
294
+ if ">>>" not in str (tmp ):
295
+ time .sleep (3 )
296
+ print ('7 ' ,sendToRepl (ser ,"\r \n " ),end = "" )
297
+ time .sleep (1 )
298
+ if sendToRepl (ser ,"\r \n " ) == "" :
299
+ ser = reset_serial (ser )
300
+ time .sleep (5 )
301
+ print ('8 ' ,sendToRepl (ser ,"\r \n " ),end = "" )
302
+ if sendToRepl (ser ,"\r \n " ) == "" :
303
+ print ('^D' ,sendToRepl (ser ,"\x04 " ),end = "" )
304
+ time .sleep (5 )
305
+
306
+ print ('^C' ,sendToRepl (ser ,"\x03 " ),end = "" )
307
+ time .sleep (1 )
308
+ print ('9 ' ,sendToRepl (ser ,"\r \n " ),end = "" )
309
+ print ('10' ,sendToRepl (ser ,"\r \n " ),end = "" )
234
310
235
311
print (safeStrToRepl (ser ,"import os\r " ),end = "" )
236
312
print (safeStrToRepl (ser ,"os.chdir('/')\r " ),end = "" )
313
+ print (safeStrToRepl (ser ,"import supervisor\r " ),end = "" )
314
+ print (safeStrToRepl (ser ,"supervisor.runtime.autoreload=False\r " ),end = "" )
237
315
microfiles = safeStrToRepl (ser ,"os.listdir()\r " )
238
316
if microfiles .find ('writefile.py' ) == - 1 :
239
317
create_writefile ()
@@ -255,14 +333,14 @@ def print_directory(path, remote=False, tabs=0):
255
333
256
334
print ()
257
335
print ("Local Dir: " ,localdir ," Remote (micro) Dir: " ,remotedir )
258
- print ("Host file: " ,hostfilename ," Remote (micro) file: " ,microfilename )
336
+ print ("Local file: " ,hostfilename ," Remote (micro) file: " ,microfilename )
259
337
inp = input ("Enter Command (? - Help, q - Quit): " )
260
338
261
- if inp .upper () == "HFILE " :
339
+ if inp .upper () == "LFILE " :
262
340
hostfilename = input ("Enter the name of the file on the Host computer: " )
263
341
elif inp .upper () == "RFILE" :
264
342
microfilename = input ("Enter the name of the file to be created on the remote microcontroller: " )
265
- elif inp .upper () == "NDIR " :
343
+ elif inp .upper () == "RMD " :
266
344
ndir = input ("Enter new folder to create on Microcontroller in " + remotedir + " ($ to abort): " )
267
345
if ndir != "$" :
268
346
print (safeStrToRepl (ser ,"os.mkdir('" + ndir + "')\r " ))
@@ -312,7 +390,17 @@ def print_directory(path, remote=False, tabs=0):
312
390
print (safeStrToRepl (ser ,"os.remove('" + fndel + "')\r " ))
313
391
except :
314
392
print ("*** Can't determine file type for " + fndel + " ***" )
315
- elif inp .upper () in ["COPY" ,"CCOPY" ]:
393
+ elif inp .upper () == "CFOLDER" :
394
+ print ("Copy all files from HOST:" + localdir + " and any subfolders to Microcontroller REMOTE:" + remotedir )
395
+ print ("This will overwrite any files with the same filename on the Microcontroller" )
396
+ if input ("Continue? (Y/N): " ).upper () == "Y" :
397
+ multicpy (localdir ,remotedir )
398
+ elif inp .upper () in ["COPY" ,"CCOPY" ,"BCOPY" ]:
399
+ if inp .upper () == "BCOPY" :
400
+ inp = "CCOPY"
401
+ binflag = True
402
+ else :
403
+ binflag = False
316
404
tErr = 0
317
405
os .chdir (localdir )
318
406
safeStrToRepl (ser ,"os.chdir('" + remotedir + "')\r " )
@@ -322,22 +410,22 @@ def print_directory(path, remote=False, tabs=0):
322
410
for filename in os .listdir ():
323
411
if filename [- (len (filterExt )):] == filterExt :
324
412
if inp .upper () == "CCOPY" :
325
- tErr += copyToRemote (filename ,filename ,True )
413
+ tErr += copyToRemote (filename ,filename ,True , binflag )
326
414
else :
327
415
tErr += copyToRemote (filename ,filename )
328
416
print ("Transmission Errors: " ,tErr )
329
417
else :
330
418
print ("*ERROR* Wildcard by file extension only (ie *.py)" )
331
419
else :
332
420
if inp .upper () == "CCOPY" :
333
- tErr = copyToRemote (hostfilename ,microfilename ,True )
421
+ tErr = copyToRemote (hostfilename ,microfilename ,True , binflag )
334
422
else :
335
423
tErr = copyToRemote (hostfilename ,microfilename )
336
424
print ("Transmission Errors: " ,tErr )
337
425
338
426
339
427
elif inp == "?" :
340
- print ("HFILE = Name of the file on the Host computer" )
428
+ print ("LFILE = Name of the file on the Host computer" )
341
429
print (" limited wildcards can be used as source" )
342
430
print (" wildcards must have a defined file extension" )
343
431
print (" i.e. *.py, *.txt, ..." )
@@ -348,12 +436,15 @@ def print_directory(path, remote=False, tabs=0):
348
436
print (" When changing directories, relative directory paths work" )
349
437
print (" i.e. '..' up one, '../..' up 2" )
350
438
print (" paths that don't start with a '/','\\ ' will be from the current path" )
351
- print ("NDIR = Create a new directory on the Microcontroller" )
439
+ print ("RMD = Create a new directory on the Microcontroller" )
352
440
print ("LDIR = Display files in local source directory" )
353
441
print ("RDIR = Display files in remote (Microcontroller) destination directory" )
354
442
print ("RDEL = Delete a file/directory from Microcontroller" )
355
443
print ("COPY = Copy the current file from the local directory to the remote directory" )
356
444
print ("CCOPY= Character (careful) verion of copy routine" )
445
+ print ("BCOPY= Binary (careful only) version of copy routine" )
446
+ print ("CFOLDER=Recursivley copy contents of current local source directory to remote directory" )
357
447
358
448
elif inp .upper () == "Q" :
449
+ ser .flush ()
359
450
ser .close ()
0 commit comments