-
Notifications
You must be signed in to change notification settings - Fork 2
/
CatchExceptions3.py
72 lines (61 loc) · 1.67 KB
/
CatchExceptions3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# ar - 18-05-2021
# import os
import time
# import smbus2
from smbus2 import SMBus
# Define I2C bus
DEVICE_BUS = 1
# Define device I2C slave address.
DEVICE_ADDR = 0x17
RA = 0x18
# Raspberry Pi Communicates with MCU via I2C protocol.
# bus = smbus2.SMBus(DEVICE_BUS)
# Write byte to specified I2C register address
# def putByte(RA, byte):
# with SMBus(DEVICE_BUS) as pbus:
# pbus.write_byte_data(DEVICE_ADDR, RA, byte)
#
# def getByte(RA):
# with SMBus(DEVICE_BUS) as gbus:
# byte = gbus.read_byte_data(DEVICE_ADDR, RA)
# return [byte]
def settle():
time.sleep(0.5)
i = 0
while True:
try:
settle()
with SMBus(DEVICE_BUS) as pbus:
pbus.write_byte_data(DEVICE_ADDR, RA, 180)
settle()
with SMBus(DEVICE_BUS) as gbus:
byte = gbus.read_byte_data(DEVICE_ADDR, RA)
settle()
if byte in (180,179,178,177):
pass
print('OK ', i, ' - ', byte)
else:
print('read != write ', i, ' - ', byte, "NOT in (180,179,178,177)")
with SMBus(DEVICE_BUS) as pbus:
pbus.write_byte_data(DEVICE_ADDR, RA, 0)
settle()
with SMBus(DEVICE_BUS) as gbus:
byte = gbus.read_byte_data(DEVICE_ADDR, RA)
settle()
if byte == 0:
pass
print('OK ', i, ' - ', byte)
else:
print('read != write ', i, ' - ', byte, "!= 0")
time.sleep(1)
i += 1
except TimeoutError as e:
print(i, ' - ', byte, ' - ', e)
settle()
except KeyboardInterrupt:
break
except:
pass
# EOF