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.

178 lines
4.7 KiB

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