mPort.setSerialPortParams(
2400,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE
);
Reader in = new InputStreamReader(mPort.getInputStream());
mPortListener = new PortListener (in);
mOut = new OutputStreamWriter(mPort.getOutputStream());
}
public void run() {
int c, n = 1;
try {
sendReturn();
sendReturn();
while ((c = mFileIn.read()) != -1) {
if (c == '\r') {
sendReturn();
if (mPortListener.isComplete() == false) {
throw new DownloadException("Timed out waiting " +
"for a response from the RCX.");
}
else if (mPortListener.isError() == true) {
throw new DownloadException("Error: " +
mPortListener.getLastLine());
}
System.out.print(".");
n++;
}
else if (c != '\n') {
mPortListener.reset();
mOut.write(c);
mOut.flush();
Thread.sleep(kCharSleep);
}
}
sendReturn();
Page 238
sendReturn();
}
catch (InterruptedException ie) { System.out.println(ie) ; }
catch (DownloadException de) {
System.out.println();
System.out.println("Line " + n + ":");
System.out.println(" " + de.getMessage());
}
catch (IOException ioe) { System.out.println(ioe); }
// Regardless of what happened, try to clean up.
try {
mPortListener.stop();
mFileIn.close();
mOut.close();