Python Forum
Python code with serial port and global undefined
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python code with serial port and global undefined
#1
I have the following code and I got this error after adding a few ser.close() lines (identified with *** down below):

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:]
Reply


Messages In This Thread
Python code with serial port and global undefined - by marciokoko - Jan-16-2017, 12:43 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Waiting for input from serial port, then move on KenHorse 3 1,457 Apr-17-2024, 07:21 AM
Last Post: DeaD_EyE
  MCU reboots after opening Serial port when ran from Raspberry PI zazas321 3 616 Mar-19-2024, 09:02 AM
Last Post: zazas321
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 5,635 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  Python error on mentioned Arduino port name dghosal 5 984 Aug-22-2023, 04:54 PM
Last Post: deanhystad
  Why Pip is not listed in the official Global Python Module index? quazirfan 2 832 Apr-21-2023, 10:55 AM
Last Post: snippsat
  Serial Port As Global Prasanjith 2 1,676 Mar-23-2023, 08:54 PM
Last Post: deanhystad
  Python Serial: How to read the complete line to insert to MySQL? sylar 1 920 Mar-21-2023, 10:06 PM
Last Post: deanhystad
  undefined function error JonWayn 5 1,581 Sep-11-2022, 03:38 AM
Last Post: JonWayn
  Undefined Led_Zeppelin 4 1,530 Aug-02-2022, 11:57 AM
Last Post: buran
  python serial port barryjo 2 1,758 Dec-27-2021, 11:09 PM
Last Post: barryjo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020