Displaying Serial inputs in G4P TextArea.

edited November 2015 in Library Questions

Hi,

I am new to G4P and now try to display ASCII line inputs in TextArea using code below. appendText works but I like to use the insertText method with newline addition . I then get the IndexOutofBound exeption error. What am I doing wrong?

import g4p_controls.*;
import processing.serial.*;

public static final short portIndex = 0;  
public static final char LF = 10;     
public static final char CR = 13;     
Serial myPort;     
GTextArea myTxArea; 
String myString="";

public void createGUI()
{
  G4P.messagesEnabled(false);
  G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
  G4P.setCursor(ARROW);
  surface.setTitle("Sketch Window");
  myTxArea = new GTextArea(this, 133, 2, 354, 262, G4P.SCROLLBARS_VERTICAL_ONLY);
  myTxArea.setOpaque(true);
}

public void setup()
{
  size(480, 320, JAVA2D);
  createGUI();
  println(" Connecting to -> " + Serial.list()[portIndex]);
  myPort = new Serial(this,Serial.list()[portIndex], 115200);
  myString = myPort.readStringUntil(LF);  
  myString = null;
  myTxArea.appendText("");
}

public void draw()
{
  background(230); 
  if ( myPort.available() > 0) 
  { 
      myString = myPort.readStringUntil(LF);
      if (myString!=null)
        //myTxArea.appendText(myString);
         myTxArea.insertText(myString,true,false);  
  }
}

Answers

  • Try

      if (myString!=null && myString.length() > 0)
         myTxArea.insertText(myString, false, true);  
    
  • Sorry but now the error changed to String Index out of bounds. If I use false,true instead all lines are displayed in textarea but without any linefeeds beetween.

  • OK go back to true false and then try

    if (myString!=null && myString.length() > 0)
       myTxArea.insertText(myString + "\n", true, false); 
    
  • Now I get sequential display of all lines again without any effect of the extra \n character. Also I cannot get any effect of the boolean operators in the insertText methode.

  • I changed the code myString = myPort.readStringUntil(CR); if ((myString!=null)&&(myString.length()>0)) { Pos=Tbox1.getCaretPos(); println(Pos[0]+" "+Pos[1]); Tbox1.moveCaretTo(15,0); Tbox1.appendText(myString); }to check caret position and found that the moveCaretPos was not working. This is probably one reason causing the problems. Is there a setting in TextArea to change that?

  • I will have to run some tests but the original purpose of the GTextArea control was for multiline keyboard input. It was never intended as a data-logger and doesn't behave well when appending or inserting text multiple times. The appendText and insertText were added at the request of a previous user.

    In your example code the moveCaretTo is overridden by the appendText which moves the caret.

  • BTW you can see that it is possible to format your code in the forum to show line numbers. It would be useful for you to learn how to do it. Click on discussions and you will see a discussion about formatting text and code for the forum. I corrected your original post for you. :)

  • Ok, now I know - thanks!

Sign In or Register to comment.