Skip to main content

I2cDevice Class

Namespace: GHIElectronics.TinyCLR.Devices.I2c · Assembly: GHIElectronics.TinyCLR.Devices.I2c

Represents a single slave on the I²C bus. Master-mode devices use the blocking Read(byte[]) / Write(byte[]) / WriteRead(byte[],byte[]) family; slave-mode devices additionally expose FrameReceived and ErrorReceived events.

public class I2cDevice : IDisposable

Properties

ConnectionSettings

public I2cConnectionSettings ConnectionSettings { get; }

The per-device connection settings.

Controller

public I2cController Controller { get; }

The I2cController that owns this device.

WriteBufferSize

public int WriteBufferSize { get; }

Slave-mode only: size in bytes of the controller's outgoing buffer.

ReadBufferSize

public int ReadBufferSize { get; }

Slave-mode only: size in bytes of the controller's incoming buffer.

BytesToWrite

public int BytesToWrite { get; }

Slave-mode only: bytes currently queued to transmit.

BytesToRead

public int BytesToRead { get; }

Slave-mode only: bytes currently available to read.

Methods

Dispose()

public void Dispose()

Releases device-level resources.

Read(byte[] buffer)

public void Read(byte[] buffer)

Reads buffer.Length bytes from the slave.

ParameterTypeDescription
bufferbyte[]

Write(byte[] buffer)

public void Write(byte[] buffer)

Writes buffer.Length bytes to the slave.

ParameterTypeDescription
bufferbyte[]

WriteRead(byte[] writeBuffer, byte[] readBuffer)

public void WriteRead(byte[] writeBuffer, byte[] readBuffer)

Performs a register-style write-then-read transaction.

ParameterTypeDescription
writeBufferbyte[]
readBufferbyte[]

Read(byte[] buffer, int offset, int length)

public void Read(byte[] buffer, int offset, int length)

Reads length bytes into buffer starting at offset.

ParameterTypeDescription
bufferbyte[]
offsetint
lengthint

Write(byte[] buffer, int offset, int length)

public void Write(byte[] buffer, int offset, int length)

Writes length bytes from buffer starting at offset.

ParameterTypeDescription
bufferbyte[]
offsetint
lengthint

WriteRead(byte[] writeBuffer, int writeOffset, int writeLength, byte[] readBuffer, int readOffset, int readLength)

public void WriteRead(byte[] writeBuffer, int writeOffset, int writeLength, byte[] readBuffer, int readOffset, int readLength)

Performs a write-then-read transaction with explicit slice offsets and lengths. Throws when the slave NACKs or the transfer is otherwise incomplete (master mode only).

ParameterTypeDescription
writeBufferbyte[]
writeOffsetint
writeLengthint
readBufferbyte[]
readOffsetint
readLengthint

ReadPartial(byte[] buffer)

public I2cTransferResult ReadPartial(byte[] buffer)

Like Read(byte[]) but returns a status + count instead of throwing on a partial transfer.

ParameterTypeDescription
bufferbyte[]

Returns I2cTransferResult

WritePartial(byte[] buffer)

public I2cTransferResult WritePartial(byte[] buffer)

Like Write(byte[]) but returns a status + count instead of throwing on a partial transfer.

ParameterTypeDescription
bufferbyte[]

Returns I2cTransferResult

WriteReadPartial(byte[] writeBuffer, byte[] readBuffer)

public I2cTransferResult WriteReadPartial(byte[] writeBuffer, byte[] readBuffer)

Like WriteRead(byte[],byte[]) but returns a status + counts.

ParameterTypeDescription
writeBufferbyte[]
readBufferbyte[]

Returns I2cTransferResult

ReadPartial(byte[] buffer, int offset, int length)

public I2cTransferResult ReadPartial(byte[] buffer, int offset, int length)

Partial read with explicit slice offsets and lengths.

ParameterTypeDescription
bufferbyte[]
offsetint
lengthint

Returns I2cTransferResult

WritePartial(byte[] buffer, int offset, int length)

public I2cTransferResult WritePartial(byte[] buffer, int offset, int length)

Partial write with explicit slice offsets and lengths.

ParameterTypeDescription
bufferbyte[]
offsetint
lengthint

Returns I2cTransferResult

WriteReadPartial(byte[] writeBuffer, int writeOffset, int writeLength, byte[] readBuffer, int readOffset, int readLength)

public I2cTransferResult WriteReadPartial(byte[] writeBuffer, int writeOffset, int writeLength, byte[] readBuffer, int readOffset, int readLength)

Partial write-then-read with explicit slice offsets and lengths.

ParameterTypeDescription
writeBufferbyte[]
writeOffsetint
writeLengthint
readBufferbyte[]
readOffsetint
readLengthint

Returns I2cTransferResult — A I2cTransferResult with the transfer status and byte counts.

ClearWriteBuffer()

public void ClearWriteBuffer()

Slave-mode only: empties the controller's outgoing buffer.

ClearReadBuffer()

public void ClearReadBuffer()

Slave-mode only: empties the controller's incoming buffer.

Events

FrameReceived

public event FrameReceivedEventHandler FrameReceived

Slave-mode only: raised when a master start/stop or data frame is observed addressed to this slave.

ErrorReceived

public event ErrorReceivedEventHandler ErrorReceived

Slave-mode only: raised when the controller detects a bus error (overrun, arbitration loss, etc.).