Results 1 to 3 of 3
  1. #1
    azibhai is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default Help me for the following

    I am using jdk1.5.0_18 and I have code to read data from a weighing machine .
    I can read the data successfully . But there is problem while reading data the output which I am getting is repeatedly running .Is there any way to stop while getting the output and again read while the data is changed?

    My codes:


    case SerialPortEvent.DATA_AVAILABLE:
    {
    System.out.println("data available...");
    String weight_Array[]=new String[8];
    int c,i=0;
    try{
    while((ins.available())>0)
    {
    if((ins.available())==0)
    break;
    else
    {
    c=(char)ins.read();
    char ch=(char)c;
    weight_Array[i]=Character.toString(ch);
    i++;
    }
    }
    System.out.println("out of while");
    for(int j=0;j<=8;j++)
    {
    System.out.print(weight_Array[j]+"$");
    }
    calc_weight(weight_Array);

    }catch(IOException e)
    {
    System.out.println(e);
    }
    break;
    }

    }
    }
    });
    }catch(Exception e)
    {
    System.out.println(e);
    }

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    This code has some errors:
    Java Code:
    while((ins.available())>0)
    {
    if((ins.available())==0)
    If your while loop is only going to work if ins.available is greater than zero why check for it when it's equal to zero? The moment that it's equal to zero, it will exit the loop automatically.
    Java Code:
    int c,i=0;
    ...
    c=(char)ins.read();
    char ch=(char)c;
    weight_Array[i]=Character.toString(ch);
    Why all the circus with variable "c"? Just define it form the begining as a char. It's never used as an int in the program.
    Java Code:
    for(int j=0;j[B][COLOR="Red"]<=[/COLOR][/B]8;j++)
    this isn't going to work. Arrays are zero indexed which means that your array goes from 0 to 7. Therefore you have to change the above to avoid an "index out of bounds" error:
    Java Code:
    for(int j=0;j[B][COLOR="Blue"]<[/COLOR][/B]8;j++)
    Question: Are you sure that whatever ins.read() is going to read is only 8 chars long?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    azibhai is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •