Page 44 - The Definitive Guide to Building Java Robots
P. 44
Preston_5564C02.fm Page 25 Wednesday, September 14, 2005 5:42 AM
CHAPTER 2 ■ SERIAL COMMUNICATION 25
s.deleteCharAt(s.length() - 1);
return s.toString();
}
public void write(byte[] b) throws Exception {
currentWrite = b;
outputStream.write(b);
}
public void close() {
serialPort.close();
}
public byte[] read(byte[] b) throws Exception {
// not used
return null;
}
public String readString(byte[] b) throws Exception {
// not used
return null;
}
public void serialEvent(SerialPortEvent event) {
if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
readBuffer = new byte[32];
i = 0;
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
int byteCount = 0;
for (int i = 0; i < currentWrite.length; i++) {
if (currentWrite[i] == readBuffer[i]) {
byteCount++;
}
}
if (byteCount != currentWrite.length) {
dataIn = true;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}