Esempio n. 1
0
        //--------
        //-------- MemoryBank I/O methods
        //--------

        /// <summary> Read  memory in the current bank with no CRC checking (device or
        /// data). The resulting data from this API may or may not be what is on
        /// the 1-Wire device.  It is recommends that the data contain some kind
        /// of checking (CRC) like in the readPagePacket() method or have
        /// the 1-Wire device provide the CRC as in readPageCRC().  readPageCRC()
        /// however is not supported on all memory types, see 'hasPageAutoCRC()'.
        /// If neither is an option then this method could be called more
        /// then once to at least verify that the same thing is read consistantly.
        ///
        /// </summary>
        /// <param name="startAddr">    starting physical address
        /// </param>
        /// <param name="readContinue"> if 'true' then device read is continued without
        /// re-selecting.  This can only be used if the new
        /// read() continious where the last one led off
        /// and it is inside a 'beginExclusive/endExclusive'
        /// block.
        /// </param>
        /// <param name="readBuf">      byte array to place read data into
        /// </param>
        /// <param name="offset">       offset into readBuf to place data
        /// </param>
        /// <param name="len">          length in bytes to read
        ///
        /// </param>
        /// <throws>  OneWireIOException </throws>
        /// <throws>  OneWireException </throws>
        public virtual void  read(int startAddr, bool readContinue, byte[] readBuf, int offset, int len)
        {
            int i;

            // attempt to put device at max desired speed
            if (!readContinue)
            {
                sp.checkSpeed();
            }

            // check if read exceeds memory
            if ((startAddr + len) > (pageLength * numberPages))
            {
                throw new OneWireException("Read exceeds memory bank end");
            }

            // see if need to access the device
            if (!readContinue)
            {
                // select the device
                if (!ib.adapter.select(ib.address))
                {
                    sp.forceVerify();

                    throw new OneWireIOException("Device select failed");
                }

                // build start reading memory block
                int    addr    = startAddr + startPhysicalAddress;
                byte[] raw_buf = new byte[3];

                raw_buf[0] = READ_MEMORY_COMMAND;
                raw_buf[1] = (byte)(addr & 0xFF);
                raw_buf[2] = (byte)((SupportClass.URShift((addr & 0xFFFF), 8)) & 0xFF);

                // do the first block for command, address
                ib.adapter.dataBlock(raw_buf, 0, 3);
            }

            // pre-fill readBuf with 0xFF
            int pgs   = len / pageLength;
            int extra = len % pageLength;

            for (i = 0; i < pgs; i++)
            {
                Array.Copy(ffBlock, 0, readBuf, offset + i * pageLength, pageLength);
            }
            Array.Copy(ffBlock, 0, readBuf, offset + pgs * pageLength, extra);

            // send second block to read data, return result
            ib.adapter.dataBlock(readBuf, offset, len);
        }
Esempio n. 2
0
        //--------
        //-------- MemoryBank I/O methods
        //--------

        /// <summary> Read  memory in the current bank with no CRC checking (device or
        /// data). The resulting data from this API may or may not be what is on
        /// the 1-Wire device.  It is recommends that the data contain some kind
        /// of checking (CRC) like in the readPagePacket() method or have
        /// the 1-Wire device provide the CRC as in readPageCRC().  readPageCRC()
        /// however is not supported on all memory types, see 'hasPageAutoCRC()'.
        /// If neither is an option then this method could be called more
        /// then once to at least verify that the same thing is read consistantly.
        ///
        /// </summary>
        /// <param name="startAddr">    starting physical address
        /// </param>
        /// <param name="readContinue"> if 'true' then device read is continued without
        /// re-selecting.  This can only be used if the new
        /// read() continious where the last one led off
        /// and it is inside a 'beginExclusive/endExclusive'
        /// block.
        /// </param>
        /// <param name="readBuf">      byte array to place read data into
        /// </param>
        /// <param name="offset">       offset into readBuf to place data
        /// </param>
        /// <param name="len">          length in bytes to read
        ///
        /// </param>
        /// <throws>  OneWireIOException </throws>
        /// <throws>  OneWireException </throws>
        public virtual void  read(int startAddr, bool readContinue, byte[] readBuf, int offset, int len)
        {
            byte[] buff = new byte[150];

            Array.Copy(ffBlock, 0, buff, 0, 150);

            // check if read exceeds memory
            if ((startAddr + len) > (pageLength * numberPages))
            {
                throw new OneWireException("Read exceeds memory bank end");
            }

            if (len < 0)
            {
                throw new OneWireException("Invalid length");
            }


            // attempt to put device at max desired speed
            if (!readContinue)
            {
                sp.checkSpeed();

                // select the device
                if (ib.adapter.select(ib.address))
                {
                    buff[0] = READ_MEMORY_COMMAND;

                    // address 1
                    buff[1] = (byte)((startAddr + startPhysicalAddress) & 0xFF);
                    // address 2
                    buff[2] = (byte)((SupportClass.URShift(((startAddr + startPhysicalAddress) & 0xFFFF), 8)) & 0xFF);

                    ib.adapter.dataBlock(buff, 0, len + 3);

                    // extract the data
                    Array.Copy(buff, 3, readBuf, offset, len);
                }
                else
                {
                    throw new OneWireIOException("Device select failed");
                }
            }
            else
            {
                ib.adapter.dataBlock(buff, 0, len);

                // extract the data
                Array.Copy(buff, 0, readBuf, offset, len);
            }
        }