简体   繁体   中英

Python I2C LCD exception

I want to do a program which uses I2C LCD display if possible or cli output if not.

Everything is fine when I start the program on Raspberry Pi where LCD is connected. The problem starts when I move the program to my laptop where there aro no GPIO, no I2C and therefore no LCD at all.

I want to make proper "if" but I couldn't find any proper solution.

Line of code which makes problems on my Laptop:

    def __init__(self, addr, port=1):
    self.addr = addr
    self.bus = smbus.SMBus(port)

Traceback (most recent call last):
  File "./lcd", line 164, in <module>
    lcd=lcd()
  File "./lcd", line 114, in __init__
    self.lcd_device = i2c_device(ADDRESS)
  File "./lcd", line 25, in __init__
    self.bus = smbus.SMBus(port)
IOError: [Errno 13] Permission denied

Any idea how to handle that "if" checking if I can use LCD or not? I use Ubuntu 18 if it changes anything.

Thx.

Look up "python try except" - you can catch that error and do whatever you want instead of crashing.

I have changed the code to try/catch instead of my typical if/else php approach.

If anyone knows how to do that with if/else it would be appreciated for the future.

Thank you Blorgbeard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM