Results 1 to 3 of 3
Thread: Help me for the following
- 05-19-2009, 11:29 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 7
- Rep Power
- 0
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);
}
- 05-19-2009, 02:08 PM #2
This code has some errors:
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:while((ins.available())>0) { if((ins.available())==0)
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:int c,i=0; ... c=(char)ins.read(); char ch=(char)c; weight_Array[i]=Character.toString(ch);
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="Red"]<=[/COLOR][/B]8;j++)
Question: Are you sure that whatever ins.read() is going to read is only 8 chars long?Java Code:for(int j=0;j[B][COLOR="Blue"]<[/COLOR][/B]8;j++)
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 05-20-2009, 06:46 AM #3
Member
- Join Date
- Apr 2009
- Posts
- 7
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks