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
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
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.
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.