Page 54 - The Definitive Guide to Building Java Robots
P. 54
Preston_5564C02.fm Page 35 Wednesday, September 14, 2005 5:42 AM
CHAPTER 2 ■ SERIAL COMMUNICATION 35
public byte[] read() {
return readString().getBytes();
}
public String readString() {
return request(WebSerialPort.READ_ONLY, JSerialPort.READ_COMMAND);
}
public void write(byte[] b) throws Exception {
String out = request(b, JSerialPort.WRITE_COMMAND);
if (out.equalsIgnoreCase(WebSerialPort.CMD_ACK) == false) {
throw new Exception("WebClient Write Failure: " + out);
}
}
// added in case where user wants to read after they send commands
// this is specific to the webcom.jsp
public byte[] read(byte[] b) {
return readString(b).getBytes();
}
public String readString(byte[] b) {
return request(b, JSerialPort.WRITE_READ_COMMAND);
}
public void close() {
// do nothing since having more than one port
}
public void setDTR(boolean dtr) {
this.dtr = dtr;
}
private String request(byte[] commands, String cmdType) {
// convert byte to string
String cmdString = byteArrayToString(commands);
log("WebClient: cmds=" + cmdString + ", cmdType=" + cmdType
+ ", timeout=" + timeout);
String out = null;
try {
String urlString = url
+ "&action=" + cmdType
+ "&commands=" + cmdString
+ "&timeout=" + timeout
+ "&dtr=" + dtr;