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