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
|
/*
* Test.class is just that.. a Test
*/
import java.io.*;
import java.util.*;
import gnu.io.*;
public class Test implements SerialPortEventListener {
InputStream inputStream;
OutputStream outputStream;
SerialPort serialPort;
Thread readThread;
public static void main(String[] args) {
if (args.length < 1) {
System.out.print("Test.class /dev/ports/serialx\n");
System.exit(-1);
}
System.out.println("opening the Port: " + args[0]);
Test reader = new Test(args[0]);
}
public Test(String PortName) {
RXTXCommDriver TxPort = new RXTXCommDriver();
System.out.print("open Ports\n");
serialPort = (SerialPort) TxPort.getCommPort(PortName, CommPortIdentifier.PORT_SERIAL);
System.out.print("Get Streams\n");
try {
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
e.printStackTrace();
}
try {
System.out.println("Baud is " + serialPort.getBaudRate());
System.out.println("Bits is " + serialPort.getDataBits());
System.out.println("Stop is " + serialPort.getStopBits());
System.out.println("Par is " + serialPort.getParity());
System.out.print("Set Params\n");
serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
System.out.println("Baud is " + serialPort.getBaudRate());
System.out.println("Bits is " + serialPort.getDataBits());
System.out.println("Stop is " + serialPort.getStopBits());
System.out.println("Par is " + serialPort.getParity());
System.out.print("Set Params\n");
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_7, SerialPort.STOPBITS_2, SerialPort.PARITY_ODD);
System.out.println("Baud is " + serialPort.getBaudRate());
System.out.println("Bits is " + serialPort.getDataBits());
System.out.println("Stop is " + serialPort.getStopBits());
System.out.println("Par is " + serialPort.getParity());
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
}
System.out.print("Sending 0x01\n");
try {
outputStream.write((byte)0x01);
System.out.print("0x01 Sent\n");
} catch (IOException e) {
e.printStackTrace();
}
}
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:
System.out.print("Data Available\n");
break;
}
}
}
|