Please post formated code, not pictures of code. See Wiki - how to post code on this forum.
Tip: when posting images crop them. 75% of your screenshot is whitespace.
def loadRecipe(recipe):
# Setup the command
cmd = 'AE LoadSetup,' + recipe
# Send the load request
log.debugf('Issue recipe load for %s', recipe)
resp = project.rasco.serial.sendCmd(cmd)
return resp
def sendCmd(command):
# Open Serial
try:
open()
except:
log.warnf('Unable to open RS232 connection on %s', port)
return False
command = start + command + end
system.serial.write(port,hello)
log.trace('Wrote hello to RS232')
try:
x = system.serial.readUntil(port,ready,False)
log.trace('Received ready from RS232')
system.serial.write(port,command)
log.tracef('Wrote %s to RS232', command)
x = system.serial.readUntil(port,hello,False)
log.trace('Received hello from RS232')
system.serial.write(port,ready)
log.trace('Wrote ready to RS232')
resp = system.serial.readUntil(port,end,False)
resp = "".join([i for i in resp if ord(i) in range(32, 127)])
except:
log.warnf('Failed to execute %s to RS232', command)
close()
return False
log.debugf('Executed %s to RS232', command)
# Close serial
close()
return resp
{ Please don't start new topics with the same question. }
Consider removing (at least temporarily) those try
/ except
clauses to see what it is that's breaking. If the resulting exception isn't obvious, post it here, and perhaps we can interpret it for you.
A few things about this code though:
- it uses variables that are not defined there (
start
,end
...).
It doesn't mean it will not work, they might be globals. It means you need to be extra careful with them. - did you define the
close
andopen
functions ?
I'd advise not to do that, as they're built-in python functions. Try not to overwrite built-in stuff. - I'd also suggest using
close
in afinally
clause, if there's no context manager foropen
. - It seems you're returning a string in case of success, and
False
in case of error.
I suggest you avoid mixing types, and returnNone
instead in case of error.
Or, if the calling function needs to know an error happened... let it handle the exceptions.
1 Like