dinspire
October 16th, 2005, 02:16 PM
have program that dealing with serial communication....for dealing with serial communication im using serialCom.java this class implements serialPortEventListener, so its override serialEvent() method..
what i want to do is retrieve the variable strOut filled in serialEvent from outside method read()...
im having problem when i read the variabel, its still null...the serial event hasn't occur yet..i want to detect when the serial event occur...the read() method retrieve that variable...so it'll not null. if still null try to retrieve again until we get the value....i need assist.thanks b4
here is the code snipplet:
<code>
//constructor
public class serialCom implements SerialPortEventListener {
//variables
public serialCom(CommPortIdentifier portD, String Name,int Baud,int Data, int Stop, int Part){
this.portD = portD;
this.Name = Name;
this.Baud = Baud;
this.Data = Data;
this.Stop = Stop;
this.Part = Part;
}
//method open serial port
public CommPort open(){
....
}
//open i/o stream
public InputStream initIn(){
....
}
public OutputStream initOut(){
....
}
//send method.. already works
public void send(OutputStream dos, String data){
.....
}
.......
//serial port event method
public void serialEvent(SerialPortEvent e){
// Create a StringBuffer and int to receive input data.
available = false;
StringBuffer inputBuffer = new StringBuffer();
int newData = 0;
// Determine type of event.
switch (e.getEventType()) {
// Read data until -1 is returned. If \r is received substitute
// \n for correct newline handling.
case SerialPortEvent.DATA_AVAILABLE:
int numBytes = 0;
byte[] readBuffer;
try {
while (this.is.available() > 0) {
readBuffer = new byte[this.is.available()];
numBytes = this.is.read(readBuffer);
fullString = new String(readBuffer);
StrOut = valueOf(readBuffer,' ');
numChar = numBytes;
messageQueue.addMessage(StrOut);
}
}catch (IOException ie) {
System.out.println("IOException " + ie.getMessage());
}
break;
}
available = true;
// StrOut.notify();
System.out.println(StrOut);
}
......
//read method
public String read(InputStream dis) {
this.is = dis;
int count = 0;
while (available==false) {
if (count > 0) {
System.out.println(getPortName() + ": waiting:" + count);
}
count++;
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
// Ignore
}
}
}
return StrOut;
}
</code>
what i want to do is retrieve the variable strOut filled in serialEvent from outside method read()...
im having problem when i read the variabel, its still null...the serial event hasn't occur yet..i want to detect when the serial event occur...the read() method retrieve that variable...so it'll not null. if still null try to retrieve again until we get the value....i need assist.thanks b4
here is the code snipplet:
<code>
//constructor
public class serialCom implements SerialPortEventListener {
//variables
public serialCom(CommPortIdentifier portD, String Name,int Baud,int Data, int Stop, int Part){
this.portD = portD;
this.Name = Name;
this.Baud = Baud;
this.Data = Data;
this.Stop = Stop;
this.Part = Part;
}
//method open serial port
public CommPort open(){
....
}
//open i/o stream
public InputStream initIn(){
....
}
public OutputStream initOut(){
....
}
//send method.. already works
public void send(OutputStream dos, String data){
.....
}
.......
//serial port event method
public void serialEvent(SerialPortEvent e){
// Create a StringBuffer and int to receive input data.
available = false;
StringBuffer inputBuffer = new StringBuffer();
int newData = 0;
// Determine type of event.
switch (e.getEventType()) {
// Read data until -1 is returned. If \r is received substitute
// \n for correct newline handling.
case SerialPortEvent.DATA_AVAILABLE:
int numBytes = 0;
byte[] readBuffer;
try {
while (this.is.available() > 0) {
readBuffer = new byte[this.is.available()];
numBytes = this.is.read(readBuffer);
fullString = new String(readBuffer);
StrOut = valueOf(readBuffer,' ');
numChar = numBytes;
messageQueue.addMessage(StrOut);
}
}catch (IOException ie) {
System.out.println("IOException " + ie.getMessage());
}
break;
}
available = true;
// StrOut.notify();
System.out.println(StrOut);
}
......
//read method
public String read(InputStream dis) {
this.is = dis;
int count = 0;
while (available==false) {
if (count > 0) {
System.out.println(getPortName() + ": waiting:" + count);
}
count++;
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
// Ignore
}
}
}
return StrOut;
}
</code>