root / trunk / extensions / extGPS / test / SimpleSnuV1.java @ 4720
History | View | Annotate | Download (4.57 KB)
1 |
/*
|
---|---|
2 |
* This class is one end of a test pair of programs
|
3 |
* SimpleRead.class is the other end.
|
4 |
* To run the test requires two com ports
|
5 |
* and a null modem cable.
|
6 |
* Start SimpleSnuV1 and then run SimpleRead
|
7 |
* on the other machine/com port.
|
8 |
* This code is supplied for demonstration purposes only.
|
9 |
* You are free to use it as you please.
|
10 |
*/
|
11 |
|
12 |
|
13 |
import java.io.*; |
14 |
import java.util.*; |
15 |
import gnu.io.*; |
16 |
|
17 |
public class SimpleSnuV1 implements Runnable, SerialPortEventListener { |
18 |
|
19 |
static CommPortIdentifier portId;
|
20 |
static Enumeration portList; |
21 |
|
22 |
InputStream inputStream;
|
23 |
OutputStream outputStream;
|
24 |
SerialPort serialPort; |
25 |
Thread readThread;
|
26 |
|
27 |
int numBytes = 0; |
28 |
String num = "021891383"; |
29 |
String rst = "atz"; |
30 |
String dial ="atd"; |
31 |
String outData = ""; |
32 |
String outDataBuff = ""; |
33 |
boolean running = true; |
34 |
boolean process = true; |
35 |
boolean waitForInput = true; |
36 |
|
37 |
public static void main(String[] args) { |
38 |
if (args.length < 1) { |
39 |
System.out.print("SimpleSnuV1.class /dev/ttyxx\n"); |
40 |
System.exit(-1); |
41 |
} |
42 |
portList = CommPortIdentifier.getPortIdentifiers(); |
43 |
while (portList.hasMoreElements()) {
|
44 |
portId = (CommPortIdentifier) portList.nextElement(); |
45 |
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
|
46 |
if (portId.getName().equals(args[0])) { |
47 |
SimpleSnuV1 reader = new SimpleSnuV1();
|
48 |
} |
49 |
} |
50 |
} |
51 |
} |
52 |
|
53 |
public SimpleSnuV1() {
|
54 |
try {
|
55 |
serialPort = (SerialPort) portId.open("SimpleSnu", 2000); |
56 |
} catch (PortInUseException e) {}
|
57 |
try {
|
58 |
inputStream = serialPort.getInputStream(); |
59 |
outputStream = serialPort.getOutputStream(); |
60 |
} catch (IOException e) {} |
61 |
try {
|
62 |
serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
|
63 |
} catch (UnsupportedCommOperationException e) {}
|
64 |
byte[] readBuffer = new byte[20]; |
65 |
try {
|
66 |
serialPort.addEventListener(this);
|
67 |
} catch (TooManyListenersException e) { |
68 |
System.out.print("To Many Event Listeners\n"); |
69 |
System.exit(-1); |
70 |
} |
71 |
serialPort.notifyOnDataAvailable(true);
|
72 |
readThread = new Thread(this); |
73 |
readThread.start(); |
74 |
} |
75 |
|
76 |
public void run() { |
77 |
int resetCount = 0; |
78 |
int numBytes = 0; |
79 |
byte[] readBuffer = new byte[20]; |
80 |
String sReadBuff = ""; |
81 |
boolean connected = false; |
82 |
while (running) {
|
83 |
if (!connected) {
|
84 |
try {
|
85 |
while (inputStream.available() > 0) { |
86 |
numBytes = inputStream.read(readBuffer); |
87 |
String tmpR = new String(readBuffer); |
88 |
sReadBuff += tmpR.substring(0, numBytes);
|
89 |
} |
90 |
} catch (IOException e) { |
91 |
System.exit(1); |
92 |
} |
93 |
if (!sReadBuff.equals("")) { |
94 |
System.out.print(sReadBuff + "\n"); |
95 |
} else {
|
96 |
|
97 |
} |
98 |
int pos = 0; |
99 |
if ((pos = sReadBuff.indexOf("atz")) != -1) { |
100 |
try {
|
101 |
Thread.sleep(1000); |
102 |
} catch (InterruptedException e) {} |
103 |
try {
|
104 |
outputStream.write(new String("OK").getBytes()); |
105 |
outputStream.write((byte)0x0D); |
106 |
outputStream.write((byte)0x0A); |
107 |
System.out.print("OK Sent\n"); |
108 |
} catch (IOException e) { |
109 |
System.exit(1); |
110 |
} |
111 |
sReadBuff = "";
|
112 |
} else if ((pos = sReadBuff.indexOf("atd")) != -1) { |
113 |
sReadBuff = "";
|
114 |
try {
|
115 |
Thread.sleep(1000); |
116 |
} catch (InterruptedException e) {} |
117 |
try {
|
118 |
outputStream.write(new String("CONNECT 9600").getBytes()); |
119 |
outputStream.write((byte)0x0D); |
120 |
outputStream.write((byte)0x0A); |
121 |
// connected = true;
|
122 |
} catch (IOException e) { |
123 |
System.exit(1); |
124 |
} |
125 |
try {
|
126 |
Thread.sleep(2000); |
127 |
} catch (InterruptedException e) {} |
128 |
} |
129 |
} else {
|
130 |
|
131 |
} |
132 |
try {
|
133 |
Thread.sleep(100); |
134 |
} catch (InterruptedException e) {} |
135 |
} |
136 |
|
137 |
System.out.print("Normal Exit...\n"); |
138 |
} |
139 |
public void serialEvent(SerialPortEvent event) { |
140 |
// try {
|
141 |
// Thread.sleep(500);
|
142 |
// } catch (InterruptedException e) {}
|
143 |
switch(event.getEventType()) {
|
144 |
case SerialPortEvent.BI:
|
145 |
System.out.print("BI\n"); |
146 |
break;
|
147 |
case SerialPortEvent.OE:
|
148 |
System.out.print("OE\n"); |
149 |
break;
|
150 |
case SerialPortEvent.FE:
|
151 |
System.out.print("FE\n"); |
152 |
break;
|
153 |
case SerialPortEvent.PE:
|
154 |
System.out.print("PE\n"); |
155 |
break;
|
156 |
case SerialPortEvent.CD:
|
157 |
System.out.print("CD\n"); |
158 |
break;
|
159 |
case SerialPortEvent.CTS:
|
160 |
System.out.print("CTS\n"); |
161 |
break;
|
162 |
case SerialPortEvent.DSR:
|
163 |
System.out.print("DSR\n"); |
164 |
break;
|
165 |
case SerialPortEvent.RI:
|
166 |
System.out.print("RI\n"); |
167 |
break;
|
168 |
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
|
169 |
System.out.print("Out Buff Empty\n"); |
170 |
break;
|
171 |
case SerialPortEvent.DATA_AVAILABLE:
|
172 |
// waitForInput = false;
|
173 |
System.out.print("Data Available\n"); |
174 |
break;
|
175 |
} |
176 |
} |
177 |
} |
178 |
|