recently am trying to write GUI script which will catch data from QMC5883l digital compass and show the data in the small gui in realtime , the script works fine with no error ,except when the compass disconnected ,an OSError well raise and the program stops , i used try-catch to solve this problem ,so now when the exception raise it well show that the compass disconnected ,however when the compass reconnected it wont work again as like it stock in the oserror exception's code i need to know if there any way to return to try script when the error ends
my code is below
# This Python file uses the following encoding: utf-8
import smbus #import SMBus module of I2C
from time import sleep #import sleep
import math
import sys
import os
from tkinter import *
#some MPU6050 Registers and their Address
Register_mode = 0x09 #Address of mode register
Register_set_reset = 0x0b #Addreas of set/reset period
X_axis_H = 0x01 #Address of X-axis MSB data register
Z_axis_H = 0x03 #Address of Z-axis MSB data register
Y_axis_H = 0x05 #Address of Y-axis MSB data register
declination = 1.466 #define declination angle of location where measurement going to be done
pi = 3.14159265359 #define pi value
def Magnetometer_Init():
#Write to mode R`egister for selecting mode
bus.write_byte_data(Device_Address, Register_mode, 0x1d)
bus.write_byte_data(Device_Address, Register_set_reset , 0x01)
def read_raw_data(addr):
#Read raw 16-bit value
high = bus.read_byte_data(Device_Address, addr)
low = bus.read_byte_data(Device_Address, addr-1)
#concatenate higher and lower value
value = ((high << 8) | low)
#to get signed value from module
if(value > 32768):
value = value - 65536
return value
bus = smbus.SMBus(1) # or bus = smbus.SMBus(0) for older version boards
Device_Address = 0x0d #QMC5883L magnetometer device address
Magnetometer_Init() # initialize qMC5883L magnetometer
print (" Reading Heading Angle")
window = Tk()
window.title("Welcome to LikeGeeks app")
window.geometry('350x200')
lbl = Label(window, text='heading angle')
lbl.grid(column=0, row=0)
def mainloop1():
try:
#Read Accelerometer raw value
x = read_raw_data(X_axis_H)
z = read_raw_data(Z_axis_H)
y = read_raw_data(Y_axis_H)
#Due to declination check for >360 degree
heading = math.atan2(y, x) + declination
if(heading > 2*pi):
heading = heading - 2*pi
#check for sign
if(heading < 0):
heading = heading + 2*pi
#convert into angle
heading_angle = int(heading * 180/pi)
lbl.configure(text=heading_angle)
window.after(1,mainloop1)
except OSError:
lbl.configure(text='compass disconnected')
window.after(0, mainloop1)
window.mainloop()
Since window.after(1, mainloop1)
is called inside the try
block, once there is exception, it will not be executed and mainloop1()
will not be executed again.
To overcome it, simply move window.after(1, mainloop1)
out of the try
block to the end of mainloop1()
as below:
def mainloop1():
try:
...
lbl.configure(text=heading_angle)
except OSError as err:
lbl.configure(text='compass disconnected')
window.after(1, mainloop1)
Then window.after(1, mainloop1)
will be executed even there is exception occurred.