saving sensor data to text file for later use on PC
Page 1 of 1

Author:  Lego Geek [ Sun Nov 14, 2010 10:43 am ]
Post subject:  saving sensor data to text file for later use on PC


I having been trying to save sensor data (ie ultrasonic, color, light) but all I get is a file with garbage in it. Can someone post a short sample program showing how to save a number to a text (txt or dat) file that can then be uploaded to a PC?

I can do all sorts of data manipulation and formating to the data before displaying it on the NXT LCD screen. But, every time I try and save the data to a file it does not work. Also, some times the file will not even upload. I am not sure why but I think the problem is with how the file is created, data is written to it, or not properly closed when the program exits.

Any help would be very much appreciated.


Author:  david fort [ Sun Nov 14, 2010 10:38 pm ]
Post subject:  Re: saving sensor data to text file for later use on PC

This is an easy way on the saving data side:
task main()
nDatalogSize = 200; // clear it out too.

Then you can upload the datalog file from the File Management dialog on the Robot->NXT Brick menu.
The file will be named DATA0001.rbt or similar.

That was the easy part. The hard part is that the file is in "binary" not "text" (ignore for now that text is also stored as binary).
so the bytes of the file will look like this:
C:\Documents and Settings\New User\Desktop>debug data0001.rdt
138E:0100 09 00 00 D5 00 00 00 00-FF 47 00 41 3B 03 41 3F .........G.A;.A?
138E:0110 03 41 30 03 41 3E 03 41-33 03 41 30 03 41 56 03 .A0.A>.A3.A0.AV.
138E:0120 41 6E 03 41 83 03 41 A7-03 41 CA 03 41 E1 03 41 An.A..A..A..A..A
138E:0130 0C 00 41 1A 00 41 2C 00-41 23 00 41 E8 03 41 BC ..A..A,.A#.A..A.
138E:0140 03 41 73 03 41 4A 03 41-44 03 41 40 03 41 45 03 .As.AJ.AD.A@.AE.
138E:0150 41 4D 03 41 4F 03 41 52-03 41 67 03 41 6A 03 41 AM.AO.AR.Ag.Aj.A
138E:0160 74 03 41 8F 03 41 AE 03-41 BA 03 41 80 03 41 48 t.A..A..A..A..AH
138E:0170 03 41 2C 03 41 3B 03 41-3B 03 41 6B 03 41 89 03 .A,.A;.A;.Ak.A..
You can look at the datalog on the NXT using Robot->Debug Windows->Datalog.
From that, I know my example had 71 samples and the first few were
In hex, those numbers are:
Samples: 0047
so you can see at the top of the file, there is some header info,
ending with the sample count, then each data point is entered after
a byte 41. I'm guessing the 41 (which is ascii for 'A') identifies which
Datalog "type" you used, and 41 is what you get if you don't specify it.
It has been a while since I used the datalog for anything, but as I recall,
if you wanted to store say, all 3 axis of an accelerometer in the datalog,
you could do that using
and that would put a different value before your number in the datalog.

Then comes the hard part:
Write a program that converts the file to something most humans can read.
I like to convert it to CSV, then import to excel or something like that.

I probably have such a program, but can't find it right now.
perhaps on other computer.

I am also fond of
writeDebugStreamLine("Xcomponent: %d",SensorValue[S2]);
and then look at it in the Robot->Debug Windows->Debug Stream.

Often I'll print out several values on a line, then just cut and paste from there into a file
and import to excel.

(I just tried the above, and it doesn't work - lots of extra stuff and no sign of my stuff.
I see there is a new version of RobotC recently announced, perhaps there are some
changes of use).

And if you are thinking, "I can't find any of these menu choices", then
Window->menu level->superuser
(maybe expert is good enough. not sure).

If none of the above strikes you as a good idea, you can also write to a data file using the File Access section, but if you want it in ASCII, then start by looking for an example of how to do it, Sample Programs\NXT\NXT Feature Samples looks pretty good.
I think Nxt File IO Test.c is the one I started with.

Good luck,
David Fort
Mentor, Team 1001

Author:  Lego Geek [ Mon Nov 15, 2010 11:13 am ]
Post subject:  Re: saving sensor data to text file for later use on PC

Hello David:

I have been looking into the Datalog feature but I am still stuck with the file being in a binary format. What I would really like to do is have it saved as a ASCII format write from the start. I did look at the NXT File IO Test.c file. I was able to remove the parts that dealt with reading the file and have just the writing the file. It was when I tried altering and using the file into my program that I ran into problems.

I will try the datalogging approach tonight. If you, or someone could rework the NXT File IO Test.c code to save long integer values that would be very much appreciated.


Author:  Aswin [ Mon Nov 15, 2010 11:47 am ]
Post subject:  Re: saving sensor data to text file for later use on PC

Hi, if you have a Bluetooth dongle you can also try my utility NXT2excel. This sends data directly to excel. No worries about writing to files or uploading. Also you have real time access to your data.
You'l find it here:

Author:  Lego Geek [ Mon Nov 15, 2010 1:51 pm ]
Post subject:  Re: saving sensor data to text file for later use on PC

Hello Aswin:

I have also looked into using your little program. The problem is that the robot will be mobile and being used outside. So, I really need the solution to be on the robot.

I will try your program over the next few nights. Hopefully it can get me some data to use for debugging.


Author:  Lego Geek [ Wed Nov 17, 2010 12:43 am ]
Post subject:  Re: saving sensor data to text file for later use on PC

After much playing around I managed to get the write subroutines from the "NXT Text File IO.c" to work with my program. I have posted the program so that others can use it. Also, I hope those of you better skilled at programming will also have a look and suggest any programming improvements. Please also note the the sensor I am trying to get the data for is the soon to be release Dexter Industries GPS sensor. Xander has created drives for the GPS sensor and they are part of his latest drivers release.

Thank you for all your help.

DGPS-test-file.c [3.72 KiB]
Downloaded 657 times

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group