| |
I2C device programming - Read data from I2C device
1. Define procedure on the emulator's side
- "Read" method of I2C device will request to device (or emulator) to read data so you need to define correspond procedure on the emulator.
- First, add some logic on the emulator side.
- Select "Emulator" tab.
- Add below script on the 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
| |
|
|
|
|
- Above procedure will return two byte (0x31, 0x32) to application when it receives "Read" request from application.
- Click "Execute Script on the Emulator" button or Press "F5" key.
- Executed result:
2. Read byte[] data from I2C device
- Use "Read(byte[] buff)" method to read byte array data from I2C device.
- Before call this method, you should define a byte[] variable.
- "Read" method will request to device (or emulator) to read data so you need to define correspond procedure on the emulator.
- Select "Application" tab.
- Add below script on the editor box.
| |
rBuff = Util.CreateArrayByte(2)
rBuff[0] = 0x01
rBuff[1] = 0x01
i2c1.Read(rBuff)
res = Util.BytesToHex(rBuff)
print res
| |
|
- Util.BytesToHex(byte[]) method was used to convert byte[] array data into string of hex format.
- Click "Send to MF Application" button or Press "F5" key.
- Executed result:
|