def read(source, count): with i2c.I2CMaster() as bus: print("reading %d bytes from %02x" % (count, source)) result = bus.transaction( i2c.writing_bytes(I2C_SLAVE_ADDRESS, source), i2c.reading(I2C_SLAVE_ADDRESS, count)) return result[0]
def handle_exception(self): ''' 1 - Unknown Command 2 - Invalid Session ID 3 - Invalid CRC 4 - Invalid Length 5 - Invalid Address 6 - Invalid Function 8 - Content Mixmatch 12- Wrong Param ''' while GPIO.input(SW_XFER_PIN): pass if not GPIO.input(SW_XFER_PIN): GPIO.setup(SW_XFER_PIN, GPIO.OUT, initial=GPIO.LOW) try: data = self.i2c.transaction(i2c.reading(SW_ADDR, 132)) return data[0] except IOError: return [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] GPIO.setup(SW_XFER_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def handle_exception(self): ''' 1 - Unknown Command 2 - Invalid Session ID 3 - Invalid CRC 4 - Invalid Length 5 - Invalid Address 6 - Invalid Function 8 - Content Mixmatch 12- Wrong Param ''' while GPIO.input(SW_XFER_PIN): pass if not GPIO.input(SW_XFER_PIN): GPIO.setup(SW_XFER_PIN, GPIO.OUT, initial=GPIO.LOW) try: data = self.i2c.transaction(i2c.reading(SW_ADDR, 132)) return data[0] except IOError: return [0,0,0,0,0,0,0,0,0,0] GPIO.setup(SW_XFER_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def i2c_read(len): data = i2cm.transaction(i2c.reading(SW_ADDR, len)) return data[0]
import time address = 0x28 # 0x28 = bin0101000 = dec40 # DF command = 7 bit slave address + 0 = 0x28 = dec40 # MR command = 7 bit slave address + 1 = 0x29 = dec41 with i2c.I2CMaster() as bus: print ("send MR command") bus.transaction(i2c.writing_bytes(address, 0x29, 0x00)) time.sleep(0.2) print ("send DF command and read 4 bytes") read_b1 = bus.transaction( i2c.writing_bytes(address, 0x28), i2c.reading(address, 1)) print ("Bye 1: %d" % read_b1[0][0]) read_b2 = bus.transaction( i2c.writing_bytes(address, 0x28), i2c.reading(address, 2)) print ("Bye 2: %d" % read_b2[0][1]) read_b3 = bus.transaction( i2c.writing_bytes(address, 0x28), i2c.reading(address, 3)) print ("Bye 3: %d" % read_b3[0][2]) read_b4 = bus.transaction( i2c.writing_bytes(address, 0x28), i2c.reading(address, 4)) print ("Bye 4: %d" % read_b4[0][3]) uh = (read_b1[0][0] << 8) | (read_b2[0][1]) uh = uh & 0x3FFF