Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

\r\n issue over serial.

Options
  • 07-01-2013 4:11pm
    #1
    Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,082 Mod ✭✭✭✭


    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.

    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.


Comments

  • Registered Users Posts: 31 angry


    Have you tried using the system property "line.separator"? I'm not sure if this will solve the issue, but may be worth a try...


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,082 Mod ✭✭✭✭Tar.Aldarion


    Thanks that was a good idea, but yields the same functionality. Very annoying.


  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    Do you have the peek logic also in the translation for-loop?

    Are you new-line endings consistent? It sounds like you append \n for the textbox (which wouldn't have placed an \r would it?) and \r\n for directly in the terminal?


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,082 Mod ✭✭✭✭Tar.Aldarion


    I didn't put it in the translation loop because if the code gets that far it is already known that \n does not follow the \r due to an earlier condition...I believe.

    At the moment if I append \r or \n or System.getProperty("line.separator"); to the edittext I get correct functionality for the edittext send and no new line for the terminal.
    if I send \r\n I get an extra space between lines, no new line for terminal, if I don't append and \r or \n i get no new line for either. So I know I'm just not being consistent somewhere


  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    Well it seems to me that if there was a string like

    something\r\n\r

    It would count 1, but put the \n after the first \r. Of course, I don't know how you would end up with these kind of inputs.

    It seems to me (and if I understand you correctly) that if you get a newline with a single \r, and you get newline with a single \n, then it's only natural you are going to get a 2 new lines with \r\n.

    You need to know what the terminal sends and expects, and what the serial device sends and expects.


  • Advertisement
  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,082 Mod ✭✭✭✭Tar.Aldarion


    Yeah I'll have to do a lot more testing on that I suppose. I wish the log said what it was receiving instead of blank spaces. I could throw in some random tests like, if \n is sent and \n+1 is \r ignore the \r and so on. What I don't get that is that in the original I could input on the terminal fine, but if I sent \r or \n or lineSeparater via edittext they gave the extra space between lines. But if I send none of them I don't get any newline. So I am always getting 0 or 2 newlines. I'll go back to that version and work it from there.


Advertisement