Hi everyone,
I have a script that was working in python 2. I updated my pi and now using python3, I'm getting errors when I try my code.
First error:
invalid syntax
Its showing my last "else:" statement as the issue, I comment it out for now to test and i get
Thanks,
Mark
I have a script that was working in python 2. I updated my pi and now using python3, I'm getting errors when I try my code.
Code:
#!/usr/bin/env python#green/data0 is pin 8#white/data1 is pin 10import timeimport RPi.GPIO as GPIOD0 = 8D1 = 10bits = ''t = 15timeout = tled_buzzer = 18door_strike = 16GPIO.setmode(GPIO.BOARD)GPIO.setup(D0, GPIO.IN, pull_up_down=GPIO.PUD_UP)GPIO.setup(D1,GPIO.IN, pull_up_down=GPIO.PUD_UP)GPIO.setup(led_buzzer, GPIO.OUT)GPIO.setup(door_strike, GPIO.OUT)def set_procname(newname): from ctypes import cdll, byref, create_string_buffer libc = cdll.LoadLibrary('libc.so.6') #Loading a 3rd party library C buff = create_string_buffer(len(newname)+1) #Note: One larger than the name (man prctl says that) buff.value = newname #Null terminated string as it should be libc.prctl(15, byref(buff), 0, 0, 0) #Refer to "#define" of "/usr/include/linux/prctl.h" for the misterious value 16 & arg[3..5] are zero as the man page says. def one(channel): global bits global timeout bits = bits + '1' timeout = t def zero(channel): global bits global timeout bits = bits + '0' timeout = tdef main(): set_procname("Wiegand Reader") global bits global timeout GPIO.add_event_detect(D0, GPIO.FALLING, callback=zero) GPIO.add_event_detect(D1, GPIO.FALLING, callback=one) while 1: if bits: timeout = timeout -1 time.sleep(0.001) if len(bits) > 1 and timeout == 0: print ("Binary:",bits) result = int(str(bits),2) result = int(bits[1:25],2) print("number", result) if result == 2923229214: # the number of my test badge bits = '' print (result) else: bits = '' timeout = t print ("Bad Read") print (result) else: time.sleep(0.001)if __name__ == '__main__': main()invalid syntax
Its showing my last "else:" statement as the issue, I comment it out for now to test and i get
Can someone point me in the right direction?Traceback (most recent call last):
File "/home/admin/rfid/wiegand_main4N.py", line 75, in <module>
main()
File "/home/admin/rfid/wiegand_main4N.py", line 44, in main
set_procname("Wiegand Reader")
File "/home/admin/rfid/wiegand_main4N.py", line 26, in set_procname
buff.value = newname #Null terminated string as it should be
TypeError: bytes expected instead of str instance
Thanks,
Mark
Statistics: Posted by Mark_M — Mon Feb 16, 2026 3:33 am