| 1 |
import gnu.io.*; |
| 2 |
import java.util.*; |
| 3 |
import java.io.InputStream; |
| 4 |
import java.io.OutputStream; |
| 5 |
|
| 6 |
|
| 7 |
public class Simple implements SerialPortEventListener |
| 8 |
{ |
| 9 |
CommPortIdentifier cpi; |
| 10 |
Enumeration ports; |
| 11 |
SerialPort port = null; |
| 12 |
InputStream input; |
| 13 |
OutputStream output; |
| 14 |
|
| 15 |
public static void main( String args[] ) |
| 16 |
{ |
| 17 |
boolean done = false; |
| 18 |
Simple reader = new Simple( ); |
| 19 |
while ( !done ) |
| 20 |
{ |
| 21 |
try { |
| 22 |
Thread.sleep(100); |
| 23 |
} catch (Exception e) {} |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
public Simple( ) |
| 28 |
{ |
| 29 |
|
| 30 |
System.out.println("Getting PortIdentifiers"); |
| 31 |
ports = CommPortIdentifier.getPortIdentifiers(); |
| 32 |
while ( ports.hasMoreElements() ) |
| 33 |
{ |
| 34 |
cpi = (CommPortIdentifier) ports.nextElement(); |
| 35 |
if (cpi.getPortType() == CommPortIdentifier.PORT_SERIAL) |
| 36 |
{ |
| 37 |
if ( cpi.getName().equals( "/dev/ttyS1" ) ) |
| 38 |
{ |
| 39 |
try { |
| 40 |
port = (SerialPort) cpi.open("Simple", 2000); |
| 41 |
port.addEventListener(this); |
| 42 |
port.notifyOnDataAvailable(true); |
| 43 |
output=port.getOutputStream(); |
| 44 |
input=port.getInputStream(); |
| 45 |
System.out.println("writing output"); |
| 46 |
output.write( new String("123456789\0").getBytes() ); |
| 47 |
} catch (Exception e) |
| 48 |
{ |
| 49 |
e.printStackTrace(); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
return; |
| 55 |
} |
| 56 |
public void serialEvent(SerialPortEvent event) { |
| 57 |
switch(event.getEventType()) |
| 58 |
{ |
| 59 |
case SerialPortEvent.BI: |
| 60 |
System.out.print("BI\n"); |
| 61 |
case SerialPortEvent.OE: |
| 62 |
System.out.print("OE\n"); |
| 63 |
case SerialPortEvent.FE: |
| 64 |
System.out.print("FE\n"); |
| 65 |
case SerialPortEvent.PE: |
| 66 |
System.out.print("PE\n"); |
| 67 |
case SerialPortEvent.CD: |
| 68 |
System.out.print("CD\n"); |
| 69 |
case SerialPortEvent.CTS: |
| 70 |
System.out.print("CTS\n"); |
| 71 |
case SerialPortEvent.DSR: |
| 72 |
System.out.print("DSR\n"); |
| 73 |
case SerialPortEvent.RI: |
| 74 |
System.out.print("RI\n"); |
| 75 |
case SerialPortEvent.OUTPUT_BUFFER_EMPTY: |
| 76 |
System.out.print("Out Buff Empty\n"); |
| 77 |
break; |
| 78 |
case SerialPortEvent.DATA_AVAILABLE: |
| 79 |
byte in[] = new byte[800]; |
| 80 |
int ret = 0; |
| 81 |
|
| 82 |
|
| 83 |
System.out.println("Got Data Available"); |
| 84 |
try { |
| 85 |
ret = input.read( in, 0, 63 ); |
| 86 |
} catch (Exception e) { |
| 87 |
System.out.println("Input Exception"); |
| 88 |
} |
| 89 |
System.out.println("Printing read() results"); |
| 90 |
if ( ret > 0 ) |
| 91 |
{ |
| 92 |
try { |
| 93 |
System.out.write( in, 0, ret ); |
| 94 |
System.out.println(); |
| 95 |
} catch ( Exception e ) |
| 96 |
{ |
| 97 |
e.printStackTrace(); |
| 98 |
} |
| 99 |
} |
| 100 |
System.exit( 0 ); |
| 101 |
break; |
| 102 |
} |
| 103 |
} |
| 104 |
} |