Debian Codegraph @

contrib/SimpleRead.java

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