root / trunk / extensions / extGPS / test / SimpleRead.java @ 11445
History | View | Annotate | Download (8.44 KB)
1 |
/*
|
---|---|
2 |
* This class is one end of a test pair of programs
|
3 |
* SimpleSnuV1.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 |
import gnu.io.CommPortIdentifier; |
13 |
import gnu.io.PortInUseException; |
14 |
import gnu.io.SerialPort; |
15 |
import gnu.io.SerialPortEvent; |
16 |
import gnu.io.SerialPortEventListener; |
17 |
import gnu.io.UnsupportedCommOperationException; |
18 |
|
19 |
import java.io.IOException; |
20 |
import java.io.InputStream; |
21 |
import java.io.OutputStream; |
22 |
import java.util.Enumeration; |
23 |
import java.util.TooManyListenersException; |
24 |
|
25 |
public class SimpleRead implements Runnable, SerialPortEventListener { |
26 |
|
27 |
static CommPortIdentifier portId;
|
28 |
static Enumeration portList; |
29 |
SerialPort serialPort = null;
|
30 |
InputStream inputStream;
|
31 |
OutputStream outputStream;
|
32 |
|
33 |
Thread readThread;
|
34 |
String aS = ""; |
35 |
int numBytes = 0; |
36 |
String num = "021891383"; |
37 |
String rst = "atz"; |
38 |
String dial ="atd"; |
39 |
String outData = ""; |
40 |
String outDataBuff = ""; |
41 |
boolean running = true; |
42 |
boolean process = true; |
43 |
boolean waitForInput = true; |
44 |
boolean fatalErr = false; |
45 |
String errMessage = ""; |
46 |
|
47 |
public static void main(String[] args) { |
48 |
if (args.length < 1) { |
49 |
System.out.print("SimpleRead.class /dev/ttyxx\n"); |
50 |
System.exit(-1); |
51 |
} |
52 |
portList = CommPortIdentifier.getPortIdentifiers(); |
53 |
while (portList.hasMoreElements()) {
|
54 |
portId = (CommPortIdentifier) portList.nextElement(); |
55 |
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
|
56 |
// if (portId.getName().equals("COM1")) {
|
57 |
if (portId.getName().equals(args[0])) { |
58 |
SimpleRead reader = new SimpleRead();
|
59 |
} |
60 |
} |
61 |
} |
62 |
} |
63 |
|
64 |
public SimpleRead() {
|
65 |
try {
|
66 |
serialPort = (SerialPort) portId.open("SimpleReadApp", 4800);// 2000); |
67 |
} catch (PortInUseException e) {}
|
68 |
try {
|
69 |
inputStream = serialPort.getInputStream(); |
70 |
outputStream = serialPort.getOutputStream(); |
71 |
} catch (IOException e) {} |
72 |
try {
|
73 |
serialPort.addEventListener(this);
|
74 |
} catch (TooManyListenersException e) {} |
75 |
serialPort.notifyOnDataAvailable(true);
|
76 |
try {
|
77 |
serialPort.setSerialPortParams(4800, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
|
78 |
//serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
|
79 |
} catch (UnsupportedCommOperationException e) {}
|
80 |
|
81 |
byte[] readBuffer = new byte[2048]; |
82 |
try {
|
83 |
int bytes;
|
84 |
do {
|
85 |
bytes = inputStream.read(readBuffer); |
86 |
byte[] characters = new byte[bytes]; |
87 |
for (int i = 0; i < characters.length; i++) { |
88 |
characters[i] = readBuffer[i]; |
89 |
} |
90 |
String str =new String(readBuffer); |
91 |
System.out.print(str.substring(0, bytes)); |
92 |
|
93 |
} while (bytes >-1); |
94 |
} catch (IOException e) { |
95 |
// TODO Auto-generated catch block
|
96 |
e.printStackTrace(); |
97 |
} |
98 |
// readThread = new Thread(this);
|
99 |
// readThread.start();
|
100 |
} |
101 |
|
102 |
public void run() { |
103 |
int resetCount = 0; |
104 |
while (running) {
|
105 |
if (fatalErr) {
|
106 |
System.out.print("Fatal Error...\n"); |
107 |
System.exit(1); |
108 |
} |
109 |
if (!process) {
|
110 |
try {
|
111 |
Thread.sleep(500); |
112 |
} catch (InterruptedException e) {} |
113 |
continue;
|
114 |
} |
115 |
if (num.equals("Nil")) { |
116 |
try {
|
117 |
Thread.sleep(500); |
118 |
} catch (InterruptedException e) {} |
119 |
continue;
|
120 |
} |
121 |
if (reset()) {
|
122 |
if (dial()) {
|
123 |
num = "Nil";
|
124 |
System.out.print("Yahoooo OK...\n"); |
125 |
//Here goes a class to handle any coms it gets passed the stuff it needs...
|
126 |
// SNHdlcMessage myMess = new SNHdlcMessage();
|
127 |
// String rply = myMess.processInput("loc1", "text", inputStream, outputStream);
|
128 |
// System.out.print(rply + "\n");
|
129 |
process = false;
|
130 |
running = false;
|
131 |
System.out.print("Normal Exit...\n"); |
132 |
System.exit(1); |
133 |
} else {
|
134 |
System.out.print("Dial Error...\n"); |
135 |
process = false;
|
136 |
running = false;
|
137 |
System.exit(1); |
138 |
} |
139 |
} else {
|
140 |
System.out.print("Reset Error...\n"); |
141 |
resetCount ++; |
142 |
if (resetCount > 2 ) { |
143 |
System.out.print("Reset To Many Times Error ...\n"); |
144 |
System.exit(1); |
145 |
} |
146 |
} |
147 |
try {
|
148 |
Thread.sleep(100); |
149 |
} catch (InterruptedException e) {} |
150 |
} |
151 |
} |
152 |
public void closePort(SerialPort serialPort) { |
153 |
if (serialPort != null) { |
154 |
serialPort.notifyOnDataAvailable(false);
|
155 |
serialPort.removeEventListener(); |
156 |
if (inputStream != null) { |
157 |
try {
|
158 |
inputStream.close(); |
159 |
inputStream = null;
|
160 |
} |
161 |
catch (IOException e) {} |
162 |
} |
163 |
if (outputStream != null) { |
164 |
try {
|
165 |
outputStream.close(); |
166 |
outputStream = null;
|
167 |
} |
168 |
catch (IOException e) {} |
169 |
} |
170 |
serialPort.close(); |
171 |
serialPort = null;
|
172 |
} |
173 |
} |
174 |
public boolean reset() { |
175 |
try {
|
176 |
outputStream.write(new String("$PMCAG,005,1,GGA,001").getBytes());//"atz").getBytes()); |
177 |
outputStream.write((byte)0x0D); |
178 |
System.out.print("--> atz\n"); |
179 |
} catch (IOException e) { |
180 |
System.out.print("Reset Output IO Exception"); |
181 |
return false; |
182 |
} |
183 |
int waitingCount = 0; |
184 |
waitForInput = true;
|
185 |
while (waitForInput) {
|
186 |
try {
|
187 |
Thread.sleep(100); |
188 |
} catch (InterruptedException e) {} |
189 |
waitingCount ++; |
190 |
//2 seconds for it to reset
|
191 |
if (waitingCount > 20) { |
192 |
return false; |
193 |
} |
194 |
} |
195 |
int numBytesTotal = 0; |
196 |
byte[] readBuffer = new byte[20]; |
197 |
String sReadBuff = ""; |
198 |
boolean dLoop = true; |
199 |
while (dLoop) {
|
200 |
try {
|
201 |
while (inputStream.available() > 0) { |
202 |
numBytes = inputStream.read(readBuffer); |
203 |
numBytesTotal += numBytes; |
204 |
String tmpR = new String(readBuffer); |
205 |
sReadBuff += tmpR.substring(0, numBytes);
|
206 |
} |
207 |
if (sReadBuff.indexOf("NO CARRIER") != -1) { |
208 |
dLoop = false;
|
209 |
} else if (sReadBuff.indexOf("BUSY") != -1) { |
210 |
dLoop = false;
|
211 |
} else if (sReadBuff.indexOf("NO DIALTONE") != -1) { |
212 |
dLoop = false;
|
213 |
} else if (sReadBuff.indexOf("OK") != -1) { |
214 |
dLoop = false;
|
215 |
System.out.print("<-- " + new String(sReadBuff)); |
216 |
return true; |
217 |
} else if (sReadBuff.indexOf("CONNECT") != -1) { |
218 |
dLoop = false;
|
219 |
} |
220 |
} catch (IOException e) { |
221 |
System.out.print("Reset Input IO Exception"); |
222 |
return false; |
223 |
} |
224 |
} |
225 |
System.out.print("<-- " + new String(sReadBuff)); |
226 |
return false; |
227 |
} |
228 |
public boolean dial() { |
229 |
try {
|
230 |
outputStream.write(new String("atd").getBytes()); |
231 |
outputStream.write(num.getBytes()); |
232 |
outputStream.write((byte)0x0D); |
233 |
System.out.print("--> atdxxxxx\n"); |
234 |
} catch (IOException e) { |
235 |
System.out.print("Dial Output IO Exception"); |
236 |
return false; |
237 |
} |
238 |
int waitingCount = 0; |
239 |
waitForInput = true;
|
240 |
while (waitForInput) {
|
241 |
try {
|
242 |
Thread.sleep(100); |
243 |
} catch (InterruptedException e) {} |
244 |
waitingCount ++; |
245 |
if (waitingCount > 1000) { |
246 |
return false; |
247 |
} |
248 |
} |
249 |
int numBytesTotal = 0; |
250 |
byte[] readBuffer = new byte[20]; |
251 |
String sReadBuff = ""; |
252 |
boolean dLoop = true; |
253 |
while (dLoop) {
|
254 |
try {
|
255 |
while (inputStream.available() > 0) { |
256 |
numBytes = inputStream.read(readBuffer); |
257 |
numBytesTotal += numBytes; |
258 |
String tmpR = new String(readBuffer); |
259 |
sReadBuff += tmpR.substring(0, numBytes);
|
260 |
} |
261 |
if (sReadBuff.indexOf("NO CARRIER") != -1) { |
262 |
dLoop = false;
|
263 |
} else if (sReadBuff.indexOf("BUSY") != -1) { |
264 |
dLoop = false;
|
265 |
} else if (sReadBuff.indexOf("NO DIALTONE") != -1) { |
266 |
dLoop = false;
|
267 |
} else if (sReadBuff.indexOf("OK") != -1) { |
268 |
dLoop = false;
|
269 |
} else if (sReadBuff.indexOf("CONNECT") != -1) { |
270 |
System.out.print("<-- " + new String(sReadBuff)); |
271 |
dLoop = false;
|
272 |
return true; |
273 |
} |
274 |
} catch (IOException e) { |
275 |
System.out.print("Dial Input IO Exception"); |
276 |
return false; |
277 |
} |
278 |
} |
279 |
System.out.print("<-- " + new String(sReadBuff)); |
280 |
return false; |
281 |
} |
282 |
public void serialEvent(SerialPortEvent event) { |
283 |
switch(event.getEventType()) {
|
284 |
case SerialPortEvent.BI:
|
285 |
System.out.print("BI\n"); |
286 |
case SerialPortEvent.OE:
|
287 |
System.out.print("OE\n"); |
288 |
case SerialPortEvent.FE:
|
289 |
System.out.print("FE\n"); |
290 |
case SerialPortEvent.PE:
|
291 |
System.out.print("PE\n"); |
292 |
case SerialPortEvent.CD:
|
293 |
System.out.print("CD\n"); |
294 |
case SerialPortEvent.CTS:
|
295 |
System.out.print("CTS\n"); |
296 |
case SerialPortEvent.DSR:
|
297 |
System.out.print("DSR\n"); |
298 |
case SerialPortEvent.RI:
|
299 |
System.out.print("RI\n"); |
300 |
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
|
301 |
System.out.print("Out Buff Empty\n"); |
302 |
break;
|
303 |
case SerialPortEvent.DATA_AVAILABLE:
|
304 |
waitForInput = false;
|
305 |
// System.out.print("Data Available\n");
|
306 |
break;
|
307 |
} |
308 |
} |
309 |
} |