Page 51 - The Definitive Guide to Building Java Robots
P. 51
Preston_5564C02.fm Page 32 Wednesday, September 14, 2005 5:42 AM
32 CHAPTER 2 ■ SERIAL COMMUNICATION
int tOut = new Integer(timeout).intValue();
this.timeout = tOut;
//if (tOut != 0) {
// com.setTimeout(tOut);
//}
if (dtr.equalsIgnoreCase("true")) {
com.setDTR(true);
}
if (dtr.equalsIgnoreCase("false")) {
com.setDTR(false);
}
if (action.equalsIgnoreCase(JSerialPort.READ_COMMAND)) {
return read();
} else if (action.equalsIgnoreCase(JSerialPort.WRITE_READ_COMMAND)) {
return read(cmds);
} else if (action.equalsIgnoreCase(JSerialPort.WRITE_COMMAND)) {
return write(cmds);
} else {
return null;
}
}
/**
*
* @param cmd
* this will be comma delimited seq of cmds
*/
private String write(String cmd) throws Exception {
com.write(urlCmdsToBytes(cmd));
return CMD_ACK;
}
private String read(String cmd) throws Exception {
write(cmd);
Utils.pause(timeout);
return read();
}
private String read() {
return com.readString();
}
public void close() {
com.close();
}
private byte[] urlCmdsToBytes(String command) {
String[] commands = command.split(",");
byte[] cmds = new byte[commands.length];