Frage

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.

http://i.imgur.com/gtdIH.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.

War es hilfreich?

Lösung

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#
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top