@@ -22,42 +22,34 @@ def create_writefile():
22
22
' file.write(inp+"\\ \\ r\\ \\ n")\\ r\\ n' ,
23
23
' file.close()\\ r\\ n' ]
24
24
25
- #print(sendToRepl(ser,"f = open('writefile.py','w')\r\n"))
26
- #for codeline in writefile:
27
- # print(sendToRepl(ser,"f.write('"+codeline+"')\r\n"))
28
- #print(sendToRepl(ser,"f.close()\r\n"))
29
-
30
- for lchar in "f = open('writefile.py','w')" :
31
- print (sendCharToRepl (ser ,lchar ),end = "" )
32
- print (sendCharToRepl (ser ,"\r " ))
25
+ print (safeStrToRepl (ser ,"f = open('writefile.py','w')\r " ),end = "" )
33
26
for codeline in writefile :
34
- for lchar in "f.write('" :
35
- print (sendCharToRepl (ser ,lchar ),end = "" )
36
- for lchar in codeline :
37
- print (sendCharToRepl (ser ,lchar ),end = "" )
38
- for lchar in "')" :
39
- print (sendCharToRepl (ser ,lchar ),end = "" )
40
- print (sendCharToRepl (ser ,"\r " ))
41
- for lchar in "f.close()" :
42
- print (sendCharToRepl (ser ,lchar ),end = "" )
43
- print (sendCharToRepl (ser ,"\r " ))
27
+ print (safeStrToRepl (ser ,"f.write('" + codeline + "')\r " ),end = "" )
28
+ print (safeStrToRepl (ser ,"f.close()\r " ),end = "" )
44
29
time .sleep (.002 )
45
30
46
- def sendCharToRepl (ser ,replCmd ):
31
+ def safeStrToRepl (ser ,replCmd ,prmpt = ">>> " ):
32
+ retVal = ""
33
+ for lchar in replCmd :
34
+ retVal += sendCharToRepl (ser ,lchar ,prmpt )
35
+
36
+ return retVal
37
+
38
+ def sendCharToRepl (ser ,replCmd ,prmpt = ">>> " ):
47
39
retVal = sendToRepl (ser ,replCmd ,.0001 )
48
40
wait_time = time .monotonic ()
49
41
if replCmd == '\r ' :
50
- while retVal != '\r \n >>> ' and time .monotonic ()- wait_time < 5 :
42
+ while retVal != '\r \n ' + prmpt and time .monotonic ()- wait_time < 5 :
51
43
if time .monotonic () < wait_time :
52
44
wait_time = time .monotonic ()
53
45
if ser .inWaiting ():
54
46
retVal += ser .read (ser .inWaiting ()).decode ()
55
- if len (retVal ) >= 4 :
56
- if retVal [- 4 :] == '>>> ' :
47
+ if len (retVal ) >= len ( prmpt ) :
48
+ if retVal [- len ( prmpt ) :] == prmpt :
57
49
break
58
- if len (retVal ) >= 6 :
59
- if retVal [- 6 :] == '\r \n >>> ' :
60
- retVal = retVal [:- 6 ]+ '\n >>> '
50
+ if len (retVal ) >= len ( prmpt ) + 2 :
51
+ if retVal [- ( len ( prmpt ) + 2 ) :] == '\r \n ' + prmpt :
52
+ retVal = retVal [:- ( len ( prmpt ) + 2 ) ]+ '\n ' + prmpt
61
53
else :
62
54
while retVal != replCmd and time .monotonic ()- wait_time < 5 :
63
55
if time .monotonic () < wait_time :
@@ -72,7 +64,7 @@ def sendToRepl(ser,replCmd,delaytime=.01):
72
64
wait_time = 5
73
65
if delaytime > .0001 :
74
66
time .sleep (delaytime * 5 )
75
- waiting = 0
67
+ waiting = - 1
76
68
deltatime = max (delaytime ,.001 )
77
69
while wait_time > 0 :
78
70
#print(waiting)
@@ -102,75 +94,95 @@ def copyToRemote(hostfilename,microfilename,careful=False):
102
94
if microfilename == "" or microfilename == "*" :
103
95
microfilename = hostfilename
104
96
file = open (hostfilename )
105
- sendToRepl (ser ,"writefile.wf('" + microfilename + "')\r \n " )
97
+ if careful :
98
+ print (safeStrToRepl (ser ,"writefile.wf('" + microfilename + "')\r " ,"." ),end = "" )
99
+ else :
100
+ print (sendToRepl (ser ,"writefile.wf('" + microfilename + "')\r \n " ),end = "" )
106
101
for line in file :
102
+ cleanLine = line .replace ('\r ' ,'' ).replace ('\n ' ,'' ).replace ('\t ' ,' ' )
107
103
#print(line,end="")
108
104
tstline = ""
109
- if line . replace ( ' \r ' , '' ). replace ( ' \n ' , '' ) != "" :
105
+ if cleanLine != "" :
110
106
if careful :
111
- for lchar in line .replace ('\r ' ,'' ).replace ('\n ' ,'' ):
112
- tstline += sendCharToRepl (ser ,lchar )
107
+ tstline = safeStrToRepl (ser ,cleanLine )
113
108
else :
114
- tstline = sendToRepl (ser ,line . replace ( ' \r ' , '' ). replace ( ' \n ' , '' ) )
109
+ tstline = sendToRepl (ser ,cleanLine )
115
110
116
- if (len (tstline ) == 0 and len (line . replace ( ' \r ' , '' ). replace ( ' \n ' , '' ) ) == 0 ):
111
+ if (len (tstline ) == 0 and len (cleanLine ) == 0 ):
117
112
pass
118
113
elif len (tstline ) == 0 or \
119
- (tstline [0 ] != "." and tstline != line . replace ( ' \r ' , '' ). replace ( ' \n ' , '' ) ) or \
120
- (tstline [0 ] == "." and tstline [1 :] != line . replace ( ' \r ' , '' ). replace ( ' \n ' , '' ) ):
114
+ (tstline [0 ] != "." and tstline != cleanLine ) or \
115
+ (tstline [0 ] == "." and tstline [1 :] != cleanLine ):
121
116
122
117
time .sleep (1 )
123
118
tstline += ser .read (ser .inWaiting ()).decode ()
124
119
125
120
if len (tstline ) == 0 :
126
- if len (line . replace ( ' \r ' , '' ). replace ( ' \n ' , '' ) ) != 0 :
121
+ if len (cleanLine ) != 0 :
127
122
print ("****** Transmission Error *******" )
128
123
print ("><" )
129
- print (">" + line . replace ( ' \r ' , '' ). replace ( ' \n ' , '' ) + "<" )
124
+ print (">" + cleanLine + "<" )
130
125
transErr += 1
131
- elif tstline != line . replace ( ' \r ' , '' ). replace ( ' \n ' , '' ) :
126
+ elif tstline != cleanLine :
132
127
if tstline [0 ] != "." :
133
128
print ("****** Transmission Error *******" )
134
129
print (">" + tstline + "<" )
135
- print (">" + line . replace ( ' \r ' , '' ). replace ( ' \n ' , '' ) + "<" )
130
+ print (">" + cleanLine + "<" )
136
131
transErr += 1
137
132
else :
138
- if tstline [1 :] != line . replace ( ' \r ' , '' ). replace ( ' \n ' , '' ) :
133
+ if tstline [1 :] != cleanLine :
139
134
print ("****** Transmission Error *******" )
140
135
print (">" + tstline + "<" )
141
- print (">" + line . replace ( ' \r ' , '' ). replace ( ' \n ' , '' ) + "<" )
136
+ print (">" + cleanLine + "<" )
142
137
transErr += 1
143
138
144
139
print (tstline ,end = "" )
145
- #print(sendToRepl(ser,'\r\n').replace('\r\n','\n'),end="")
146
- tstline = sendToRepl (ser ,'\r \n ' )
140
+ if careful :
141
+ tstline = sendCharToRepl (ser ,"\r " ,"." )
142
+ else :
143
+ #print(sendToRepl(ser,'\r\n').replace('\r\n','\n'),end="")
144
+ tstline = sendToRepl (ser ,'\r \n ' )
147
145
#print("DEBUG:>"+tstline+"<")
148
146
kount = 50
147
+ padtstline = False
148
+ if tstline == "" :
149
+ tstline = " "
150
+ padtstline = True
149
151
while tstline [- 1 ] != "." and kount > 0 :
150
152
kount -= 1
151
153
#print("@",end="")
152
154
tstline += ser .read (ser .inWaiting ()).decode ()
155
+ if padtstline :
156
+ tstline = tstline [1 :]
153
157
print (tstline .replace ('\r \n ' ,'\n ' ),end = "" )
154
158
155
- print (sendToRepl (ser ,"*\r \n " ))
159
+ if careful :
160
+ print (sendCharToRepl (ser ,"*" ),end = "" )
161
+ print (sendCharToRepl (ser ,"\r " ))
162
+ else :
163
+ print (sendToRepl (ser ,"*\r \n " ),end = "" )
156
164
file .close ()
157
165
return transErr
158
166
159
167
def print_directory (path , remote = False , tabs = 0 ):
160
168
if remote :
161
- dirlisttxt = sendToRepl (ser ,"os.listdir()\r \n " )
169
+ safeStrToRepl (ser ,"os.listdir()" )
170
+ dirlisttxt = sendCharToRepl (ser ,"\r " )
171
+ #dirlisttxt = sendToRepl(ser,"os.listdir()\r\n")
162
172
try :
163
- dirlist = (dirlisttxt .split ('\r \ n ' )[1 :- 1 ][0 ])[1 :- 1 ].replace ("'" ,"" ).replace (" " ,"" ).split ("," )
173
+ dirlist = (dirlisttxt .split ('\n ' )[1 :- 1 ][0 ])[1 :- 1 ].replace ("'" ,"" ).replace (" " ,"" ).split ("," )
164
174
except :
165
175
dirlist = []
166
176
else :
167
177
dirlist = os .listdir (path )
168
178
169
179
for file in sorted (dirlist ,key = str .lower ):
170
180
if remote :
171
- stattxt = sendToRepl (ser ,"os.stat('" + file + "')\r \n " )
181
+ safeStrToRepl (ser ,"os.stat('" + file + "')" )
182
+ stattxt = sendCharToRepl (ser ,"\r " )
183
+ #stattxt = sendToRepl(ser,"os.stat('"+file+"')\r\n")
172
184
try :
173
- stats = list (map (int ,(stattxt .split ('\r \ n ' )[1 :- 1 ][0 ]).replace ('(' ,'' ).replace (')' ,'' ).split (',' )))
185
+ stats = list (map (int ,(stattxt .split ('\n ' )[1 :- 1 ][0 ]).replace ('(' ,'' ).replace (')' ,'' ).split (',' )))
174
186
except :
175
187
stats = [0 ,0 ,0 ,0 ,0 ,0 ,0 ]
176
188
else :
@@ -200,43 +212,47 @@ def print_directory(path, remote=False, tabs=0):
200
212
ser = reset_serial (None )
201
213
print ("Serial port reset" )
202
214
if ser .inWaiting ():
203
- print (ser .read (ser .inWaiting ()).decode ())
215
+ print (ser .read (ser .inWaiting ()).decode (), end = "" )
204
216
205
217
print ("Attempting to get board attention" )
206
- print (sendToRepl (ser ,"\x02 " ))
218
+ print (sendToRepl (ser ,"\x02 " ), end = "" )
207
219
try :
208
- print (sendToRepl (ser ," " ))
220
+ print (sendToRepl (ser ," " ), end = "" )
209
221
except :
210
- print (sendToRepl (ser ,"\x04 " ))
222
+ print (sendToRepl (ser ,"\x04 " ), end = "" )
211
223
if sendToRepl (ser ,"\r \n " ) == "" :
212
224
ser = reset_serial (ser )
213
225
time .sleep (5 )
214
- print (sendToRepl (ser ,"\r \n " ))
226
+ print (sendToRepl (ser ,"\r \n " ), end = "" )
215
227
if sendToRepl (ser ,"\r \n " ) == "" :
216
- print (sendToRepl (ser ,"\x04 " ))
228
+ print (sendToRepl (ser ,"\x04 " ), end = "" )
217
229
time .sleep (5 )
218
230
219
- print (sendToRepl (ser ,"\x03 " ))
220
- print (sendToRepl (ser ,"\r \n " ))
221
- print (sendToRepl (ser ,"\r \n " ))
231
+ print (sendToRepl (ser ,"\x03 " ), end = "" )
232
+ print (sendToRepl (ser ,"\r \n " ), end = "" )
233
+ print (sendToRepl (ser ,"\r \n " ), end = "" )
222
234
223
- print (sendToRepl (ser ,"import os\r \n " , .2 ) )
224
- print (sendToRepl (ser ,"os.chdir('/')\r \n " , .2 ) )
225
- microfiles = sendToRepl (ser ,"os.listdir()\r \n " , .2 )
235
+ print (safeStrToRepl (ser ,"import os\r " ), end = "" )
236
+ print (safeStrToRepl (ser ,"os.chdir('/')\r " ), end = "" )
237
+ microfiles = safeStrToRepl (ser ,"os.listdir()\r " )
226
238
if microfiles .find ('writefile.py' ) == - 1 :
227
239
create_writefile ()
228
- print (sendToRepl (ser ,"import writefile\r \n " , .2 ) )
240
+ print (safeStrToRepl (ser ,"import writefile\r " ), end = "" )
229
241
230
242
inp = "*"
231
243
hostfilename = ""
232
244
microfilename = ""
233
- while inp [ 0 ] .upper () != "Q" :
245
+ while inp .upper () != "Q" :
234
246
localdir = os .getcwd ()
235
- sendToRepl (ser ,"\r \n " )
247
+ sendCharToRepl (ser ,"\r " )
248
+ safeStrToRepl (ser ,"os.getcwd()" )
249
+ remotedir = sendCharToRepl (ser ,"\r " )
236
250
try :
237
- remotedir = sendToRepl (ser ,"os.getcwd()\r \n " ,.1 ).split ("\r \n " )[1 ][1 :- 1 ]
251
+ #remotedir = sendToRepl(ser,"os.getcwd()\r\n",.1).split("\r\n")[1][1:-1]
252
+ remotedir = remotedir .split ("\n " )[1 ][1 :- 1 ]
238
253
except :
239
254
remotedir = '/'
255
+
240
256
print ()
241
257
print ("Local Dir: " ,localdir ," Remote (micro) Dir: " ,remotedir )
242
258
print ("Host file: " ,hostfilename ," Remote (micro) file: " ,microfilename )
@@ -249,8 +265,8 @@ def print_directory(path, remote=False, tabs=0):
249
265
elif inp .upper () == "NDIR" :
250
266
ndir = input ("Enter new folder to create on Microcontroller in " + remotedir + " ($ to abort): " )
251
267
if ndir != "$" :
252
- print (sendToRepl (ser ,"os.mkdir('" + ndir + "')\r \n " ))
253
- sendToRepl (ser ,"os.chdir('" + ndir + "')\r \n " )
268
+ print (safeStrToRepl (ser ,"os.mkdir('" + ndir + "')\r " ))
269
+ safeStrToRepl (ser ,"os.chdir('" + ndir + "')\r " )
254
270
elif inp .upper () == "LDIR" :
255
271
print_directory (localdir )
256
272
elif inp .upper () == "RDIR" :
@@ -261,7 +277,7 @@ def print_directory(path, remote=False, tabs=0):
261
277
except :
262
278
print ("Error setting requested default directory" )
263
279
elif inp .upper () == "RCD" :
264
- sendToRepl (ser ,"os.chdir('" + input ("Enter destination directory: " )+ "')\r \n " )
280
+ safeStrToRepl (ser ,"os.chdir('" + input ("Enter destination directory: " )+ "')\r " )
265
281
elif inp .upper () == "RDEL" :
266
282
fndel = input ("Enter filename/directory to delete ($ to abort): " )
267
283
if fndel != "$" :
@@ -270,36 +286,36 @@ def print_directory(path, remote=False, tabs=0):
270
286
while ans .upper () not in ["Y" ,"N" ]:
271
287
ans = input ("Delete all files in folder - Are you sure? (Y/N): " )
272
288
if ans .upper () == "Y" :
273
- dirlisttxt = sendToRepl (ser ,"os.listdir()\r \n " , .5 )
289
+ dirlisttxt = safeStrToRepl (ser ,"os.listdir()\r " )
274
290
try :
275
- dirlist = (dirlisttxt .split ('\r \ n ' )[1 :- 1 ][0 ])[1 :- 1 ].replace ("'" ,"" ).replace (" " ,"" ).split ("," )
291
+ dirlist = (dirlisttxt .split ('\n ' )[1 :- 1 ][0 ])[1 :- 1 ].replace ("'" ,"" ).replace (" " ,"" ).split ("," )
276
292
except :
293
+ print ("*** Error processing directory listing ***" )
277
294
dirlist = []
278
295
for file in dirlist :
279
- stattxt = sendToRepl (ser ,"os.stat('" + file + "')\r \n " , .1 )
296
+ stattxt = safeStrToRepl (ser ,"os.stat('" + file + "')\r " )
280
297
try :
281
- stats = list (map (int ,(stattxt .split ('\r \n ' )[1 :- 1 ][0 ]).replace ('(' ,'' ).replace (')' ,'' ).split (',' )))
298
+ stats = list (map (int ,(stattxt .split ('\n ' )[1 :- 1 ][0 ]).replace ('(' ,'' ).replace (')' ,'' ).split (',' )))
299
+ isdir = stats [0 ] & 0x4000
300
+ if not isdir :
301
+ print (safeStrToRepl (ser ,"os.remove('" + file + "')\r " ))
282
302
except :
283
- stats = [0x4000 ]
284
-
285
- isdir = stats [0 ] & 0x4000
286
- if not isdir :
287
- print (sendToRepl (ser ,"os.remove('" + file + "')\r \n " ))
303
+ print ("*** Can't determine file type for " + file + " ***" )
288
304
else :
289
- stattxt = sendToRepl (ser ,"os.stat('" + fndel + "')\r \n " , .1 )
305
+ stattxt = safeStrToRepl (ser ,"os.stat('" + fndel + "')\r " )
290
306
try :
291
- stats = list (map (int ,(stattxt .split ('\r \n ' )[1 :- 1 ][0 ]).replace ('(' ,'' ).replace (')' ,'' ).split (',' )))
307
+ stats = list (map (int ,(stattxt .split ('\n ' )[1 :- 1 ][0 ]).replace ('(' ,'' ).replace (')' ,'' ).split (',' )))
308
+ isdir = stats [0 ] & 0x4000
309
+ if isdir :
310
+ print (safeStrToRepl (ser ,"os.rmdir('" + fndel + "')\r " ))
311
+ else :
312
+ print (safeStrToRepl (ser ,"os.remove('" + fndel + "')\r " ))
292
313
except :
293
- stats = [0x4000 ]
294
- isdir = stats [0 ] & 0x4000
295
- if isdir :
296
- print (sendToRepl (ser ,"os.rmdir('" + fndel + "')\r \n " ))
297
- else :
298
- print (sendToRepl (ser ,"os.remove('" + fndel + "')\r \n " ))
314
+ print ("*** Can't determine file type for " + fndel + " ***" )
299
315
elif inp .upper () in ["COPY" ,"CCOPY" ]:
300
316
tErr = 0
301
317
os .chdir (localdir )
302
- sendToRepl (ser ,"os.chdir('" + remotedir + "')\r \n " )
318
+ safeStrToRepl (ser ,"os.chdir('" + remotedir + "')\r " )
303
319
if hostfilename [0 ] == "*" :
304
320
if hostfilename [1 ] == "." and len (hostfilename ) > 2 :
305
321
filterExt = hostfilename [1 :]
@@ -320,7 +336,7 @@ def print_directory(path, remote=False, tabs=0):
320
336
print ("Transmission Errors: " ,tErr )
321
337
322
338
323
- elif inp [ 0 ] == "?" :
339
+ elif inp == "?" :
324
340
print ("HFILE = Name of the file on the Host computer" )
325
341
print (" limited wildcards can be used as source" )
326
342
print (" wildcards must have a defined file extension" )
@@ -341,5 +357,3 @@ def print_directory(path, remote=False, tabs=0):
341
357
342
358
elif inp .upper () == "Q" :
343
359
ser .close ()
344
-
345
-
0 commit comments