Example #1
0
        /// <summary>
        /// Reads bytes from device with passed address.
        /// </summary>
        /// <param name="address"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public byte[] ReadBytes(int address, int count)
        {
            byte[] buf = new byte[count];
            int    res = I2CNativeLib.ReadBytes(busHandle, address, buf, buf.Length);

            if (res == -1)
            {
                throw new IOException(String.Format("Error accessing address '{0}': {1}", address, UnixMarshal.GetErrorDescription(Stdlib.GetLastError())));
            }
            if (res <= 0)
            {
                throw new IOException(String.Format("Error reading from address '{0}': I2C transaction failed", address));
            }

            if (res < count)
            {
                Array.Resize(ref buf, res);
            }

            return(buf);
        }