SPL I2C device programming - Read data from the I2C device


This tutorial is continued from the previous tutorial.


5. Define procedure on the emulator's side
  1. "Read" method of I2C device will request to device (or emulator) to read data so you need to define correspond procedure on the emulator.

  2. First, add some logic on the emulator side.

  3. Add below script on the emulator's editor box.

      
    BindI2CComponent  
    	/Id:i2c1
    	/Procedure_OnRead:proc1
    
    procedure proc1
    
    	print "OnReadRequested: " + value.Length + " bytes"
    	
    	buff = Util.CreateArrayByte(2)
    	buff[0] = 0x31
    	buff[1] = 0x32
    
    	i2c1.SetReadBuffer(buff)
    end
    
      


  4. Above procedure will return two byte (0x31, 0x32) to application when it receives "Read" request from application.

  5. Click "Send Script" button to send SPL script to emulator.




6. Read data from the I2C device
  1. You can read data from device by using below method.

       devicename.Read(byte[] buff)
       devicename.Read(byte[] buff, int timeout)

  2. Add below script to read data from the device on the MF Application's editor panel.



      
    rBuff = Util.CreateArrayByte(2)
    rBuff[0] = 0x01
    rBuff[1] = 0x01
    
    i2c1.Read(rBuff)
    
    print "I2C Read Data -> " + Util.BytesToHex(rBuff)
    
      


  3. Click "Send Script" button to send SPL script to emulator.



7. Executed result
  1. Executed result will be shown as follows.