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