KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ShipmentScaleApplet


1 /*
2  * $Id: ShipmentScaleApplet.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24
25 import java.applet.Applet JavaDoc;
26 import java.applet.AppletContext JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.OutputStream JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31 import java.util.TooManyListenersException JavaDoc;
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 /**
45  * ShipmentScaleApplet - Applet for reading weight from a scale and input into the browser
46  *
47  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
48  * @version $Rev: 5462 $
49  * @since 3.0
50  */

51 public class ShipmentScaleApplet extends Applet JavaDoc implements SerialPortEventListener, CommPortOwnershipListener {
52     
53     private AppletContext JavaDoc ctx = null;
54     
55     private CommPortIdentifier portId = null;
56     private SerialPort serialPort = null;
57     private boolean portOpen = false;
58     
59     private InputStream JavaDoc in = null;
60     private OutputStream JavaDoc out = null;
61     
62     public void init() {
63         this.ctx = this.getAppletContext();
64         /*
65         String port = this.getParameter("serialPort");
66         try {
67             this.configurePort(port);
68         } catch (Exception e) {
69             e.printStackTrace();
70         }
71         */

72         try {
73             this.sendFakeMessage();
74         } catch (IOException JavaDoc e) {
75             e.printStackTrace();
76         }
77     }
78     
79     public void paint() {
80         
81     }
82     
83     public void configurePort(String JavaDoc port) throws UnsupportedCommOperationException, IOException JavaDoc {
84         try {
85             portId = CommPortIdentifier.getPortIdentifier(port);
86         } catch (NoSuchPortException e) {
87             // TODO Auto-generated catch block
88
e.printStackTrace();
89         }
90         try {
91             serialPort = (SerialPort) portId.open("SerialScale", 30000);
92         } catch (PortInUseException e) {
93             // TODO Auto-generated catch block
94
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 JavaDoc e) {
106             // TODO Auto-generated catch block
107
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     /* (non-Javadoc)
118      * @see javax.comm.SerialPortEventListener#serialEvent(javax.comm.SerialPortEvent)
119      */

120     public void serialEvent(SerialPortEvent event) {
121         // Create a StringBuffer and int to receive input data.
122
StringBuffer JavaDoc inputBuffer = new StringBuffer JavaDoc();
123         int newData = 0;
124
125         // Determine type of event.
126
switch (event.getEventType()) {
127
128             // Read data until -1 is returned. If \r is received substitute
129
// \n for correct newline handling.
130
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                         //inputBuffer.append("(" + newData + ")");
146
}
147                     
148                     } catch (IOException JavaDoc ex) {
149                         System.err.println(ex);
150                         return;
151                     }
152                 }
153
154                 // Append received data to messageAreaIn.
155
checkResponse(inputBuffer.toString());
156                 break;
157
158             // If break event append BREAK RECEIVED message.
159
case SerialPortEvent.BI:
160                 break;
161         }
162     }
163
164     /* (non-Javadoc)
165      * @see javax.comm.CommPortOwnershipListener#ownershipChange(int)
166      */

167     public void ownershipChange(int arg0) {
168         // TODO Auto-generated method stub
169

170     }
171     
172     // send the code to the scale and requests the weight
173
public void sendMessage() throws IOException JavaDoc {
174         String JavaDoc 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 JavaDoc {
184         out.close();
185         in.close();
186         serialPort.close();
187     }
188     
189     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
190         ShipmentScaleApplet applet = new ShipmentScaleApplet();
191         applet.sendMessage();
192         applet.close();
193     }
194     
195     
196     // validates the response from the scale and calls the set method
197
private void checkResponse(String JavaDoc response) {
198         StringTokenizer JavaDoc token = new StringTokenizer JavaDoc(response, "|");
199         if (token != null && token.hasMoreElements()) {
200             String JavaDoc weightStr = token.nextToken();
201             setWeight(weightStr);
202         }
203     }
204     
205     private void sendFakeMessage() throws IOException JavaDoc {
206         String JavaDoc weight = this.getParameter("fakeWeight");
207         if (weight == null) {
208             weight = "5";
209         }
210         setWeight(weight);
211     }
212     
213     // calls the setWeight(weight) JavaScript function on the current page
214
private void setWeight(String JavaDoc weight) {
215         JSObject win = JSObject.getWindow(this);
216         String JavaDoc[] args = { weight };
217         win.call("setWeight", args);
218     }
219 }
220
Popular Tags