/// <summary> /// Reads an array of bytes from an I2C device. /// </summary> /// <param name="address">The address of the I2C device.</param> /// <param name="readBuffer">The array of bytes that will be read from the I2C device.</param> /// <param name="errorBehavior">Whether or not to throw an exception if the read does not complete.</param> /// <returns>The number of bytes read from the device.</returns> public int Read(byte address, byte[] readBuffer, LengthErrorBehavior errorBehavior = LengthErrorBehavior.ThrowException) { if (readBuffer == null) { return(0); } int numWritten, numRead; WriteRead(address, null, 0, 0, readBuffer, 0, readBuffer.Length, out numWritten, out numRead, errorBehavior); return(numRead); }
/// <summary> /// Reads a register from a I2C device using a memory map API. /// </summary> /// <param name="address">The address of the I2C device.</param> /// <param name="register">The single byte to write to the device (normally the register address on the device).</param> /// <param name="errorBehavior">Whether or not to throw an exception if the read does not complete.</param> /// <returns>The single byte read from the device.</returns> public byte ReadRegister(byte address, byte register, LengthErrorBehavior errorBehavior = LengthErrorBehavior.ThrowException) { byte[] toWrite = new byte[1] { register }; byte[] toRead = new byte[1] { 0 }; int numWritten, numRead; WriteRead(address, toWrite, 0, 1, toRead, 0, 1, out numWritten, out numRead, errorBehavior); return(toRead[0]); }
/// <summary> /// Writes an array of bytes and then reads an array of bytes from/to an I2C device. /// </summary> /// <param name="address">The bus address of the I2C device (bottom 7 bits only).</param> /// <param name="writeBuffer">The array of data to write to the device.</param> /// <param name="readBuffer">The array that will hold data read from the device.</param> /// <param name="errorBehavior">Whether or not to throw an exception if the write/read does not complete.</param> /// <returns>The total number of bytes transferred in the transaction.</returns> public int WriteRead(byte address, byte[] writeBuffer, byte[] readBuffer, LengthErrorBehavior errorBehavior = LengthErrorBehavior.ThrowException) { int numWritten, numRead; WriteRead(address, writeBuffer, 0, writeBuffer == null ? 0 : writeBuffer.Length, readBuffer, 0, readBuffer == null ? 0 : readBuffer.Length, out numWritten, out numRead, errorBehavior); if (numWritten < 0) { return(numWritten); } if (numRead < 0) { return(numRead); } return(numWritten + numRead); }
/// <summary> /// Writes an array of bytes and then reads an array of bytes from/to an I2C device. /// </summary> /// <param name="address">The bus address of the I2C device (bottom 7 bits only).</param> /// <param name="writeBuffer">The array of data to write to the device.</param> /// <param name="readBuffer">The array that will hold data read from the device.</param> /// <param name="errorBehavior">Whether or not to throw an exception if the write/read does not complete.</param> /// <returns>The total number of bytes transferred in the transaction.</returns> public int WriteRead(byte address, byte[] writeBuffer, byte[] readBuffer, LengthErrorBehavior errorBehavior = LengthErrorBehavior.ThrowException) { int numWritten, numRead; WriteRead(address, writeBuffer, 0, writeBuffer == null ? 0 : writeBuffer.Length, readBuffer, 0, readBuffer == null ? 0 : readBuffer.Length, out numWritten, out numRead, errorBehavior); if (numWritten < 0) return numWritten; if (numRead < 0) return numRead; return numWritten + numRead; }
/// <summary> /// Writes an array of bytes and then reads an array of bytes from/to an I2C device. /// </summary> /// <param name="address">The bus address of the I2C device (bottom 7 bits only).</param> /// <param name="writeBuffer">The array of data to write to the device..</param> /// <param name="writeOffset">The index of the first byte in the "writeBuffer" array to be written.</param> /// <param name="writeLength">The number of bytes from the "writeBuffer" array to be written.</param> /// <param name="readBuffer">The array that will hold data read from the device.</param> /// <param name="readOffset">The index of the first location in the "readBuffer" array to be written to.</param> /// <param name="readLength">The number of bytes that will be written to the "readBuffer" array.</param> /// <param name="numWritten">The number of bytes actually written to the device.</param> /// <param name="numRead">The number of bytes actually read from the device.</param> /// <param name="errorBehavior">Whether or not to throw an exception if the write/read does not complete.</param> public void WriteRead(byte address, byte[] writeBuffer, int writeOffset, int writeLength, byte[] readBuffer, int readOffset, int readLength, out int numWritten, out int numRead, LengthErrorBehavior errorBehavior = LengthErrorBehavior.ThrowException) { // Sanity check the passed arguments if (writeBuffer == null) { writeBuffer = new byte[0]; writeOffset = 0; writeLength = 0; } else if (writeBuffer.Length < writeOffset + writeLength || writeOffset < 0 || writeLength < 0) { throw new ArgumentException("SoftwareI2C: WriteRead call to device at address " + address + " on socket " + socket + " has bad writeBuffer parameters (buffer too small or negative length or offset specified)"); } if (readBuffer == null) { readBuffer = new byte[0]; readOffset = 0; readLength = 0; } else if (readBuffer.Length < readOffset + readLength || readOffset < 0 || readLength < 0) { throw new ArgumentException("SoftwareI2C: WriteRead call to device at address " + address + " on socket " + socket + " has bad readBuffer parameters (buffer too small or negative length or offset specified)"); } if (!usingManaged) { socket.NativeI2CWriteRead(socket, sdaPin, sclPin, address, writeBuffer, writeOffset, writeLength, readBuffer, readOffset, readLength, out numWritten, out numRead); } else { DoManagedWriteRead(address, writeBuffer, writeOffset, writeLength, readBuffer, readOffset, readLength, out numWritten, out numRead); } if (errorBehavior == LengthErrorBehavior.ThrowException && (numWritten != writeLength || numRead != readLength)) { throw new ApplicationException("SoftwareI2C: Exception writing to device at address " + address + " on socket " + socket + " - perhaps device is not responding or not plugged in."); } }
/// <summary> /// Writes an array of bytes to an I2C device. /// </summary> /// <param name="address">The address of the I2C device.</param> /// <param name="writeBuffer">The array of bytes that will be written to the I2C device.</param> /// <param name="errorBehavior">Whether or not to throw an exception if the write/read does not complete.</param> /// <returns>The number of bytes written to the device.</returns> public int Write(byte address, byte[] writeBuffer, LengthErrorBehavior errorBehavior = LengthErrorBehavior.ThrowException) { if (writeBuffer == null) return 0; int numWritten, numRead; WriteRead(address, writeBuffer, 0, writeBuffer.Length, null, 0, 0, out numWritten, out numRead, errorBehavior); return numWritten; }
/// <summary> /// Reads a register from a I2C device using a memory map API. /// </summary> /// <param name="address">The address of the I2C device.</param> /// <param name="register">The single byte to write to the device (normally the register address on the device).</param> /// <param name="errorBehavior">Whether or not to throw an exception if the read does not complete.</param> /// <returns>The single byte read from the device.</returns> public byte ReadRegister(byte address, byte register, LengthErrorBehavior errorBehavior = LengthErrorBehavior.ThrowException) { byte[] toWrite = new byte[1] { register }; byte[] toRead = new byte[1] { 0 }; int numWritten, numRead; WriteRead(address, toWrite, 0, 1, toRead, 0, 1, out numWritten, out numRead, errorBehavior); return toRead[0]; }
/// <summary> /// Writes an array of bytes and then reads an array of bytes from/to an I2C device. /// </summary> /// <param name="address">The bus address of the I2C device (bottom 7 bits only).</param> /// <param name="writeBuffer">The array of data to write to the device..</param> /// <param name="writeOffset">The index of the first byte in the "writeBuffer" array to be written.</param> /// <param name="writeLength">The number of bytes from the "writeBuffer" array to be written.</param> /// <param name="readBuffer">The array that will hold data read from the device.</param> /// <param name="readOffset">The index of the first location in the "readBuffer" array to be written to.</param> /// <param name="readLength">The number of bytes that will be written to the "readBuffer" array.</param> /// <param name="numWritten">The number of bytes actually written to the device.</param> /// <param name="numRead">The number of bytes actually read from the device.</param> /// <param name="errorBehavior">Whether or not to throw an exception if the write/read does not complete.</param> public void WriteRead(byte address, byte[] writeBuffer, int writeOffset, int writeLength, byte[] readBuffer, int readOffset, int readLength, out int numWritten, out int numRead, LengthErrorBehavior errorBehavior = LengthErrorBehavior.ThrowException) { // Sanity check the passed arguments if (writeBuffer == null) { writeBuffer = new byte[0]; writeOffset = 0; writeLength = 0; } else if (writeBuffer.Length < writeOffset + writeLength || writeOffset < 0 || writeLength < 0) { throw new ArgumentException("SoftwareI2C: WriteRead call to device at address " + address + " on socket " + socket + " has bad writeBuffer parameters (buffer too small or negative length or offset specified)"); } if (readBuffer == null) { readBuffer = new byte[0]; readOffset = 0; readLength = 0; } else if (readBuffer.Length < readOffset + readLength || readOffset < 0 || readLength < 0) { throw new ArgumentException("SoftwareI2C: WriteRead call to device at address " + address + " on socket " + socket + " has bad readBuffer parameters (buffer too small or negative length or offset specified)"); } if (!usingManaged) { socket.NativeI2CWriteRead(socket, sdaPin, sclPin, address, writeBuffer, writeOffset, writeLength, readBuffer, readOffset, readLength, out numWritten, out numRead); } else { DoManagedWriteRead(address, writeBuffer, writeOffset, writeLength, readBuffer, readOffset, readLength, out numWritten, out numRead); } if (errorBehavior == LengthErrorBehavior.ThrowException && (numWritten != writeLength || numRead != readLength)) { throw new ApplicationException("SoftwareI2C: Exception writing to device at address " + address + " on socket " + socket + " - perhaps device is not responding or not plugged in."); } }