Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

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

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Bartender
Posts: 825
5
Python Ruby Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please CarefullyChooseOneForum. I have moved a thread to Android forum.
 
Paul Gildea
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Paul Gildea
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
His brain is the size of a cherry pit! About the size of this ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic