KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > utils > CommPortsOpen


1 /*
2  ************************************************************************************
3  * Copyright (C) 2001-2006 Openbravo S.L.
4  * Licensed under the Apache Software License version 2.0
5  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6  * Unless required by applicable law or agreed to in writing, software distributed
7  * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8  * CONDITIONS OF ANY KIND, either express or implied. See the License for the
9  * specific language governing permissions and limitations under the License.
10  ************************************************************************************
11 */

12 package org.openbravo.utils;
13
14 import java.io.*;
15 import javax.comm.*;
16 import java.util.*;
17
18 public class CommPortsOpen {
19   /** How long to wait for the open to finish up. */
20   public static final int TIMEOUTSECONDS = 30;
21   /** The baud rate to use. */
22   public static final int BAUD = 9600;
23   /** The input stream */
24   protected DataInputStream is;
25   /** The output stream */
26   protected PrintStream os;
27   /** The chosen Port Identifier */
28   CommPortIdentifier thePortID;
29   /** The chosen Port itself */
30   CommPort thePort;
31
32
33   public static void main(String JavaDoc[] args) throws Exception JavaDoc, IOException, NoSuchPortException, PortInUseException, UnsupportedCommOperationException {
34     if (args.length != 1) {
35       System.err.println("Usage: CommPortsOpen filename");
36       System.exit(1);
37     }
38     String JavaDoc inputFileName = args[0];
39     CommPortsOpen cpo = new CommPortsOpen();
40     cpo.transmit_data(inputFileName);
41     cpo.close();
42     System.exit(0);
43   }
44
45   public CommPortsOpen() throws Exception JavaDoc, IOException, NoSuchPortException, PortInUseException, UnsupportedCommOperationException {
46     choosePort(CommPortIdentifier.PORT_PARALLEL);
47
48     System.out.println("Trying to open " + thePortID.getName() + "...");
49     switch (thePortID.getPortType()) {
50       case CommPortIdentifier.PORT_SERIAL:
51         thePort = thePortID.open("Openbravo CommData", TIMEOUTSECONDS * 1000);
52         SerialPort myPort = (SerialPort) thePort;
53         // set up the serial port
54
myPort.setSerialPortParams(BAUD, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
55         break;
56       case CommPortIdentifier.PORT_PARALLEL:
57         thePort = thePortID.open("Openbravo Printing", TIMEOUTSECONDS * 1000);
58         ParallelPort pPort = (ParallelPort)thePort;
59         int mode = pPort.getMode();
60         switch (mode) {
61           case ParallelPort.LPT_MODE_ECP:
62             System.out.println("Mode is: ECP");
63             break;
64           case ParallelPort.LPT_MODE_EPP:
65             System.out.println("Mode is: EPP");
66             break;
67           case ParallelPort.LPT_MODE_NIBBLE:
68             System.out.println("Mode is: Nibble Mode.");
69             break;
70           case ParallelPort.LPT_MODE_PS2:
71             System.out.println("Mode is: Byte mode.");
72             break;
73           case ParallelPort.LPT_MODE_SPP:
74             System.out.println("Mode is: Compatibility mode.");
75             break;
76             // ParallelPort.LPT_MODE_ANY is a "set only" mode;
77
// tells the API to pick "best mode"; will report the
78
// actual mode it selected.
79
default:
80             throw new IllegalStateException JavaDoc ("Parallel mode " + mode + " invalid.");
81         }
82         break;
83       default:// Neither parallel nor serial??
84
throw new IllegalStateException JavaDoc("Unknown port type " + thePortID);
85     }
86     // Get the input and output streams
87
// Printers can be write-only
88
try {
89       is = new DataInputStream(thePort.getInputStream());
90     } catch (IOException e) {
91       System.err.println("Can't open input stream: write-only");
92       is = null;
93     }
94     os = new PrintStream(thePort.getOutputStream(), true);
95   }
96
97   private void choosePort(int portType) throws Exception JavaDoc {
98     Enumeration listaPort = CommPortIdentifier.getPortIdentifiers();
99     if (portType==0) portType = CommPortIdentifier.PORT_PARALLEL;
100     while (listaPort.hasMoreElements()) {
101       thePortID = (CommPortIdentifier) listaPort.nextElement();
102       if (thePortID.getPortType() == portType) {
103         if (thePortID.isCurrentlyOwned()) throw new Exception JavaDoc("The port is use now by: " + thePortID.getCurrentOwner());
104         return;
105       }
106     }
107     thePortID=null;
108     throw new Exception JavaDoc("port not found for type: " + portType);
109   }
110
111   public void transmit(String JavaDoc data) throws IOException {
112     os.write(data.getBytes());
113   }
114
115   public void close() throws IOException {
116     System.out.println("Closing port.");
117     if (is != null) is.close();
118     os.close();
119     thePort.close();
120     thePort = null;
121     thePortID = null;
122   }
123
124   protected void transmit_data(String JavaDoc inputFileName) throws IOException {
125     BufferedReader file = new BufferedReader(
126         new FileReader(inputFileName));
127     String JavaDoc line;
128     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
129     while ((line = file.readLine()) != null) sb.append(line).append("\r\n");
130     System.out.println(sb.toString());
131     transmit(sb.toString());
132     // Finally, clean up.
133
file.close();
134   }
135 }
136
Popular Tags