Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9119493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:24:30+00:00 2026-06-17T05:24:30+00:00

Updated question: I am trying to connect to a terminal emulator using a library

  • 0

Updated question: 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 \n to the data to get to a new line when I pressed the enter key like so:

mEntry = (EditText) findViewById(R.id.term_entry);

mEntry.setOnEditorActionListener(new TextView.OnEditorActionListener() {

    @Override
    public boolean onEditorAction(TextView v, int actionId,
            KeyEvent event) {

        /* Ignore enter-key-up events. */
        if (event != null && event.getAction() == KeyEvent.ACTION_UP) {

            return false;
        }

        Editable e = (Editable) v.getText();
        String data = e.toString() + "\n";
        sendOverSerial(data.getBytes());
        TextKeyListener.clear(e);
        return true;
    }
});

And the write method:

public void write(byte[] bytes, int offset, int count) {

super.write(bytes, offset, count);

if (isRunning()) {
    doLocalEcho(bytes);
}
return;

}

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:

  private void doLocalEcho(byte[] data) {
 String str = new String(data);   
    appendToEmulator(data, 0, data.length);
    notifyUpdate();
}

 public void write(byte[] bytes, int offset, int count) {
    // Count the number of CRs
 String str = new String(bytes);
    int numCRs = 0;
    for (int i = offset; i < offset + count; ++i) {
        if (bytes[i] == '\r') {
            ++numCRs;
        }
    }

    if (numCRs == 0) {
        // No CRs -- just send data as-is
        super.write(bytes, offset, count);

        if (isRunning()) {
           doLocalEcho(bytes);
        }
        return;
    }

    // Convert CRs into CRLFs
    byte[] translated = new byte[count + numCRs];
    int j = 0;
    for (int i = offset; i < offset + count; ++i) {
        if (bytes[i] == '\r') {
            translated[j++] = '\r';
            translated[j++] = '\n';
        } else {
            translated[j++] = bytes[i];
        }
    }

   super.write(translated, 0, translated.length);

    // If server echo is off, echo the entered characters locally
    if (isRunning()) {
        doLocalEcho(translated);
    }
}

So that worked fine, now when I typed 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 \n there was an extra space between every newline as well as getting the extra newline.

https://i.stack.imgur.com/1pZaL.png

So I thought that when counting the number of carriage returns peek ahead at the next byte, and if it is '\n' don’t count it:

 for (int i = offset; i < offset + count; ++i) {

        if (bytes[i] == '\r' && 
                  ( 
                    (i+1 < offset + count) && // next byte isn't out of index 
                    (bytes[i+1] != '\n')
                  ) // next byte isn't a new line 
               ) 
        {
            ++numCRs;
        }
    }

This fixed the problem of the spaces…but that was rather stupid as I am now back in a circle to the original problem, if I type directly in the terminal there is no new line, as it sees the \r\n and sees the next byte is invalid. What would be the best way to get both working together? I either have these weird extra spaces and all input is fine, or normal spacing and I can’t enter text directly from the terminal, only the textbox. I assume it’s really easy to fix, I just am scratching my head looking at it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:24:31+00:00Added an answer on June 17, 2026 at 5:24 am

    EDIT: Not fixed, thought I had but then when I enter directly from the terminal enter does not produce a new line. It must be because the \n is ignored after the \r

    I have most of this fixed. When counting the number of carriage returns peek ahead at the next byte, and if it is '\n' don’t count it:

     for (int i = offset; i < offset + count; ++i) {
    
                if (bytes[i] == '\r' && 
                          ( 
                            (i+1 < offset + count) && // next byte isn't out of index 
                            (bytes[i+1] != '\n')
                          ) // next byte isn't a new line 
                       ) 
                {
                    ++numCRs;
                }
            }
    

    The only problem left now is that I still get the prompt back twice like this:

    switch#
    switch#
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is an updated part 2 of a question I asked earlier. I'm trying
Updated Question: $(this).attr(EmployeeId, 'A42345'); $.ajax({ type: POST, url: url, data: {EmployeeId: ' + id
This is somewhat of an updated question from a problem I had earlier. I
I've simple question. I'm trying to on duplicate key update supplier name and price
UPDATED QUESTION Since the ctor is not supported by .NETCF (public FileStream(IntPtr handle, FileAccess
Updated question, see below I'm starting a new project and I would like to
Updated question given Andrew Hare's correct answer: Given the following C# classes: public class
UPDATED QUESTION: Ok, I am going to simplify my question since I don't really
Updated question: Django is giving me the following sql query: SELECT auth_user.id, auth_user.username, auth_user.first_name,
Edit (updated question) I have a simple C program: // it is not important

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.