Self defined Event processing problem

Hi every1Cthe situation is like the following:

2 PCs communicate through Bluetooth serial port profile by 3 classes that i wrote.

First,BluetoothManger, it implements SerialportEventListener of Javax.comm package,its job is to read the incoming data from the other end and store them into an array,and it works.

Second,DataReceiver,it implements EventObject, so whenever it copies the array from BluetoothManger, it will pass them into the next stageAlgorithm.

Third,Algorithm., it implements EventListener, it has function @public void dataChanged(DataReceiver dr)@, this function iscausing a loop to run forever

The other end is sending 20 numbers each frame,and there are only 10 frames,but once the sender side start sending stuff, the receiver end kept running forever...

The following is the code,thanks very much in advance for anybody's voice!!!

import java.io.*;

import java.util.*;

import javax.comm.*;

publicclass BluetoothManagerimplements SerialPortEventListener{

static CommPortIdentifier portId;

static Enumeration portList;

InputStream inputStream;

SerialPort serialPort;

DataReceiver dr;

Convert c;

byte[] tempBuffer;

byte[] smallArray;

int[] intArray;

int[] ints;

int k;

int h;

public BluetoothManager()

{

tempBuffer=newbyte[100];

smallArray=newbyte[100];

ints=newint[20];

intArray=newint[230];

dr=new DataReceiver(ints);

c=new Convert();

k=0;

h=0;

}

publicvoid listenToPort()

{

portList=CommPortIdentifier.getPortIdentifiers();

while(portList.hasMoreElements()){

portId = (CommPortIdentifier) portList.nextElement();

if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL){

System.out.println("found..." + portId.getName());

if (portId.getName().equals("COM6")){// 4

try{

System.out.println("about to try.." + portId.getName());

serialPort = (SerialPort) portId.open("SerialPort", 2000);

}catch (PortInUseException e){ System.out.println("Exception: Port in use!");}

try{

inputStream = serialPort.getInputStream();

}catch (IOException e){}

try{

serialPort.addEventListener(this);

}catch (TooManyListenersException e){}

serialPort.notifyOnDataAvailable(true);

try{

serialPort.setSerialPortParams(9600,

SerialPort.DATABITS_8,

SerialPort.STOPBITS_1,

SerialPort.PARITY_NONE);

}catch (UnsupportedCommOperationException e){}

}

}

}

}

///////////////////////////////////////////////////////////////////////////////

publicint[] storeFrameArray(byte[] temp)

{

smallArray=temp;

for(int i=0;i<20;i++){

byte[] temp2=newbyte[4];

System.arraycopy(smallArray,k,temp2,0,4);

ints[i]=c.toInt(temp2);

k=k+4;

if (k>=80) k=0;

}

return ints;

}

//////////////////////////////////////////////////////////////////////////

public DataReceiver getDataRX(){

return dr;

}

/////////////////////////////////////////////////////////////////////////////

publicvoid serialEvent(SerialPortEvent event)

{

switch(event.getEventType()){

case SerialPortEvent.DATA_AVAILABLE:

try{

while(inputStream.available()>0){

inputStream.read(tempBuffer);

dr.setArray(this.storeFrameArray(tempBuffer));

}

}catch(IOException e){}

break;

}

}

import java.util.*;

publicclass DataReceiverextends EventObject{

int[] rxArray;

int h;

boolean dataChangedFlag;

public DataReceiver(int[] ints)

{

super(ints);

rxArray=newint[20];

dataChangedFlag=false;

h=0;

}

publicvoid setArray(int[] array)

{

System.arraycopy(array,0,rxArray,0,20);

dataChangedFlag=true;

}

publicint[] getIntArray()

{

return rxArray;

}

publicboolean getFlag()

{

dataChangedFlag=false;

return dataChangedFlag;

}

publicvoid addDataReceiverListener(Algorithm algo)

{

while(dataChangedFlag=true){

algo.dataChanged(this);

}

}

}

import java.util.*;

publicclass Algorithmimplements EventListener{

boolean finishFlag;

int[] alArray;

int[] partInts;

int[] events;

int eventIndex;

int[] dd;

int i;

int result;

DataReceiver drr;

public Algorithm(BluetoothManager bMAL)

{

drr=bMAL.getDataRX();

finishFlag=false;

alArray=newint[230];

partInts=newint[20];

events=newint[500];

dd=newint[20];

result=0;

eventIndex=0;

drr.addDataReceiverListener(this);

}

publicvoid calculate(int[] intss)

{

for(int i=0;i<20;i++)dd[i]=intss[i]*5;

for(int j=0;j<20;j++){

if(dd[j]<100&&dd[j]>50){

events[eventIndex]=dd[j];

System.out.println("Index"+eventIndex+":"+dd[j]);

eventIndex++;

}

}

}

publicvoid dataChanged(DataReceiver data)

{

data.getFlag();

partInts=data.getIntArray();

calculate(partInts);

}

}

Thanks again

[11587 byte] By [nerdEvera] at [2007-11-27 11:37:12]
# 1

> Hi every1Cthe situation is like the following:

What?

~

yawmarka at 2007-7-29 17:13:11 > top of Java-index,Java Essentials,Java Programming...
# 2

u didnt understand my question?

nerdEvera at 2007-7-29 17:13:11 > top of Java-index,Java Essentials,Java Programming...