public byte[] I2CReadBytes(int handle, int num) { byte[] data = new byte[num]; GCHandle h = GCHandle.Alloc(data, GCHandleType.Pinned); try { short ret = PiGpioNativeMethods.i2cReadDevice((ushort)handle, h.AddrOfPinnedObject(), (ushort)num); if (ret < 0) { throw new PiGPIOException(ret); } if (ret < num) { byte[] real = new byte[ret]; Array.Copy(data, real, ret); return(real); } else { return(data); } } finally { h.Free(); } }