private void ReceiveData(out byte[] data)
        {
            int       len;
            ErrorCode err;

            data = null;

            err = _device.Reader.Read(p_ptpdata, 0, 512, 5000, out len);

            CheckError(err);

            ptpdata = (PTPData)Marshal.PtrToStructure(p_ptpdata, typeof(PTPData));

            if (ptpdata.Length <= 512)
            {
                // must read empty end package if length is multiple of 512
                if (ptpdata.Length == 512)
                {
                    // if 0 length is used Read does nothing, so use 512 and supply p_ptpdata in case something is received
                    err = _device.Reader.Read(p_ptpdata, 0, 512, 5000, out len);
                    CheckErrorAndLength(err, len, 0);
                }

                data = new byte[ptpdata.Length - 12];
                Array.Copy(ptpdata.data, data, data.Length);

                return;
            }

            // N.B.: USBEndPointReader expects multiple of MaxPacketSize but does returns actually length
            int    padded_remaining_length = (ptpdata.Length - 1) & ~0x1ff; // ((ptpdata.Length-512)+511) % 512
            IntPtr p = Marshal.AllocHGlobal(padded_remaining_length);

            try
            {
                err = _device.Reader.Read(p, 0, padded_remaining_length, 5000, out len);
                CheckErrorAndLength(err, len, ptpdata.Length - 512);

                // must read empty end package if length is multiple of 512
                if ((len & 0x1ff) == 0)
                {
                    // if 0 length is used Read does nothing, so use 512 and supply p_ptpdata in case something is received
                    err = _device.Reader.Read(p_ptpdata, 0, 512, 5000, out len);
                    CheckErrorAndLength(err, len, 0);
                }

                data = new byte[ptpdata.Length - 12];
                Array.Copy(ptpdata.data, data, 500);
                Marshal.Copy(p, data, 500, ptpdata.Length - 512);
            }
            finally
            {
                Marshal.FreeHGlobal(p);
            }
        }
        public PTPCommunication(PTPDevice dev)
        {
            _device = dev;

            reqres       = new PTPReqRes();
            reqres.data  = new byte[484];
            ptpdata      = new PTPData();
            ptpdata.data = new byte[500];
            p_reqres     = Marshal.AllocHGlobal(Marshal.SizeOf(reqres));
            p_ptpdata    = Marshal.AllocHGlobal(Marshal.SizeOf(ptpdata));

            ResetAll();
        }
        public PTPCommunication(PTPDevice dev)
        {
            _device = dev;

            reqres = new PTPReqRes();
            reqres.data = new byte[484];
            ptpdata = new PTPData();
            ptpdata.data = new byte[500];
            p_reqres = Marshal.AllocHGlobal(Marshal.SizeOf(reqres));
            p_ptpdata = Marshal.AllocHGlobal(Marshal.SizeOf(ptpdata));

            ResetAll();
        }
        public PTPCommunication(PTPDevice dev)
        {
            this.device = dev;

            this.reqres = new PTPReqRes();
            this.reqres.data = new byte[484];
            this.ptpdata = new PTPData();
            this.ptpdata.data = new byte[500];
            this.p_reqres = Marshal.AllocHGlobal(Marshal.SizeOf(this.reqres));
            this.p_ptpdata = Marshal.AllocHGlobal(Marshal.SizeOf(this.ptpdata));

            this.ResetAll();
        }
        private void ReceiveData(out byte[] data)
        {
            int len;
            ErrorCode err;

            data = null;

            err = _device.Reader.Read(p_ptpdata, 0, 512, 5000, out len);

            CheckError(err);

            ptpdata = (PTPData)Marshal.PtrToStructure(p_ptpdata, typeof(PTPData));

            if (ptpdata.Length <= 512)
            {
                // must read empty end package if length is multiple of 512
                if (ptpdata.Length == 512)
                {
                    // if 0 length is used Read does nothing, so use 512 and supply p_ptpdata in case something is received
                    err = _device.Reader.Read(p_ptpdata, 0, 512, 5000, out len);
                    CheckErrorAndLength(err, len, 0);
                }

                data = new byte[ptpdata.Length - 12];
                Array.Copy(ptpdata.data, data, data.Length);

                return;
            }

            // N.B.: USBEndPointReader expects multiple of MaxPacketSize but does returns actually length
            int padded_remaining_length = (ptpdata.Length-1) & ~0x1ff; // ((ptpdata.Length-512)+511) % 512
            IntPtr p = Marshal.AllocHGlobal(padded_remaining_length);

            try
            {
                err = _device.Reader.Read(p, 0, padded_remaining_length, 5000, out len);
                CheckErrorAndLength(err, len, ptpdata.Length-512);

                // must read empty end package if length is multiple of 512
                if ((len & 0x1ff) == 0)
                {
                    // if 0 length is used Read does nothing, so use 512 and supply p_ptpdata in case something is received
                    err = _device.Reader.Read(p_ptpdata, 0, 512, 5000, out len);
                    CheckErrorAndLength(err, len, 0);
                }

                data = new byte[ptpdata.Length - 12];
                Array.Copy(ptpdata.data, data, 500);
                Marshal.Copy(p, data, 500, ptpdata.Length-512);
            }
            finally
            {
                Marshal.FreeHGlobal(p);
            }
        }