NXC and C programming communication

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
delpenton
Posts: 2
Joined: 01 Nov 2010, 18:02

NXC and C programming communication

Post by delpenton »

I am running a cpu intensive algorithm written in C on my pc. The program will get inputs from the NXT, and the pc will compute and provide the result back to it. I thought about using bluetooth for this purpose. Is there an NXT-like bluetooth library that I can use in C to communicate with the NXT? Any other communication protocol for which an NXT and C library exist?
kizzard
Posts: 7
Joined: 23 Oct 2010, 15:11

Re: NXC and C programming communication

Post by kizzard »

If you are OK with C++: http://www.norgesgade14.dk/bluetoothlibrary.php
It did not compile straight away for me with gcc, I had to modify the code (mainly syntactic errors that are probably compiler dependent - removing namespace qualifiers and such) but it does work perfectly.

It works out of the box with nxc (after tweaking it to actually compile) and I even got it working with lejos after some trial and error.

There might be better solutions but that is the only one I have found so far.
physics-matt
Posts: 76
Joined: 29 Sep 2010, 06:57

Re: NXC and C programming communication

Post by physics-matt »

Again, using C++, you can also use the fantom library supplied by Lego. You need to download the software development kit from their website.

It's a C++ library for communicating with the NXT and I've used it on numerous projects where I'm doing intense programming on the PC. I've started writing a tutorial for setting it up with Visual Studio if that helps: https://sites.google.com/site/mattsrobo ... nxt-from-c

I've also written a wrapper class (NXTController) to make simple communication easier, which you can download from here:
https://sites.google.com/site/mattsrobo ... ects=0&d=1

It's not terribly well documented I'm afraid, but there is some example code which ought to help. Essentially, it allows you to write something like this in C++

Code: Select all

NXTController nxtc;
nxtc.SendMessage( 0, 0, "hello nxt" );
This will send a message to mailbox 0 of the nxt, which NXC can pick up with

Code: Select all

ReceiveMessage(0, true, buffer);
Let me know if you find it useful!

Matt
linusa
Posts: 228
Joined: 16 Oct 2010, 11:44
Location: Aachen, Germany
Contact:

Re: NXC and C programming communication

Post by linusa »

First of all, there are two things to distinguish: kizzard and physics-matt described ways to talk to the NXT via direct commands / the Fantom protocol. Once you've done that, your C-Program controls the NXT. All those libraries are fine...

A subset of this is basically a method to pass string messages between NXC and C programs. I used to describe how in my weather tutorial, I found this remaining link here: https://webcache.googleusercontent.com/ ... =firefox-a

There is a similar / better tutorial, focusing on Python. Unfortunately I couldn't find it on google right now (I think it was by some French person?). It was very good, sorry for not being able to post it right now.

Anyway, if you just want to pass strings between NXC and C, that's probably the way to go...
RWTH - Mindstorms NXT Toolbox for MATLAB
state of the art in nxt remote control programming
http://www.mindstorms.rwth-aachen.de
MotorControl now also in Python, .net, and Mathematica
linusa
Posts: 228
Joined: 16 Oct 2010, 11:44
Location: Aachen, Germany
Contact:

Re: NXC and C programming communication

Post by linusa »

FYI: I re-posted that google cache entry to the forums, here: https://sourceforge.net/apps/phpbb/mind ... ?f=3&t=181
RWTH - Mindstorms NXT Toolbox for MATLAB
state of the art in nxt remote control programming
http://www.mindstorms.rwth-aachen.de
MotorControl now also in Python, .net, and Mathematica
schodet
Posts: 139
Joined: 29 Sep 2010, 11:21
Contact:

Re: NXC and C programming communication

Post by schodet »

linusa wrote:There is a similar / better tutorial, focusing on Python. Unfortunately I couldn't find it on google right now (I think it was by some French person?). It was very good, sorry for not being able to post it right now.
Are you talking about this: Exchange messages with a NXT program?

Google do not like me.
LEGO things http://ni.fr.eu.org/lego/ - NXT Improved Firmware (GCC) http://nxt-firmware.ni.fr.eu.org/ - Other robots http://apbteam.org
linusa
Posts: 228
Joined: 16 Oct 2010, 11:44
Location: Aachen, Germany
Contact:

Re: NXC and C programming communication

Post by linusa »

schodet wrote:
linusa wrote:There is a similar / better tutorial, focusing on Python. Unfortunately I couldn't find it on google right now (I think it was by some French person?). It was very good, sorry for not being able to post it right now.
Are you talking about this: Exchange messages with a NXT program?

Google do not like me.
Yes, that was excactly what I meant. Strang I couldn't find it. Thanks
RWTH - Mindstorms NXT Toolbox for MATLAB
state of the art in nxt remote control programming
http://www.mindstorms.rwth-aachen.de
MotorControl now also in Python, .net, and Mathematica
schodet
Posts: 139
Joined: 29 Sep 2010, 11:21
Contact:

Re: NXC and C programming communication

Post by schodet »

linusa wrote:Yes, that was exactly what I meant. Strange I couldn't find it. Thanks
I suppose Google thinks there is not enough link to my page.
LEGO things http://ni.fr.eu.org/lego/ - NXT Improved Firmware (GCC) http://nxt-firmware.ni.fr.eu.org/ - Other robots http://apbteam.org
delpenton
Posts: 2
Joined: 01 Nov 2010, 18:02

Re: NXC and C programming communication

Post by delpenton »

Thanks for your answers. That wrapper library looks perfect for what I need. To that end I downloaded the Lego bluetooth SDK and I've been trying to build the main.cpp example using mingw since my VC++ express edition expired. I was able to compile it after I tweaked the fantom/platform.h file to support gcc under windows but now I'm getting link errors with fantom.lib probably due to name mangling and library format differences as expected:

C:\Users\delpenton\AppData\Local\Temp\ccEsFsTz.o:main.cpp:(.text+0x63): undefined reference to `_imp___ZN10nFANTOM1004iNXT17createNXTIteratorEtmRNS_7tStatusE'
C:\Users\delpenton\AppData\Local\Temp\ccEsFsTz.o:main.cpp:(.text+0xb4): undefined reference to `_imp___ZN10nFANTOM1004iNXT18destroyNXTIteratorEPNS_12iNXTIteratorE'
C:\Users\delpenton\AppData\Local\Temp\ccEsFsTz.o:main.cpp:(.text+0x511): undefined reference to `_imp___ZN10nFANTOM1004iNXT10destroyNXTEPS0_'
C:\Users\delpenton\AppData\Local\Temp\ccEsFsTz.o:main.cpp:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

Is there a way to convert this .lib file to the .a library format used by gcc? Thanks,

depelton
linusa
Posts: 228
Joined: 16 Oct 2010, 11:44
Location: Aachen, Germany
Contact:

Re: NXC and C programming communication

Post by linusa »

delpenton wrote:Thanks for your answers. That wrapper library looks perfect for what I need. To that end I downloaded the Lego bluetooth SDK and I've been trying to build the main.cpp example using mingw since my VC++ express edition expired.
Ok, fine that you got yourself "up and running", and I see you seem to know what you're doing. But just a word: This Fantom SDK is one of the the lowest levels possible. It's basically "raw access". It's not what I would describe as "wrapper library", just to let you know. Those libraries I (or the others) were referring to took the work of implementing the Fantom functions from you. Maybe they can be compiled using gcc? Look for NXT++, aNXT, libnxt, and similar things (check the english Wikipedia on NXT, there is a huge list).
RWTH - Mindstorms NXT Toolbox for MATLAB
state of the art in nxt remote control programming
http://www.mindstorms.rwth-aachen.de
MotorControl now also in Python, .net, and Mathematica
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest