COM_OpenNXTEx

Opens USB or Bluetooth connection to NXT; advanced version, more options

Contents

Syntax

handle = COM_OpenNXTEx('USB', UseThisNXTMAC)

handle = COM_OpenNXTEx('Bluetooth', UseThisNXTMAC, inifilename)

handle = COM_OpenNXTEx('Any', UseThisNXTMAC, inifilename)

handle = COM_OpenNXTEx('USB' , UseThisNXTMAC, 'MotorControlFilename', motorcontrolfile)

handle = COM_OpenNXTEx('Bluetooth', UseThisNXTMAC, inifilename, 'MotorControlFilename', motorcontrolfile)

handle = COM_OpenNXTEx('Any' , UseThisNXTMAC, inifilename, 'MotorControlFilename', motorcontrolfile)

Description

This function establishes a connection to an NXT brick and returns the handle structure that has to be used with NXT-functions (you can call COM_SetDefaultNXT(handle) afterwards for easier use).

For a more convenient way to open an NXT handle with less parameters, the function COM_OpenNXT is provided.

Different types of connection modes are supported. In all modes, you can set UseThisNXTMAC to a string with the NXT's MAC address (serial number). A connection will then only be estabslished to a matching NXT brick. This can be useful for programs with multiple NXT devices. Set it to an empty string '' to use any NXT available (usually the first one found). The string can have the format '001612345678' or '00:16:12:34:56:78'.

handle = COM_OpenNXTEx('USB', UseThisNXTMAC)

This will try to open a connection via USB. Device drivers (Fantom on Windows, libusb on Linux) have to be installed.

handle = COM_OpenNXTEx('Bluetooth', UseThisNXTMAC, inifilename)

Uses Bluetooth as communication method. A valid inifile containing parameters like the COM-Port has to be specified in inifilename. To create an inifile with Bluetooth settings, the function COM_MakeBTConfigFile is available.

Note that as of right now, the parameter UseThisNXTMAC will be ignored for Bluetooth connections until implemented in a future version.

handle = COM_OpenNXTEx('Any', UseThisNXTMAC, inifilename)

This syntax combines the two parameter settings from above. inifilename has to be given. The function will try to locate an NXT device on the USB bus first. If this fails for some reason (no USB connection to the NXT available, no device drivers installed, or NXT device is busy), the function will silently try to establish a connection via Bluetooth.

The advantage is that this version works with both Bluetooth and USB connections without changing any code. Plug or unplug the USB cable to switch between connection types...

The optional string-parameter 'MotorControlFilename' can be used to override the default file name for the embedded NXC program MotorControl, which will be launched by the method. Specify 'MotorControlFilename', followed by a the actual filename to be started in motorcontrolfile. You can set this to any executable file present on the NXT. The filename conventions are the same as for NXT_StartProgram. Set it to an empty string '' to disable the embedded MotorControl program. In this case, the class NXTMotor will not completely be available for usage. This option is intended for advanced users.

Limitations of COM_CloseNXT

If you call COM_CloseNXT('all') after a clear all command has been issued, the function will not be able to close all remaining open USB handles, since they have been cleared out of memory. This is a problem on Linux systems. You will not be able to use the NXT device without rebooting it. Solution: Either use only clear in your programs, or you use the COM_CloseNXT('all') statement before clear all. The best way however is to track your handles carefully and close them manually (COM_CloseNXT(handle)) before exiting whenever possible!%

Examples

   myNXT = COM_OpenNXTEx('Any', '001612345678', 'bluetooth.ini');
   % This will connect to an NXT device with the MAC/serial number 001612345678,
   % first trying via USB. If this fails (no drivers installed or no matching USB
   % device found), a connection via Bluetooth will be established, using
   % the paramters found in the given config file.
   myNXT = COM_OpenNXTEx('USB', '', 'MotorControlFilename, 'MyProgram.rxe');
   % This will try to connect to an NXT device via USB, using the first
   % one found (we set |UseThisNXTMAC| to |''|). Instead of the embedded
   % default MotorControl program, a custom user file MyProgram.rxe will
   % try to be launched...

See also

COM_OpenNXT, COM_CloseNXT, COM_MakeBTConfigFile, COM_SetDefaultNXT,

Signature