Пример #1
0
        public byte[] Read(I2cReadParameter param, uint byteCount)
        {
            I2CBusCapability capability = GetBusCapabilities();

            if (capability.IsUnsupported)
            {
                throw new Exception("I2C Bus not supported");
            }

            if (byteCount > capability.MaxBlockLen)
            {
                throw new IOException("Byte count of " + byteCount + " exceeds max Block Length of " +
                                      capability.MaxBlockLen);
            }

            var buf = new byte[byteCount];

            uint resultCode = EApiI2CReadTransfer((uint)Eapi_I2C_ID.EAPI_ID_I2C_EXTERNAL, param.Addr, param.Cmd, buf, (uint)buf.Length, byteCount);

            if (!_eApiStatusCodes.IsSuccess(resultCode))
            {
                throw new Exception("EApiI2CReadTransfer " + Eapi_I2C_ID.EAPI_ID_I2C_EXTERNAL + ": " + _eApiStatusCodes.GetStatusStringFrom(resultCode));
            }
            //_logger.Information("Read Bytes: " + BitConverter.ToString(buf));
            return(buf);
        }
Пример #2
0
        internal void Write(I2cWriteParam param)
        {
            I2CBusCapability capability = GetBusCapabilities();

            if (capability.IsUnsupported)
            {
                throw new Exception("I2C Bus not supported");
            }

            if (capability.MaxBlockLen < param.Data.Length)
            {
                throw new IOException("Byte count of " + param.Data.Length + " exceeds max Block Length of " + capability.MaxBlockLen);
            }

            uint resultCode = EApiI2CWriteTransfer((uint)Eapi_I2C_ID.EAPI_ID_I2C_EXTERNAL, param.Addr, param.Cmd, param.Data, (uint)param.Data.Length);

            if (!_eApiStatusCodes.IsSuccess(resultCode))
            {
                throw new Exception("EApiI2CWriteTransfer " + Eapi_I2C_ID.EAPI_ID_I2C_EXTERNAL + ": " + _eApiStatusCodes.GetStatusStringFrom(resultCode));
            }
        }