You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

281 lines
7.8 KiB

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