Jan-16-2017, 12:43 AM
(This post was last modified: Jan-17-2017, 01:08 AM by marciokoko.)
I have the following code and I got this error after adding a few ser.close() lines (identified with *** down below):
Could it be because after the serial is opened in the self.write() method, execution is sent to the self.read() method which at its end closes the serial with a ser.close() which I just added, and returns execution to the two self.read() calls in self.write()? Because I didnt get this error before. But then again I wasnt really calling self.read() either.
Quote:File "tsrb430.py", line 162, in read
responseBits = convert_hex_to_bin_str (x)
NameError: global name 'convert_hex_to_bin_str' is not defined
Could it be because after the serial is opened in the self.write() method, execution is sent to the self.read() method which at its end closes the serial with a ser.close() which I just added, and returns execution to the two self.read() calls in self.write()? Because I didnt get this error before. But then again I wasnt really calling self.read() either.
def relayToggle(self):
timestring = AIHome.results['Relay1ON']
print timestring
hours,minutes = timestring.split(":")
print hours
print minutes
print(datetime.datetime.now())
ref_time = datetime.datetime.combine(datetime.datetime.now(), datetime.time(int(hours), int(minutes)))
if ref_time > datetime.datetime.now():
print("ref_time>relay should be OFF")
self.write(1,0)
else:
print("now>relay should be ON")
self.write(1,1)
def write(self, relay, state):
ser = serial.Serial(
port='/dev/serial0',
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
print "Serial is open: " + str(ser.isOpen())
print "Now Writing"
print relay
print state
self.read()***
while True:
# Relay1ON 1
if relay == 1 & state == 0:
ser.write("o")
print ('Relay toggled off')
break
# case 2
if relay == 1 & state == 1:
ser.write("e")
print ('Relay toggled on')
break
# else case 3
print ('something else')
ser.write("o")
x = ser.readline()
print "Something else '" + x + "'"
break
print "Did write, now read"
x = ser.readline()
print "Writing & Reading got '" + x + "'"
self.read()***
ser.close()
def read(self):
ser = serial.Serial(
port='/dev/serial0',
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
print "Read:Serial is open: " + str(ser.isOpen())
print "Read:Now Reading"
ser.write("[")
print "Did read"
x = ser.readline()
print "Read:Reading got '" + x + "'"
responseBits = convert_hex_to_bin_str (x)
responseBits = responseBits.zfill(4)
responseBits = list(responseBits)
responseBits.reverse()
relayStates = {}
relay = 1
for bit in responseBits:
relayStates[relay] = int(bit)
relay += 1
self.relayStatesD = relayStates
#report states to someone?
print relayStatesD
ser.close()***
def convert_hex_to_int(hexChars):
try:
ints = [ord(char) for char in hexChars]
return ints
except TypeError:
pass
return []
def convert_hex_to_bin_str(hexChars):
response = convert_hex_to_int(hexChars)[0]
responseBinary = bin(response)
return responseBinary[2:]
