File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Android and the fly likes Strip extra \r\n's in stream? Android Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "Strip extra \r\n Watch "Strip extra \r\n New topic
Author

Strip extra \r\n's in stream? Android

Paul Gildea
Greenhorn

Joined: Jun 30, 2009
Posts: 18
I am trying to connect to a terminal emulator using a library in android, this will connect to a serial device and should show me sent/received data. I should be able to send data over the connection via a text box below the terminal or by typing in the terminal itself and hitting enter on the keyboard in both cases.

When I was sending data via textbox I had to append \r\n to the data to get to a new line when I pressed the enter key like so:


And the write method:


With this I got the prompt back twice from the terminal, which is wrong. When I was hitting enter after typing in the terminal session itself no new line was occurring at all. So I had to test the data for \r and replace it with \r\n


So that worked fine, now when I type in the terminal session itself and hit enter i got the newline I wanted. However now every time I send data from the text box with with \r\n there is an extra space between every new line as well as getting the extra newline. I assume \r\n is getting translated to \r\n\n and I'm not sure why I am getting the extra space between every line, so how do i change the code so that both ways of entering text generate the correct result?

Issues here are that as well as after I send data and am getting the terminal prompt back twice in all cases, for some reason I also get a space between every line. If I go back to the original write function I don't get the erroneous spaces but I get the prompt twice and typing in the terminal session itself does not work, no new line.

Original terminal getting prompt twice sending data from a text box, and when I write via terminal itself I don't get any new line.



Here is what the badly formatted terminal with the new write looks like, extra spaces, getting prompt twice when data is sent:



^^^^ Useless at java!
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Please CarefullyChooseOneForum. I have moved a thread to Android forum.


The quieter you are, the more you are able to hear.
Paul Gildea
Greenhorn

Joined: Jun 30, 2009
Posts: 18
Kemal Sokolovic wrote:Please CarefullyChooseOneForum. I have moved a thread to Android forum.

Just had it in java as it is java code in particular that is the problem, not the android API.
Thank you, won't make the mistake again
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3953
    
  17

When you are counting the number of CRs you could peak ahead at the next byte, and if it is '\n' don't count it:


Then do something similar when you are copying in the '\r\n' into the results.

On the other hand, Android is a Linux box, and you only need to use '\n'. Perhaps you should simply append '\n' to the TextView (not '\r\n'), and perhaps just replaces any '\r' you find in the write() method with '\n' (maybe with a peak ahead to see if the next character is a '\n' and if it is do nothing)


Steve
Paul Gildea
Greenhorn

Joined: Jun 30, 2009
Posts: 18
Steve Luke wrote:When you are counting the number of CRs you could peak ahead at the next byte, and if it is '\n' don't count it:


Then do something similar when you are copying in the '\r\n' into the results.

On the other hand, Android is a Linux box, and you only need to use '\n'. Perhaps you should simply append '\n' to the TextView (not '\r\n'), and perhaps just replaces any '\r' you find in the write() method with '\n' (maybe with a peak ahead to see if the next character is a '\n' and if it is do nothing)


Thanks a lot Luke I got it working with the help of your code, no more weird spaces, just the one extra newline somewhere as I get the prompt back twice like this after I send a command:

Switch#
Switch#


So i have an erroneous newline somewhere hmm!
Paul Gildea
Greenhorn

Joined: Jun 30, 2009
Posts: 18
I fixed the doubt prompt issue (stopped sending \r) now but the original problem is not actually fixed.

When I used your code it stopped the spaces, but now when I enter text from the terminal itself and hit enter...no new line appears. It must be because the \n is ignored after the \r.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3953
    
  17

You will have to examine a couple of the messages byte by byte -- perhaps send them to a Log -- and see what is being sent. Then do whatever translation you make and examining the results from that as well. Then you should be able to see what is coming in and how it is being transformed to make an educated change in your code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strip extra \r\n's in stream? Android
 
Similar Threads
copy paste java object using System clipboard
Copy and paste java objects using System clipboard
Help request on proxy for socket I/O
Reading file into byte array