Page 52 - The Definitive Guide to Building Java Robots
P. 52
Preston_5564C02.fm Page 33 Wednesday, September 14, 2005 5:42 AM
CHAPTER 2 ■ SERIAL COMMUNICATION 33
for (int x = 0; x < commands.length; x++) {
int i = new Integer(commands[x]).intValue();
cmds[x] = (byte) i;
}
return cmds;
}
}
The next part of our serial port extension to the web is webcom.jsp. This JSP will import the
classes from Chapter 2, and then parse the parameters on the URL, sending those strings to the
constructor of the previous class, WebSerialPort.
Because the constructor throws an exception, I’ll wrap the constructor and execute
methods in a try-catch block with an example of how to use the JSP. Outside of construction,
and invoking the execute method, all functionality and logic for the webcom.jsp is located in
the WebSerialPort class. (See Example 2-7.)
Example 2-7. webcom.jsp
<%@ page import="com.scottpreston.javarobot.chapter2.*" %><%
// WebClient class will throw exception if these are not set
String portId = request.getParameter("portid");
String action = request.getParameter("action");
String cmdInput = request.getParameter("commands");
String timeout = request.getParameter("timeout");
String dtr = request.getParameter("dtr");
try {
WebSerialPort com = new WebSerialPort(portId);
out.println(com.execute(action,cmdInput,timeout,dtr));
} catch (Exception e) {
out.println(e);
int term = '!';
%>
usage: /webcom.jsp?portid=[1,2,..]&action=[r,w,wr]➥
&commands=[100,120,222,..]&timeout=[0,50,..]&dtr=true
<p>sample:
<a href="/webcom.jsp?portid=1&action=wr&commands=100,<%=term%>➥
&timeout=0&dtr=true">sample 1</a>
<% }%>
If you did not notice, the WebSerialPort did not implement the JSerialPort interface. Why
is that? Well, for two reasons. One, it did not need to since the behavior was slightly different