nxtlib/src/Protocol.cs
/// <summary>
/// [Native] Writes data to an NXT until all data is written.
/// </summary>
/// <param name="handle">The handle, located in the OpenWrite command.</param>
/// <param name="data">The data to be written to flash memory.</param>
/// <returns>The number of bytes written.</returns>
[Native] Writes data to an NXT until all data is written.
[ Native ] Writes data to an NXT until all data is written .
public int Write(byte handle, byte[] data)
        {
            UInt16 bytesWritten = 0;
            int n = 0;
            while (bytesWritten < data.Length)
            {
                System.Threading.Thread.Sleep(7);
                int j = 0;
                int max = data.Length;
                if ((61 * (n + 1)) < max) { max = (61 * (n + 1)); }
                byte[] datatowrite = new byte[max - (61*n)];
                for (int i = 61*n; i < max; i++)
                {
                    datatowrite[j] = data[i];
                    j++;
                }
                byte[] request = new byte[3 + datatowrite.Length];
                request[0] = 0x01;
                request[1] = (byte)MessageCommand.Write;
                request[2] = handle;
                datatowrite.CopyTo(request, 3);
                byte[] reply = CompleteRequest(request);
                byte handleOut = reply[3];
                if (handleOut != handle)
                {
                    throw new NXTReplyIncorrect();
                }
                bytesWritten += GetUInt16(reply, 4);
                n++;
            }
            return Convert.ToInt32(bytesWritten);
        }
public int Write ( byte handle , byte [ ] data ) { UInt16 bytesWritten = 0 ; int n = 0 ; while ( bytesWritten < data . Length ) { System . Threading . Thread . Sleep ( 7 ) ; int j = 0 ; int max = data . Length ; if ( ( 61 * ( n + 1 ) ) < max ) { max = ( 61 * ( n + 1 ) ) ; } byte [ ] datatowrite = new byte [ max - ( 61 * n ) ] ; for ( int i = 61 * n ; i < max ; i ++ ) { datatowrite [ j ] = data [ i ] ; j ++ ; } byte [ ] request = new byte [ 3 + datatowrite . Length ] ; request [ 0 ] = 0x01 ; request [ 1 ] = ( byte ) MessageCommand . Write ; request [ 2 ] = handle ; datatowrite . CopyTo ( request , 3 ) ; byte [ ] reply = CompleteRequest ( request ) ; byte handleOut = reply [ 3 ] ; if ( handleOut != handle ) { throw new NXTReplyIncorrect ( ) ; } bytesWritten += GetUInt16 ( reply , 4 ) ; n ++ ; } return Convert . ToInt32 ( bytesWritten ) ; }
[Native] Writes data to an NXT until all data is written.
[ Native ] Writes data to an NXT until all data is written .
The number of bytes written.
The number of bytes written .
handle
byte
The handle, located in the OpenWrite command.
The handle located in the OpenWrite command .
data
byte[]
The data to be written to flash memory.
The data to be written to flash memory .