Robot App Store Developer Program

robots-app-store-developer-program

Knowledge Base

Lego NXT File Handling- Search Files

  By Anna Sandler

Please make sure you've read the "Introduction To File Handling" tutorial before you continue reading this chapter.

Usages

  • Get information about the specified file actual size;
  • Check whether a specific file name exists in NXT's file system;
  • Get all the files of a specified type;
  • Get all the files of a specified file name;
  • Get all the files that exist in NXT's file system;
  • Find the biggest file in NXT's file system for removal purposes;

Search expressions:

On the NXT brick’s flash memory, files are organized in a simple, flat, file-system without folders.
The system is case insensitive, meaning uppercase characters are treated the same as lower case characters.
  • [fileName . fileType]- If you know the exact name of the file;
  • [* . fileType]- If you want to retrieve all files of a specified file type;
  • [fileName . *]- If you want to retrieve all file types of a specified file name;
  • [* . *]- Get all files in the NXT's file system;

How to search for a file:

  • Use the FileFindFirst command with the file name as a search term;
  • If the first result (from the response message) is not satisfying,
    use the FileFindNext command with the handler id from the FileFindFirst command response message;
  • Continue with the FileFindNext command with the handler id from the previous response message until you find the file,
    or you get the 0x87 error- file not found if you've reached the end of the results list.
  • Use the FileClose command when you finish with the search;



FileFindFirst command

This command is used to find an existing file on NXT's brick.

Make sure that specified file name exists in the file system.

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 for each file that was found to free unused resources.

Syntax

[0x16] [0x00] [0x01][0x86] [fileName or * . fileType or *]

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- find a file according to specified expression.
  • Byte 4-23: File name including its extension, or a search expression according to the "Search expression" section above.
    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:

[0x1C] [0x00] [0x02] [0x86] [0x00 or error] [handler id] [fileName.fileType] [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: Handler id. The id of the handler that has been requested to be created.
  • Byte 6-25: File name. The full name of the found file, including its extension and a NULL terminators to complete the missing bytes.
    Total length of the name should always be 20.
  • Byte 26-29: File size in bytes. LSB first.

Example:

Search for the RobotAppStore1.log file.


The command that is sent to NXT is:
0x16 0x00 0x01 0x86 RobotAppStore1.log\0\0

The command that is received from NXT as a reply is:
0x1C 0x00 0x02 0x86 0x00 0x04 RobotAppStore1.log\0\0 0x04 0x00 0x00 0x00



FileFindNext command

This command is used to find the next result of the search expression from FileFindFirst command.

Make sure that specified handler id was retrieved from the response message of previous search.

All opened files handles will be closed automatically when the program is finished.

It is important to use the FileClose command for each file that was found to free unused resources.

Syntax

[0x03] [0x00] [0x01] [0x87] [handler id]

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- find the next file according to specified expression from previous FileFindFirst command.
  • Byte 4: Handler id. The search handler id from the previous search response message (FileFindFirst or FileFindNext commands).

The reply message that is coming back looks like:

[0x1C] [0x00] [0x02] [0x87] [0x00 or error] [handler id] [fileName.fileType] [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: Handler id. The id of the handler that has been requested to be created.
  • Byte 6-25: File name. The full name of the found file, including its extension and a NULL terminators to complete the missing bytes.
    Total length of the name should always be 20.
  • Byte 26-29: File size in bytes. LSB first.

Example:

FileFindFirst command has been performed to search for all the log files (*.log).
The response message included the file RobotAppStore1.log with handler id 0x04.
Now, let's see what other log files exist in the NXT's file system.

The command that is sent to NXT is:
0x16 0x00 0x01 0x87 0x00

The command that is received from NXT as a reply is:
0x1C 0x00 0x02 0x87 0x00 0x05 RobotAppStore2.log\0\0 0x04 0x00 0x00 0x00

abortion cost without insurance site post abortion







Related Robot-Apps™

Comments