Beispiel #1
0
        /// <summary>
        /// Reads a number of characters from the ISerial input buffer and writes them into an array of characters at a given offset.
        /// </summary>
        /// <param name="buffer">The byte array to write the input to.</param>
        /// <param name="offset">The offset in buffer at which to write the bytes.</param>
        /// <param name="count">The maximum number of bytes to read. Fewer bytes are read if count is greater than the number of bytes in the input buffer.</param>
        /// <returns>The number of bytes read.</returns>
        public int Read(byte[] buffer, int offset, int count)
        {
            Utils.PrintDeep("-------READ_START--------");

            Utils.PrintDeep("BleSerial.Read.." +
                            " Offset " + offset.ToString("D2") +
                            " | Count " + count.ToString("D2"));

            ExceptionCheck(buffer, offset, count);

            try
            {
                for (int i = 0; i < count; i++)
                {
                    // FIFO collection to read data frames in the same order received
                    buffer[i + offset] = ble_port_serial.GetBufferElement();

                    // Last two bytes are the CRC
                    if (ble_port_serial.BytesToRead() == 0)
                    {
                        Utils.PrintDeep("BleSerial.Read.." +
                                        " DataFrames " + (i + 1).ToString("D2") +
                                        " | Buffer " + Utils.ByteArrayToString(buffer));

                        return(i + 1);
                    }
                }
            }
            catch (Exception e)
            {
                Utils.PrintDeep("BleSerial.Read -> ERROR: " + e.Message);

                throw e;
            }
            finally
            {
                Utils.PrintDeep("-------READ_FINISH-------");
            }

            return(0);
        }
Beispiel #2
0
        /// <summary>
        /// Reads a number of characters from the ISerial input buffer and writes them into an array of characters at a given offset.
        /// </summary>
        /// <param name="buffer">The byte array to write the input to.</param>
        /// <param name="offset">The offset in buffer at which to write the bytes.</param>
        /// <param name="count">The maximum number of bytes to read. Fewer bytes are read if count is greater than the number of bytes in the input buffer.</param>
        /// <returns>The number of bytes read.</returns>
        public int Read(byte[] buffer, int offset, int count)
        {
            ExceptionCheck(buffer, offset, count);

            int readedElements = 0;

            try{
                for (int i = 0; i < count; i++)
                {
                    if (ble_port_serial.BytesToRead() == 0)
                    {
                        return(readedElements);
                    }
                    buffer[i + offset] = ble_port_serial.GetBufferElement();
                    readedElements++;
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            return(readedElements);
        }