1 12 package org.openbravo.utils; 13 14 import java.io.*; 15 import javax.comm.*; 16 import java.util.*; 17 18 public class CommPortsOpen { 19 20 public static final int TIMEOUTSECONDS = 30; 21 22 public static final int BAUD = 9600; 23 24 protected DataInputStream is; 25 26 protected PrintStream os; 27 28 CommPortIdentifier thePortID; 29 30 CommPort thePort; 31 32 33 public static void main(String [] args) throws Exception , IOException, NoSuchPortException, PortInUseException, UnsupportedCommOperationException { 34 if (args.length != 1) { 35 System.err.println("Usage: CommPortsOpen filename"); 36 System.exit(1); 37 } 38 String 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 , 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 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 default: 80 throw new IllegalStateException ("Parallel mode " + mode + " invalid."); 81 } 82 break; 83 default: throw new IllegalStateException ("Unknown port type " + thePortID); 85 } 86 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 { 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 ("The port is use now by: " + thePortID.getCurrentOwner()); 104 return; 105 } 106 } 107 thePortID=null; 108 throw new Exception ("port not found for type: " + portType); 109 } 110 111 public void transmit(String 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 inputFileName) throws IOException { 125 BufferedReader file = new BufferedReader( 126 new FileReader(inputFileName)); 127 String line; 128 StringBuffer sb = new StringBuffer (); 129 while ((line = file.readLine()) != null) sb.append(line).append("\r\n"); 130 System.out.println(sb.toString()); 131 transmit(sb.toString()); 132 file.close(); 134 } 135 } 136 | Popular Tags |