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