Skip to content

Commit f027a19

Browse files
Serial transmission improvements
1 parent 236c3cc commit f027a19

File tree

1 file changed

+101
-87
lines changed

1 file changed

+101
-87
lines changed

Diff for: mubootstrap.py

+101-87
Original file line numberDiff line numberDiff line change
@@ -22,42 +22,34 @@ def create_writefile():
2222
' file.write(inp+"\\\\r\\\\n")\\r\\n',
2323
' file.close()\\r\\n']
2424

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="")
3326
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="")
4429
time.sleep(.002)
4530

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=">>> "):
4739
retVal = sendToRepl(ser,replCmd,.0001)
4840
wait_time = time.monotonic()
4941
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:
5143
if time.monotonic() < wait_time:
5244
wait_time = time.monotonic()
5345
if ser.inWaiting():
5446
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:
5749
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
6153
else:
6254
while retVal != replCmd and time.monotonic()-wait_time < 5:
6355
if time.monotonic() < wait_time:
@@ -72,7 +64,7 @@ def sendToRepl(ser,replCmd,delaytime=.01):
7264
wait_time = 5
7365
if delaytime > .0001:
7466
time.sleep(delaytime*5)
75-
waiting = 0
67+
waiting = -1
7668
deltatime = max(delaytime,.001)
7769
while wait_time > 0:
7870
#print(waiting)
@@ -102,75 +94,95 @@ def copyToRemote(hostfilename,microfilename,careful=False):
10294
if microfilename == "" or microfilename == "*":
10395
microfilename = hostfilename
10496
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="")
106101
for line in file:
102+
cleanLine = line.replace('\r','').replace('\n','').replace('\t',' ')
107103
#print(line,end="")
108104
tstline = ""
109-
if line.replace('\r','').replace('\n','') != "":
105+
if cleanLine != "":
110106
if careful:
111-
for lchar in line.replace('\r','').replace('\n',''):
112-
tstline += sendCharToRepl(ser,lchar)
107+
tstline = safeStrToRepl(ser,cleanLine)
113108
else:
114-
tstline = sendToRepl(ser,line.replace('\r','').replace('\n',''))
109+
tstline = sendToRepl(ser,cleanLine)
115110

116-
if (len(tstline) == 0 and len(line.replace('\r','').replace('\n','')) == 0):
111+
if (len(tstline) == 0 and len(cleanLine) == 0):
117112
pass
118113
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):
121116

122117
time.sleep(1)
123118
tstline += ser.read(ser.inWaiting()).decode()
124119

125120
if len(tstline) == 0:
126-
if len(line.replace('\r','').replace('\n','')) != 0:
121+
if len(cleanLine) != 0:
127122
print("****** Transmission Error *******")
128123
print("><")
129-
print(">"+line.replace('\r','').replace('\n','')+"<")
124+
print(">"+cleanLine+"<")
130125
transErr += 1
131-
elif tstline != line.replace('\r','').replace('\n',''):
126+
elif tstline != cleanLine:
132127
if tstline[0] != ".":
133128
print("****** Transmission Error *******")
134129
print(">"+tstline+"<")
135-
print(">"+line.replace('\r','').replace('\n','')+"<")
130+
print(">"+cleanLine+"<")
136131
transErr += 1
137132
else:
138-
if tstline[1:] != line.replace('\r','').replace('\n',''):
133+
if tstline[1:] != cleanLine:
139134
print("****** Transmission Error *******")
140135
print(">"+tstline+"<")
141-
print(">"+line.replace('\r','').replace('\n','')+"<")
136+
print(">"+cleanLine+"<")
142137
transErr += 1
143138

144139
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')
147145
#print("DEBUG:>"+tstline+"<")
148146
kount = 50
147+
padtstline = False
148+
if tstline == "":
149+
tstline = " "
150+
padtstline = True
149151
while tstline[-1] != "." and kount>0:
150152
kount -= 1
151153
#print("@",end="")
152154
tstline += ser.read(ser.inWaiting()).decode()
155+
if padtstline:
156+
tstline = tstline[1:]
153157
print(tstline.replace('\r\n','\n'),end="")
154158

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="")
156164
file.close()
157165
return transErr
158166

159167
def print_directory(path, remote=False, tabs=0):
160168
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")
162172
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(",")
164174
except:
165175
dirlist = []
166176
else:
167177
dirlist = os.listdir(path)
168178

169179
for file in sorted(dirlist,key=str.lower):
170180
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")
172184
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(',')))
174186
except:
175187
stats = [0,0,0,0,0,0,0]
176188
else:
@@ -200,43 +212,47 @@ def print_directory(path, remote=False, tabs=0):
200212
ser = reset_serial(None)
201213
print("Serial port reset")
202214
if ser.inWaiting():
203-
print(ser.read(ser.inWaiting()).decode())
215+
print(ser.read(ser.inWaiting()).decode(),end="")
204216

205217
print("Attempting to get board attention")
206-
print(sendToRepl(ser,"\x02"))
218+
print(sendToRepl(ser,"\x02"),end="")
207219
try:
208-
print(sendToRepl(ser," "))
220+
print(sendToRepl(ser," "),end="")
209221
except:
210-
print(sendToRepl(ser,"\x04"))
222+
print(sendToRepl(ser,"\x04"),end="")
211223
if sendToRepl(ser,"\r\n") == "":
212224
ser = reset_serial(ser)
213225
time.sleep(5)
214-
print(sendToRepl(ser,"\r\n"))
226+
print(sendToRepl(ser,"\r\n"),end="")
215227
if sendToRepl(ser,"\r\n") == "":
216-
print(sendToRepl(ser,"\x04"))
228+
print(sendToRepl(ser,"\x04"),end="")
217229
time.sleep(5)
218230

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="")
222234

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")
226238
if microfiles.find('writefile.py') == -1:
227239
create_writefile()
228-
print(sendToRepl(ser,"import writefile\r\n",.2))
240+
print(safeStrToRepl(ser,"import writefile\r"),end="")
229241

230242
inp = "*"
231243
hostfilename = ""
232244
microfilename = ""
233-
while inp[0].upper() != "Q":
245+
while inp.upper() != "Q":
234246
localdir = os.getcwd()
235-
sendToRepl(ser,"\r\n")
247+
sendCharToRepl(ser,"\r")
248+
safeStrToRepl(ser,"os.getcwd()")
249+
remotedir = sendCharToRepl(ser,"\r")
236250
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]
238253
except:
239254
remotedir = '/'
255+
240256
print()
241257
print("Local Dir: ",localdir," Remote (micro) Dir: ",remotedir)
242258
print("Host file: ",hostfilename," Remote (micro) file: ",microfilename)
@@ -249,8 +265,8 @@ def print_directory(path, remote=False, tabs=0):
249265
elif inp.upper() == "NDIR":
250266
ndir = input("Enter new folder to create on Microcontroller in "+remotedir+" ($ to abort): ")
251267
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")
254270
elif inp.upper() == "LDIR":
255271
print_directory(localdir)
256272
elif inp.upper() == "RDIR":
@@ -261,7 +277,7 @@ def print_directory(path, remote=False, tabs=0):
261277
except:
262278
print("Error setting requested default directory")
263279
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")
265281
elif inp.upper() == "RDEL":
266282
fndel = input("Enter filename/directory to delete ($ to abort): ")
267283
if fndel != "$":
@@ -270,36 +286,36 @@ def print_directory(path, remote=False, tabs=0):
270286
while ans.upper() not in ["Y","N"]:
271287
ans = input("Delete all files in folder - Are you sure? (Y/N): ")
272288
if ans.upper() == "Y":
273-
dirlisttxt = sendToRepl(ser,"os.listdir()\r\n",.5)
289+
dirlisttxt = safeStrToRepl(ser,"os.listdir()\r")
274290
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(",")
276292
except:
293+
print("*** Error processing directory listing ***")
277294
dirlist = []
278295
for file in dirlist:
279-
stattxt = sendToRepl(ser,"os.stat('"+file+"')\r\n",.1)
296+
stattxt = safeStrToRepl(ser,"os.stat('"+file+"')\r")
280297
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"))
282302
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+" ***")
288304
else:
289-
stattxt = sendToRepl(ser,"os.stat('"+fndel+"')\r\n",.1)
305+
stattxt = safeStrToRepl(ser,"os.stat('"+fndel+"')\r")
290306
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"))
292313
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+" ***")
299315
elif inp.upper() in ["COPY","CCOPY"]:
300316
tErr = 0
301317
os.chdir(localdir)
302-
sendToRepl(ser,"os.chdir('"+remotedir+"')\r\n")
318+
safeStrToRepl(ser,"os.chdir('"+remotedir+"')\r")
303319
if hostfilename[0] == "*":
304320
if hostfilename[1] == "." and len(hostfilename) > 2:
305321
filterExt = hostfilename[1:]
@@ -320,7 +336,7 @@ def print_directory(path, remote=False, tabs=0):
320336
print("Transmission Errors: ",tErr)
321337

322338

323-
elif inp[0] == "?":
339+
elif inp == "?":
324340
print("HFILE = Name of the file on the Host computer")
325341
print(" limited wildcards can be used as source")
326342
print(" wildcards must have a defined file extension")
@@ -341,5 +357,3 @@ def print_directory(path, remote=False, tabs=0):
341357

342358
elif inp.upper() == "Q":
343359
ser.close()
344-
345-

0 commit comments

Comments
 (0)