1 24 25 import java.applet.Applet ; 26 import java.applet.AppletContext ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.OutputStream ; 30 import java.util.StringTokenizer ; 31 import java.util.TooManyListenersException ; 32 33 import javax.comm.CommPortIdentifier; 34 import javax.comm.CommPortOwnershipListener; 35 import javax.comm.NoSuchPortException; 36 import javax.comm.PortInUseException; 37 import javax.comm.SerialPort; 38 import javax.comm.SerialPortEvent; 39 import javax.comm.SerialPortEventListener; 40 import javax.comm.UnsupportedCommOperationException; 41 42 import netscape.javascript.JSObject; 43 44 51 public class ShipmentScaleApplet extends Applet implements SerialPortEventListener, CommPortOwnershipListener { 52 53 private AppletContext ctx = null; 54 55 private CommPortIdentifier portId = null; 56 private SerialPort serialPort = null; 57 private boolean portOpen = false; 58 59 private InputStream in = null; 60 private OutputStream out = null; 61 62 public void init() { 63 this.ctx = this.getAppletContext(); 64 72 try { 73 this.sendFakeMessage(); 74 } catch (IOException e) { 75 e.printStackTrace(); 76 } 77 } 78 79 public void paint() { 80 81 } 82 83 public void configurePort(String port) throws UnsupportedCommOperationException, IOException { 84 try { 85 portId = CommPortIdentifier.getPortIdentifier(port); 86 } catch (NoSuchPortException e) { 87 e.printStackTrace(); 89 } 90 try { 91 serialPort = (SerialPort) portId.open("SerialScale", 30000); 92 } catch (PortInUseException e) { 93 e.printStackTrace(); 95 } 96 97 serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); 98 serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_OUT); 99 100 in = serialPort.getInputStream(); 101 out = serialPort.getOutputStream(); 102 103 try { 104 serialPort.addEventListener(this); 105 } catch (TooManyListenersException e) { 106 e.printStackTrace(); 108 } 109 110 serialPort.enableReceiveTimeout(30); 111 serialPort.notifyOnDataAvailable(true); 112 serialPort.notifyOnBreakInterrupt(true); 113 portId.addPortOwnershipListener(this); 114 this.portOpen = true; 115 } 116 117 120 public void serialEvent(SerialPortEvent event) { 121 StringBuffer inputBuffer = new StringBuffer (); 123 int newData = 0; 124 125 switch (event.getEventType()) { 127 128 case SerialPortEvent.DATA_AVAILABLE: 131 while (newData != -1) { 132 try { 133 newData = in.read(); 134 if (newData == -1) { 135 break; 136 } 137 if (newData != 32 && newData != 3) { 138 if ('\r' == (char)newData) { 139 inputBuffer.append('|'); 140 } else if ('\n' == (char)newData) { 141 inputBuffer.append(""); 142 } else { 143 inputBuffer.append((char)newData); 144 } 145 } 147 148 } catch (IOException ex) { 149 System.err.println(ex); 150 return; 151 } 152 } 153 154 checkResponse(inputBuffer.toString()); 156 break; 157 158 case SerialPortEvent.BI: 160 break; 161 } 162 } 163 164 167 public void ownershipChange(int arg0) { 168 170 } 171 172 public void sendMessage() throws IOException { 174 String message = "W\r"; 175 char[] msgChars = message.toCharArray(); 176 for (int i = 0; i < msgChars.length; i++) { 177 out.write((int)msgChars[i]); 178 } 179 out.flush(); 180 serialPort.sendBreak(1000); 181 } 182 183 public void close() throws IOException { 184 out.close(); 185 in.close(); 186 serialPort.close(); 187 } 188 189 public static void main(String args[]) throws Exception { 190 ShipmentScaleApplet applet = new ShipmentScaleApplet(); 191 applet.sendMessage(); 192 applet.close(); 193 } 194 195 196 private void checkResponse(String response) { 198 StringTokenizer token = new StringTokenizer (response, "|"); 199 if (token != null && token.hasMoreElements()) { 200 String weightStr = token.nextToken(); 201 setWeight(weightStr); 202 } 203 } 204 205 private void sendFakeMessage() throws IOException { 206 String weight = this.getParameter("fakeWeight"); 207 if (weight == null) { 208 weight = "5"; 209 } 210 setWeight(weight); 211 } 212 213 private void setWeight(String weight) { 215 JSObject win = JSObject.getWindow(this); 216 String [] args = { weight }; 217 win.call("setWeight", args); 218 } 219 } 220 | Popular Tags |