Exemple #1
0
        public byte[] ReadBytes(int address, int count)
        {
            var buf = new byte[count];
            var res = I2CNativeLib.ReadBytes(busHandle, address, buf, buf.Length);

            if (res == -1)
            {
                var message = String.Format(
                    "Error accessing address '{0}': {1}",
                    address,
                    UnixMarshal.GetErrorDescription(Stdlib.GetLastError()));
                log.Error(message);
                throw new IOException(message);
            }

            if (res <= 0)
            {
                var message = String.Format("Error reading from address '{0}': I2C transaction failed", address);
                log.Error(message);
                throw new IOException(message);
            }

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

            return(buf);
        }
Exemple #2
0
        public void Open(string busPath)
        {
            log.Trace("I2C bus opening");
            BusPath = busPath;
            int res = I2CNativeLib.OpenBus(busPath);

            if (res < 0)
            {
                throw new IOException(String.Format("Error opening bus '{0}': {1}", busPath, UnixMarshal.GetErrorDescription(Stdlib.GetLastError())));
            }
            busHandle = res;
            log.Trace("I2C bus handle - {0}", busHandle);
        }
Exemple #3
0
        protected virtual void Dispose(bool disposing)
        {
            log.Trace("Disposing");
            if (disposing)
            {
                // disposing managed resouces
            }

            if (busHandle != 0)
            {
                I2CNativeLib.CloseBus(busHandle);
                busHandle = 0;
            }
        }
Exemple #4
0
        public void WriteBytes(int address, byte[] bytes)
        {
            log.Trace("Writing to I2C. Bus handle {0}", busHandle);
            var res = I2CNativeLib.WriteBytes(busHandle, address, bytes, bytes.Length);

            if (res == -1)
            {
                string message = String.Format(
                    "Error accessing address '{0}': {1}",
                    address,
                    UnixMarshal.GetErrorDescription(Stdlib.GetLastError()));

                log.Error(message);
                throw new IOException(message);
            }

            if (res == -2)
            {
                var message = String.Format("Error writing to address '{0}': I2C transaction failed", address);
                log.Error(message);
                throw new IOException(message);
            }
        }