BrickPi.Brick.BrickPiTx C# (CSharp) Method

BrickPiTx() private method

Send data to the BrickPi
private BrickPiTx ( int dest, int byteCount, byte OutArray ) : void
dest int The target Arduino, can be 1 or 2
byteCount int number of bytes to send
OutArray byte byte array to send
return void
        private async void BrickPiTx(int dest, int byteCount, byte[] OutArray)
        {
            if (byteCount > OutArray.Length)
                return;
            byte[] txBuff = new byte[3 + byteCount];
            txBuff[0] = (byte)dest;
            txBuff[1] = (byte)((dest + byteCount + sum(0, byteCount, OutArray)) % 256);
            txBuff[2] = (byte)byteCount;
            //OutArray.CopyTo(txBuff, 3);
            for (int i = 0; i < byteCount; i++)
                txBuff[i + 3] = OutArray[i];

            try
            {
                if (serialPort != null)
                {
                    Task<UInt32> storeAsyncTask;
                    //Launch the WriteAsync task to perform the write
                    if (dataWriteObject == null)
                        dataWriteObject = new DataWriter(serialPort.OutputStream);
                    dataWriteObject.WriteBytes(txBuff);
                    storeAsyncTask = dataWriteObject.StoreAsync().AsTask();
                    UInt32 bytesWritten = await storeAsyncTask;
                    if (bytesWritten > 0)
                    {
                        //Debug.WriteLine(string.Format("Bytes written successfully: {0}", bytesWritten));
                    }
                    else
                    {
                        Debug.WriteLine("Error sending data");
                    }
                }
                else
                {
                    Debug.WriteLine("No serial port initialized");
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(string.Format("Eror sending data: {0}", ex.Message));
                // Cleanup once complete
                if (dataWriteObject != null)
                {
                    dataWriteObject.DetachStream();
                    dataWriteObject = null;
                }
            }
        }