Overview

The C++ communication library makes it easy to communicate with the NXT using either a Bluetooth or network connection. To be able to communicate with the NXT over Bluetooth a partnership between the PC and the NXT has to be established. Once a partnership is established you should be able to see which Com-port the NXT uses. Please follow this link if you don't know how to do this. To communicate with the NXT over a network you need to download and run the NXT network server. Below you will find documentation and samples for both Bluetooth and network communication. If you find bugs or have any questions or comments fell free to drop me an email.

The C++ communication library

The library can be used with most C++ compiler on a Windows-box - also the library can be used with Pocket PCs running Windows Mobile using MS Visual Embedded C++. With the library it is possible to:

  • Open and close connections with multiple NXT units
  • Control the motors
  • Send and receive messages using the mailbox system
  • Use standard LEGO Sensors including sound and sonar sensors
  • Set Brick name, get battery level, read firmware version etc.
  • Play a tone
  • Play sound files
  • Use the NXT's file system
  • Download and upload files
  • Start and stop on-brick programs
  • Use Compass, Gyro, Color and Tilt sensors from Hitechnic
  • Communicate with I2C sensors
  • Use the I2C PCF8591 A/D converter
  • Use the I2C PCF8574 I/O Chip
  • Use exceptions to catch sensor and connection errors
  • Generic connection classes which makes it easy to switch between either Bluetooth or network communication
  • and more...

Download

Version 2.0 (uploaded April 6th, 2009)

  • Added support for network communication using the NXT network server
  • Added missing implementation of delete flash memory
  • Added network and server errors to the error list
  • Renamed errors in the error list
  • Grouped errors in the error list
  • Added error type enumeration and get_error_type function to the Nxt_exception class
  • and more...

Version 1.1 (uploaded January 26th, 2009)

  • Added sub classes for analog sensors (rotation, temperature ect.)
  • Added/updated samples
  • Library documentation
  • Renamed some of the struct (sorry for that)
  • Removed the read(unsigned int return_value) in sensor.h
  • All ADC ports on the PCF8591 chip are now read with a single call

Version 1.1 for linux by Christoph Sprenger (uploaded May 3th, 2009)

Same features as above. This is still work in progress. Sprenger's intension was to have one version that could work on both Windows and posix systems. However this is not the case yet - but maybe in future releases. If anyone out there would like to finish the job please let me know

Getting started

To get started you need a C++ compiler. I would recommend Code Blocks version 8.02 with GCC compiler and GDB debugger from MinGW. Install Code blocks and do the following (click here for step by step image):

  • Download and unzip the C++ library to a folder of your choice
  • Create a new console C++ project in the same folder
  • Add all the files from the library to your project
  • If you want to communicate over a network remember to link to libwsock32.a - otherwise delete/remove network.h and network.cpp from your project. In Code blocks this is done using project->build option->linker setting->add. The file libwsock32.a is located in the folder (Code Blocks install directory)/MinGW/lib/ (click here for step by step images)

To write your fist Bluetooth program simply copy the code below into your main file - remember to change the comport!!. The program will make motor B turn when the touch sensor on port 1 is pressed.

touch_motor.cpp
#include <cstdlib>
#include <iostream>
#include <string>
#include <conio.h>
#include "nxt.h"
using namespace std;

//set up the NXT
Connection *connection = new Bluetooth();
Sensor *sensor1 = new Touch(IN_1, connection);
Motor *motorB = new Motor(OUT_B, connection);
int main()
{
  try{
    cout << "Try to connect to the NXT" << endl;
    connection->connect(40);
    cout << "Connected" << endl;
    cout << "Press touch sensor to make motor B turn - hit any key to end" << endl;
    while(!_kbhit()){//hit a key to end
      if(sensor1->read()){
        motorB->on(75);
      }
      else{
        motorB->stop();
      }
    }
    connection->disconnect();
  }
  catch (Nxt_exception& e){
    //some error occurred - print it out
    cout << e.what() << endl;
    cout << "error code: " << e.error_code() << endl;
    cout << "error type: " << e.error_type() << endl;
    cout << e.who() << endl;
    connection->disconnect();
  }
  return 0;
}

If you have compiled and run the above program with success use the samples below to start writing your own programs. To view a complete list of available commands and sensor opportunities please consult the library documentation.

Once you are able to write consol program you might want to create GUI programs just like the Vehicle Remote. There are a lot of RAD tools around that lets you create good looking GUI programs together with the C++ library. I personally use CodeGear/Borland C++ builder but it also possible to use Code Blocks to create GUI programs. Ramacco used QT, Code Blocks and the C++ library to create the small remote shown to the right. Please visit his website for more information and source code.

Sample programs

Link Description
Sample1 Get readings from the four ADC ports on the PCF8591 8-bit ADC
Sample2 Read color with the HiTechnic color sensor
Sample3 Read the direction with the HiTechnic compass sensor
Sample4 Upload a file from the PC to the NXT and download the same file
Sample5 Print brick info including firmware-version, brick name ect.
Sample6 Read angular acceleration with the HiTechnic gyro sensor
Sample7 Read and write from/to the PCF8574 8-bit I/O chip
Sample8 Get sensor reading from the light sensor and turn the LED on and off
Sample9 Read and write data to/from the NXT's mailbox - run this NXC code on the NXT (download compiled NXC file)
Sample10 Make the motor turn, stop and coast
Sample11 Retrieve a file list from the NXT and print it
Sample12 Get sensor readings from the RCX light sensor
Sample13 Get tick count from the RCX rotation sensor
Sample14 Read distance with the Sonar (ultrasonic) sensor
Sample15 Read sound level with the sound sensor
Sample16 Start and stop a program on the NXT
Sample17 Play a sound file on the NXT
Sample18 Read the temperature with the RCX temperature sensor
Sample19 Get the x y z coordinates with the HiTechnic tilt sensor
Sample20 Determine whether or not the touch sensor is pressed
Sample21 Motor B turns when the touch sensor on port 1 is pressed
Sample22 Make the user select the sensor type and print the sensor value
Sample23 Same as sample 20 but with a network connection. Be sure to download and run the NXT network server

Documentation

The library documentation provides a complete list of available commands and features not necessarily shown in the samples above. Also it contains a list of errors that might be thrown whenever communication fails or the NXT reports an error.

License

There is no license for the files/software - which I guess means that you are free to sell it to your friends give it to your dad, make a fortune... do whatever you like as long as you give me credit.