| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
import java.io.*; |
| 7 |
import java.util.*; |
| 8 |
import gnu.io.*; |
| 9 |
|
| 10 |
public class Test implements SerialPortEventListener { |
| 11 |
|
| 12 |
InputStream inputStream; |
| 13 |
OutputStream outputStream; |
| 14 |
SerialPort serialPort; |
| 15 |
Thread readThread; |
| 16 |
|
| 17 |
public static void main(String[] args) { |
| 18 |
if (args.length < 1) { |
| 19 |
System.out.print("Test.class /dev/ports/serialx\n"); |
| 20 |
System.exit(-1); |
| 21 |
} |
| 22 |
System.out.println("opening the Port: " + args[0]); |
| 23 |
Test reader = new Test(args[0]); |
| 24 |
} |
| 25 |
|
| 26 |
public Test(String PortName) { |
| 27 |
RXTXCommDriver TxPort = new RXTXCommDriver(); |
| 28 |
System.out.print("open Ports\n"); |
| 29 |
serialPort = (SerialPort) TxPort.getCommPort(PortName, CommPortIdentifier.PORT_SERIAL); |
| 30 |
System.out.print("Get Streams\n"); |
| 31 |
try { |
| 32 |
inputStream = serialPort.getInputStream(); |
| 33 |
outputStream = serialPort.getOutputStream(); |
| 34 |
} catch (IOException e) { |
| 35 |
e.printStackTrace(); |
| 36 |
} |
| 37 |
try { |
| 38 |
serialPort.addEventListener(this); |
| 39 |
} catch (TooManyListenersException e) { |
| 40 |
e.printStackTrace(); |
| 41 |
} |
| 42 |
try { |
| 43 |
System.out.println("Baud is " + serialPort.getBaudRate()); |
| 44 |
System.out.println("Bits is " + serialPort.getDataBits()); |
| 45 |
System.out.println("Stop is " + serialPort.getStopBits()); |
| 46 |
System.out.println("Par is " + serialPort.getParity()); |
| 47 |
System.out.print("Set Params\n"); |
| 48 |
serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); |
| 49 |
System.out.println("Baud is " + serialPort.getBaudRate()); |
| 50 |
System.out.println("Bits is " + serialPort.getDataBits()); |
| 51 |
System.out.println("Stop is " + serialPort.getStopBits()); |
| 52 |
System.out.println("Par is " + serialPort.getParity()); |
| 53 |
System.out.print("Set Params\n"); |
| 54 |
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_7, SerialPort.STOPBITS_2, SerialPort.PARITY_ODD); |
| 55 |
System.out.println("Baud is " + serialPort.getBaudRate()); |
| 56 |
System.out.println("Bits is " + serialPort.getDataBits()); |
| 57 |
System.out.println("Stop is " + serialPort.getStopBits()); |
| 58 |
System.out.println("Par is " + serialPort.getParity()); |
| 59 |
} catch (UnsupportedCommOperationException e) { |
| 60 |
e.printStackTrace(); |
| 61 |
} |
| 62 |
System.out.print("Sending 0x01\n"); |
| 63 |
try { |
| 64 |
outputStream.write((byte)0x01); |
| 65 |
System.out.print("0x01 Sent\n"); |
| 66 |
} catch (IOException e) { |
| 67 |
e.printStackTrace(); |
| 68 |
} |
| 69 |
} |
| 70 |
public void serialEvent(SerialPortEvent event) { |
| 71 |
switch(event.getEventType()) { |
| 72 |
case SerialPortEvent.BI: |
| 73 |
System.out.print("BI\n"); |
| 74 |
case SerialPortEvent.OE: |
| 75 |
System.out.print("OE\n"); |
| 76 |
case SerialPortEvent.FE: |
| 77 |
System.out.print("FE\n"); |
| 78 |
case SerialPortEvent.PE: |
| 79 |
System.out.print("PE\n"); |
| 80 |
case SerialPortEvent.CD: |
| 81 |
System.out.print("CD\n"); |
| 82 |
case SerialPortEvent.CTS: |
| 83 |
System.out.print("CTS\n"); |
| 84 |
case SerialPortEvent.DSR: |
| 85 |
System.out.print("DSR\n"); |
| 86 |
case SerialPortEvent.RI: |
| 87 |
System.out.print("RI\n"); |
| 88 |
case SerialPortEvent.OUTPUT_BUFFER_EMPTY: |
| 89 |
System.out.print("Out Buff Empty\n"); |
| 90 |
break; |
| 91 |
case SerialPortEvent.DATA_AVAILABLE: |
| 92 |
System.out.print("Data Available\n"); |
| 93 |
break; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
} |
| 98 |
|