Robot App Store Developer Program

robots-app-store-developer-program

Knowledge Base

Lego NXT File Handling- Write To Files

  By Anna Sandler

This is an advanced topic, please make sure you are familiar with "Introduction To File Handling" tutorial before you continue reading this chapter.

Edit existing textual files on NXT's brick

Only textual files can be reopened for editing.
In order to edit existing textual files on NXT's brick, the following operation must be completed:
  • The file should be opened for writing purposes using the TextFileOpenAppend command that is described below;
  • Use the FileWrite command to write new bytes to the end of the file. This command is described below;
  • Use the FileClose command to save changes to the flash memory and release file's resources (we will go deeper into that in the NXT File Handling- Save and Close tutorial);

Download or transfer files to NXT's brick

In order to send files to NXT's brick, the following operation must be completed:



TextFileOpenAppend command

This command is used to open an existing textual file (txt, log) on NXT's brick by specified the file name for writing operations on the unused space in the file.

When a file is opened successfully, the following operations are performed:
  • File handle is created;
  • The specified file newly created handler is registered to the tracker table; therefore, ResolveHandle command will be able retrieve this file handler according to a file name (will be discussed in later posts);
  • The beginning of the unused space in the file is ready to be written.
Make sure that specified file has an unwritten space for append. (Very important!)

All opened files handles will be closed automatically when the program is finished or failed to open the file.
When the file is closed automatically, unused space inside the specified file is removed to maximize the free space on the device.

It is important to use the FileClose command when the file is no longer needed to free unused resources.
There is a limitation of 16 opened file handles (read and write) in the same thread, and four files that can be opened for writing purposes at the same time.

Syntax

[0x15] [0x00] [0x01] [0x8C] [fileName.fileType]

Explanation:

  • Byte 0-1: Command length LSB first.
  • Byte 2: Command type- system command with a request for a response. Response should always be requested for this command.
  • Byte 3: Command- file open for append.
  • Byte 4-23: file name of existing textual file on NXT's brick, including its extension.
    The system is case insensitive.
    The bytes of file name and type characters should be ASCII encoded.
    NULL terminator must appear at the end ('\0').
    In case the name of the file is shorter than 15 characters, NULL terminator byte should be added to the end of the file name extension for each missing character.
    Total file name characters, including the NULL terminators, should always be 20.

The reply message that is coming back looks like:

[0x08] [0x00] [0x02] [0x8C] [0x00 or error][Handle id] [File size LSB][...] [...] [File size MSB]

Explanation:

  • Byte 0-1: Message length LSB first.
  • Byte 2: Command type- reply message
  • Byte 3: Command that has requested for this reply.
  • Byte 4: Command status- 0x00 for success or error number for failure. Check the NXT File Handling over Bluetooth- Introduction chapter for error numbers.
  • Byte 5: Handle id. The id of handle that has been requested to be opened for append.
  • Byte 6-9: File size in bytes that is unused LSB first.

Example:

The command that is sent to NXT is:
0x16 0x00 0x01 0x8C RobotAppStore1.txt\0\0

The command that is received from NXT as a reply is:
0x08 0x00 0x02 0x8C 0x00 0x04 0x01 0x00 0x00 0x00

FileWrite command

This command is used to write to opened file handle, specified data.
When the writing command is finished successfully, the following operations are performed:
  • Data is written from the current internal cursor position;
  • Internal writing cursor is referenced at the end of specified written data, next writing command will write data to this point and on;
  • Pending file write buffers are committed to flash memory with each 256 written bytes; therefore, there will be a delay of six milliseconds for each flash writing;
In case the command contains byte's length that is bigger than the free space in the specified file, the system will start the writing process until the file is full, it will return the number of bytes that were actually written.

Non textual files must be filled completely before calling the FileClose command.
Only after the close command, the file system on the flash memory is updated with the newly created file.

There is a limitation of 64 bytes per telegram. Make sure you divide the data bytes into several FileWrite commands each is limited to 64 bytes length.

For textual data- make sure the string is ASCII encoded and there is a NULL terminator ('\0') at the end of the data.

Syntax

[0x04 - ???] [0x00- ???] [0x01 or 0x81] [0x83] [Handle id] [Data]

Explanation:

  • Byte 0-1: Command length LSB first.
  • Byte 2: Command type- system command. For system command with response message use 0x01, otherwise, for system command without the reply message, use 0x81.
  • Byte 3: Command- write.
  • Byte 4: Handle id. This number is included in the response message of any of the open file commands.
  • Byte 5-N: Data to write. The bytes of the data to write. In case the data represented by string, it should be ASCII encoded and a NULL terminator must appear at the end (\0).

The reply message that is coming back (if requested) looks like:

[0x06] [0x00] [0x02] [0x83] [0x00 or error] [Handle id] [Number of data that have been written LSB] [Number of data that have been written MSB]

Explanation:

  • Byte 0-1: Message length LSB first.
  • Byte 2: Command type- reply message
  • Byte 3: Command that has requested for this reply.
  • Byte 4: Command status- 0x00 for success or error number for failure. Check the NXT File Handling over Bluetooth- Introduction chapter for error numbers.
  • Byte 5: Handle id. The id of handle that has been requested to be read from.
  • Byte 6-7: The number of bytes that has been actually written into the flash LSB first.

Example:

RobotAppStore1.txt has been opened for append with 0x04 handle id. The allocated free size is big enough for the text we about to send. Let’s add to the end of the file: “Check for more at www.RobotAppStore.com”

The command that is sent to NXT is:
0x2B 0x00 0x01 0x83 0x04 Check for more at www.RobotAppStore.com\0

The command that is received from NXT as a reply is:
0x06 0x00 0x02 0x83 0x00 0x04 0x27 0x00

What's next?

Let's move forward and learn NXT File Handling- Save and Close

married men cheat women who cheat on men how to cheat husband
will my wife cheat again click here how do i know if my wife has cheated
my wife cheated on me now what my wife cheated on me now what why are women unfaithful
sumatriptan succ sumatriptan succ sumatriptan succ







Related Robot-Apps™

Comments