BT connection - byte order

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

BT connection - byte order

Postby simakasp » Sat May 29, 2010 2:32 pm

Hi

I'm working on a project where a lot of data processing isn't done on the NXT, but on a remote PC.

At the moment I succesfully established a BT connection between the NXT and my remote program. Now I have a few questions about the data exchange, especially the byte order.

I'm using a java.io.DataOutputStream to send the first integer value of my protocoll header to the PC, a simple "1" at the moment, and receive a 16777216 when I call readInt() on my remote DataInputStream.
16777216 is 01000000 in hex, so it looks like the program on the NXT uses Little Endian. Am I right?
That's confusing, because I always thought Java uses Big Endian on every platform.
Now I could simply convert the 4 bytes to network byte order on the next by hand, but I wonder if there isn't a more elegant way to do it.

Is there some OutputStream that automatically converts data to network byte order?
I also stumbled upon a build.xml in the net, which included a parameter for the "endian-ess". So is it possible to compile my next program in a way that big endian is used instead of little endian? (with Eclipse)


Sorry for beeing so detailed, but I hope to be not the only dumb person struggling with this topic, and so the search engine of this site has a little to spit out in the future.
simakasp
New User
 
Posts: 3
Joined: Sat May 29, 2010 1:50 pm

Re: BT connection - byte order

Postby skoehler » Sat May 29, 2010 2:43 pm

DataInputStream and DataOutputStream use BigEndian, AFAIK.

One side (either PC or NXT) is doing things wrong. Are you using DataOutputStream on the NXT and DataInputStream on the PC side?

Have you tried sending for complex data? (For example 0x11223344)
skoehler
leJOS Team Member
 
Posts: 1458
Joined: Thu Oct 30, 2008 4:54 pm

Postby gloomyandy » Sat May 29, 2010 2:49 pm

If you use Date Streams on both ends they should just work. How are you establishing the connection? are you using the leJOS classes on the PC side? leJOS by default uses a packet header on Bluetooth connections, so if you are not using the leJOS classes than you need to either add this yourself, or use RAW mode on the NXT when establishing the connection... You may also want to take a look at the sample programs provided with leJOS, they show how to send/receive ints using a Data Stream.

Also it helps when you ask questions like this if you tell us about your environment...
1. What version of leJOS
2. What PC side operating environment (Mac, Linux, Windows), version etc.

To answer your byte order question take a look at the leJOS source code (you have it as part of the install). Here is the online version....
http://lejos.svn.sourceforge.net/viewvc ... iew=markup
As you can see it is big endian...

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4240
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby simakasp » Sat May 29, 2010 2:50 pm

I use a java.io.DataOutputStream on the NXT.

NXT
Code: Select all
// members
private NXTConnection            m_Connection;
private java.io.DataOutputStream m_Out;
   
// init()
this.m_Connection  = Bluetooth.waitForConnection();
this.m_Out         = this.m_Connection.openDataOutputStream();

// writeTest(int i)
this.m_Out.write(i);


*edit*

Remote
Code: Select all
private lejos.pc.comm.NXTComm      m_Comm;
private java.io.InputStream        m_InputStream;
private java.io.DataInputStream    m_DataInputStream;

this.m_Comm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
NXTInfo[] info = this.m_Comm.search("myNXTName", NXTCommFactory.BLUETOOTH);
this.m_Comm.open(info[0]);
this.m_InputStream = this.m_Comm.getInputStream();
this.m_DataInputStream = new java.io.DataInputStream(this.m_InputStream);
[...]
int   type = this.m_DataInputStream.readInt();


Lejos is v.0.8.5 an the remote program is running on a windows machine.
simakasp
New User
 
Posts: 3
Joined: Sat May 29, 2010 1:50 pm

Postby skoehler » Sat May 29, 2010 3:11 pm

simakasp wrote:
Code: Select all
// members
private NXTConnection            m_Connection;
private java.io.DataOutputStream m_Out;
   
// init()
this.m_Connection  = Bluetooth.waitForConnection();
this.m_Out         = this.m_Connection.openDataOutputStream();

// writeTest(int i)
this.m_Out.write(i);



Try using writeInt(i) instead of write(i).
skoehler
leJOS Team Member
 
Posts: 1458
Joined: Thu Oct 30, 2008 4:54 pm

Postby simakasp » Sat May 29, 2010 3:34 pm

skoehler wrote:Try using writeInt(i) instead of write(i).


That could be the point.
I'm going to test it on monday, so long have a nice weekend and thanks for your help.
simakasp
New User
 
Posts: 3
Joined: Sat May 29, 2010 1:50 pm

Postby skoehler » Sat May 29, 2010 4:28 pm

simakasp wrote:
skoehler wrote:Try using writeInt(i) instead of write(i).


That could be the point.
I'm going to test it on monday, so long have a nice weekend and thanks for your help.


Actually, I don't fully understand what's going on, because readInt() should have blocked or thrown an EOFException. write(1) only writes ONE BYTE, and not four bytes. So it actually shouldn't have worked at all, or you only posted a fraction of the source. Nonetheless, calling write() and readInt() is 100% wrong.
skoehler
leJOS Team Member
 
Posts: 1458
Joined: Thu Oct 30, 2008 4:54 pm


Return to NXJ Software

Who is online

Users browsing this forum: Baidu [Spider], Google [Bot] and 3 guests

more stuff