Home→Forums→MonoBrick EV3 Firmware→I2C communication PCF8574AP→Reply To: I2C communication PCF8574AP
October 9, 2014 at 17:59
#4875
Bartłomiej Drozd
Participant
unfortunetly, it is not workking… i found source of sending function
/// <summary>
/// Write and read an array of bytes to the sensor
/// </summary>
/// <returns>The bytes that was read</returns>
/// <param name="register">Register to write to.</param>
/// <param name="data">Byte array to write</param>
/// <param name="rxLength">Length of the expected reply</param>
protected byte[] WriteAndRead (byte register, byte[] data, int rxLength)
{
if (rxLength > BufferSize)
throw new ArgumentOutOfRangeException("I2C Receive Buffer only holds " + BufferSize + " bytes");
if (data.Length > BufferSize) {
throw new ArgumentOutOfRangeException("I2C Write Buffer only holds " + BufferSize + " bytes");
}
bool dataReady = false;
int replyIndex = 0;
byte[] writeData = new byte[BufferSize];//30
Array.Copy (data, 0, writeData, 0, data.Length);
ByteArrayCreator command = new ByteArrayCreator ();
command.Append ((int)-1);
command.Append ((byte)this.port);
command.Append ((byte)1);//repeat
command.Append ((short)0);//time
command.Append ((byte)(data.Length + 2));//length of write data
command.Append ((byte)((byte)I2CAddress >> 1));
command.Append (register);
command.Append(writeData);
command.Append ((byte)-rxLength);
replyIndex = command.Data.Length;
command.Append (new byte[BufferSize]);//make room for reply
byte[] i2cData = command.Data;
while (!dataReady) {
unchecked {
I2CDevice.IoCtl ((Int32)I2CIOSetup, i2cData);
}
int status = BitConverter.ToInt32 (i2cData, 0);
if (status < 0) {
throw new Exception ("I2C I/O error");
}
if (status == 0) {
byte[] reply = new byte[rxLength];
if (rxLength > 0) {
Array.Copy(i2cData,replyIndex, reply,0, rxLength);
}
return reply;
}
}
throw new TimeoutException("I2C timeout");
}
maybe this should help…
Follow