/Users/lyon/j4p/src/serialPorts/Test.java
|
1 package serialPorts;
2
3 /*
4 * Test.class is just that.. a Test
5 */
6
7
8 import gnu.io.*;
9
10 import javax.comm.CommPortIdentifier;
11 import javax.comm.SerialPort;
12 import javax.comm.UnsupportedCommOperationException;
13 import javax.comm.SerialPortEventListener;
14 import javax.comm.PortInUseException;
15 import javax.comm.CommPort;
16 import javax.comm.SerialPortEvent;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.OutputStream;
20 import java.util.TooManyListenersException;
21
22 public class Test implements SerialPortEventListener {
23
24 InputStream inputStream;
25 OutputStream outputStream;
26 SerialPort serialPort;
27 Thread readThread;
28
29 public static void main(String[] args) {
30 String portName = "/dev/cu.modem";
31 Test reader = new Test(portName);
32 }
33
34 public Test(String PortName) {
35 CommPortIdentifier cpid = Utils.getPortByName(PortName);
36 if (cpid == null){
37 System.out.println("unable to obtain comm port;"+PortName);
38 return;
39 }
40 serialPort = (SerialPort) getPort(cpid);
41
42 System.out.print("Get Streams\n");
43 try {
44 inputStream = serialPort.getInputStream();
45 outputStream = serialPort.getOutputStream();
46 } catch (IOException e) {
47 e.printStackTrace();
48 }
49 try {
50 serialPort.addEventListener((SerialPortEventListener) this);
51 } catch (TooManyListenersException e) {
52 e.printStackTrace();
53 }
54 try {
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 System.out.print("Set Params\n");
60 serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
61 System.out.println("Baud is " + serialPort.getBaudRate());
62 System.out.println("Bits is " + serialPort.getDataBits());
63 System.out.println("Stop is " + serialPort.getStopBits());
64 System.out.println("Par is " + serialPort.getParity());
65 System.out.print("Set Params\n");
66 serialPort.setSerialPortParams(9600, SerialPort.DATABITS_7, SerialPort.STOPBITS_2, SerialPort.PARITY_ODD);
67 System.out.println("Baud is " + serialPort.getBaudRate());
68 System.out.println("Bits is " + serialPort.getDataBits());
69 System.out.println("Stop is " + serialPort.getStopBits());
70 System.out.println("Par is " + serialPort.getParity());
71 } catch (UnsupportedCommOperationException e) {
72 e.printStackTrace();
73 }
74 System.out.print("Sending 0x01\n");
75 try {
76 outputStream.write((byte) 0x01);
77 System.out.print("0x01 Sent\n");
78 } catch (IOException e) {
79 e.printStackTrace();
80 }
81 }
82
83 private CommPort getPort(CommPortIdentifier cpid) {
84 try {
85 return cpid.open("Test",200);
86 } catch (PortInUseException e) {
87 e.printStackTrace();
88 return null;
89 }
90 }
91
92 public void serialEvent(SerialPortEvent event) {
93 switch (event.getEventType()) {
94 case SerialPortEvent.BI:
95 System.out.print("BI\n");
96 case SerialPortEvent.OE:
97 System.out.print("OE\n");
98 case SerialPortEvent.FE:
99 System.out.print("FE\n");
100 case SerialPortEvent.PE:
101 System.out.print("PE\n");
102 case SerialPortEvent.CD:
103 System.out.print("CD\n");
104 case SerialPortEvent.CTS:
105 System.out.print("CTS\n");
106 case SerialPortEvent.DSR:
107 System.out.print("DSR\n");
108 case SerialPortEvent.RI:
109 System.out.print("RI\n");
110 case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
111 System.out.print("Out Buff Empty\n");
112 break;
113 case SerialPortEvent.DATA_AVAILABLE:
114 System.out.print("Data Available\n");
115 break;
116 }
117 }
118
119 }
120
121