Beispiel #1
0
        /// <summary>
        /// Writes a specified number of characters to the serial port using data from a buffer.
        /// </summary>
        /// <param name="buffer">The byte array that contains the data to write to the port.</param>
        /// <param name="offset">The zero-based byte offset in the buffer parameter at which to begin copying bytes to the port.</param>
        /// <param name="count">The number of bytes to write.</param>
        /// <remarks></remarks>
        public async Task Write(byte[] buffer, int offset, int count)
        {
            Utils.PrintDeep("-------WRITE_START-------");

            Utils.PrintDeep("BleSerial.Write.." +
                            " Buffer " + Utils.ByteArrayToString(buffer) +
                            " | Offset ( is always ) " + offset.ToString("D2") +
                            " | NumBytesToWrite " + count.ToString("D2"));

            try
            {
                ExceptionCheck(buffer, offset, count);

                ble_port_serial.ClearBuffer();

                if (ble_port_serial.semaphore.CurrentCount <= 0)
                {
                    ble_port_serial.semaphore.Release();
                }

                ble_port_serial.timeInit = 0L;

                int totalBytesToWrite = count;
                int bytesWritten      = 0;
                do
                {
                    // Each data frame max length is 16
                    int bytesDataFrame = (totalBytesToWrite < 16) ? totalBytesToWrite : 16;

                    Utils.PrintDeep("BleSerial.Write.." +
                                    " Next stream " + bytesDataFrame.ToString("D2") + " bytes" +
                                    " | Written " + bytesWritten.ToString("D2") + "/" + count.ToString("D2"));

                    await ble_port_serial.Write_Characteristic(buffer, bytesWritten + offset, bytesDataFrame);

                    totalBytesToWrite -= bytesDataFrame;
                    bytesWritten      += bytesDataFrame;
                }while (totalBytesToWrite > 0);
            }
            catch (Exception e)
            {
                Utils.PrintDeep("BleSerial.Write -> ERROR: " + e.Message);

                throw e;
            }
            finally
            {
                Utils.PrintDeep("-------WRITE_FINISH------");
            }
        }
Beispiel #2
0
        /// <summary>
        /// Writes a specified number of characters to the serial port using data from a buffer.
        /// </summary>
        /// <param name="buffer">The byte array that contains the data to write to the port.</param>
        /// <param name="offset">The zero-based byte offset in the buffer parameter at which to begin copying bytes to the port.</param>
        /// <param name="count">The number of bytes to write.</param>
        /// <remarks></remarks>
        public void Write(byte[] buffer, int offset, int count)
        {
            int bytesToWrite = count;
            int bytesWritten = 0;

            ExceptionCheck(buffer, offset, count);
            do
            {
                int currentWriteCount = 16;
                if (bytesToWrite <= 16)
                {
                    currentWriteCount = bytesToWrite;
                }
                ble_port_serial.ClearBuffer();   // TO-DO
                ble_port_serial.Write_Characteristic(buffer, bytesWritten + offset, currentWriteCount);
                // TODO: check ack before next iteration

                bytesToWrite = bytesToWrite - currentWriteCount;
                bytesWritten = bytesWritten + currentWriteCount;
            }while (bytesToWrite > 0);
        }