Shop OBEX P1 Docs P2 Docs Learn Events
use of UARTs in the background — Parallax Forums

use of UARTs in the background

atekippeatekippe Posts: 15
edited 2004-11-12 14:31 in General Discussion
Does anybody know where I can get some more information on using the buffered serial communication in the background?· I know how to·set up the Uarts and use them in the main program to communicate with each other but am I missing something about the background that could speed up my program?

atekippe
ISU

Comments

  • atekippeatekippe Posts: 15
    edited 2004-11-10 16:25
    Also, during our continuous comminication with the device we get this error.

    [noparse][[/noparse]Error IDE-0028] Unhandled exception in JVM:java.lang.OutOfMemoryError.
    Exception thrown in file 'C:\Program Files Inc.\Javelin Stamp IDE\lib\lang\StringBuffer.java' at line 272

    Is there a way to clear the memory so it never reaches it's upper limit or another way to get around this?

    Here is the code we are running

    //
    Routine to receive entire responce in buffer
    static void bufferMessage(){
    c = 0xff;
    count = 0;
    while (count <1){
    //if(rxUart.byteAvailable()){
    c = (char)rxUart.receiveByte();
    if(c=='\n'){
    count=1;
    }
    buffer.append(c);

    }
    }


    //
    Main Program
    public static void main(){

    while (count < 101) {
    buffer.clear(); // clear buffer

    //
    Uart commands poling epoch4 for Peak Thickness
    txUart.sendString("PH? \r\n"); // ask for peak thickness

    bufferMessage(); // call receive subroutine

    if (count>99){
    System.out.println(count);
    System.out.println("out of loop");
    }

    else {

    x=buffer.length();
    count=0;
    while (count <x){
    l=(char)buffer.charAt(count);
    if(l==m){
    u=count;
    }
    if(l==n){
    v=count;
    }
    count=count+1;
    }
    if (x>1){
    buffer.delete(u-1,x); // deletes from buffer 6 to end of string
    buffer.delete(0,v+4); // deletes front 3 chars in buffer
    // System.out.println("
    Peak Thickness [noparse][[/noparse]in]
    ");
    String s = buffer.toString(); // prints to screen remaining chars
    System.out.println(s);
    }
    }

    atekippe
    ISU
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2004-11-10 18:04
    If you look at the StringBuffer class, you'll see that

    append() calls expand() that creates a new string if the

    buffer is too small. And because there is no garbage collection

    you eventuelly run out of memory.

    You can better use a simple char array plus index pointer to

    store c. This minimizes code and speeds up your application.

    static char[noparse]/noparse buffer = new char[noparse][[/noparse]100];

    static int index=0;

    static void readMessage() {

    · int c;

    · index=0; //start new message

    · while (true) {

    ··· while (!rx.byteAvailable()) ; //wait for input

    ··· buffer[noparse][[/noparse]index++] = (char)(c = rx.reaceiveByte());

    ··· if ((c == '\n') || (index>=100)) return;

    · }

    }

    regards peter
  • atekippeatekippe Posts: 15
    edited 2004-11-12 14:31
    Thanks peter,

    atekippe
    ISU
Sign In or Register to comment.