Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unexpected leading characters in buffered reader

 
John Hagan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am an amateur programmer programming for the Lego NXT robot using Lejos. I say this because some of the methods/classes I am using may be unique to Lejos. I have two programs interacting. The first is on a W7 PC. It runs from the command line in a console. It is called PC_BT_Connect. The second program (BTReceiveTest) is a blue tooth listener running on the robot, which also runs java. I begin by starting the listnener on the robot. Then I start the command line program which seeks out and connects successfully. I enter characters a line at a time and send them via the bluetooth connector. The robot receives them and prints the line to its screen. Then it turns the string around and sends it back to the PC where it is output to the console. This all works fine, but I am picking up a random first character that gets posted to the PC console on the return message. Can you help me find out where this is coming from?



This is the robot listener:



This is the output:



--John
 
Stephan van Hulst
Bartender
Pie
Posts: 5432
52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you constantly opening a new DataInputStream on btc?
 
Winston Gutkowski
Bartender
Pie
Posts: 10111
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Hagan wrote:...but I am picking up a random first character that gets posted to the PC console on the return message. Can you help me find out where this is coming from?

I honestly don't know, but I wonder if it might be a byte order mark.

I've never used Lego, but from a quick glance at the NXTComm docs (I assume I'm looking at the right one), it doesn't appear to require a DataInputStream; and since you're only sending text, I'm wondering why you chose it.
If it were me, I think I'd want to use a BufferedReader, possibly with a specific Charset like UTF-8. That way, you don't have to write your own readline() method, because BufferedReader already has one, and BOMs (if sent/received) would be handled internally.

However, as I say, I've never used Lego, so there may be other things going on that I don't understand.

Winston
 
John Hagan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan, thanks for pointing out the fact that I had the streams defined in the wrong place. Winston, I had to go back and relearn input and output streams and stream readers. The leading character did not disappear until I eliminated all of the Data input/output streams. Thanks for helping me clean up my code. I post the updated code and output below for your review and comments.

the code from the W7 PC side:


the code for the NXT robot side:

This is the output on the windows console:


Thanks again. --John
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic