Guest User

Untitled

a guest
Jul 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import gnu.io.*;
  4. import java.sql.*;
  5.  
  6.  
  7. public class trying5 implements Runnable, SerialPortEventListener {
  8. static Enumeration portList;
  9. static CommPortIdentifier portId;
  10.  
  11. SerialPort serialPort;
  12. InputStream inputStream;
  13. Thread readThread;
  14. Connection con;
  15.  
  16. public static void main(String[] args) {
  17. portList = CommPortIdentifier.getPortIdentifiers();
  18. while (portList.hasMoreElements()) {
  19. portId = (CommPortIdentifier) portList.nextElement();
  20. if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
  21. if (portId.getName().equals("COM3")) {
  22. trying5 reader = new trying5();
  23.  
  24. }
  25. }
  26. }
  27. }
  28.  
  29. public trying5() {
  30.  
  31.  
  32. try {
  33. serialPort = (SerialPort) portId.open("trying5Application", 2000);
  34. }
  35. catch (PortInUseException e)
  36.  
  37. {
  38. System.out.println(e);
  39. }
  40.  
  41. try {
  42. inputStream = serialPort.getInputStream();
  43. }
  44. catch (IOException e)
  45.  
  46. {
  47. System.out.println(e);
  48. }
  49.  
  50. try {
  51. serialPort.addEventListener(this);
  52. }
  53. catch (TooManyListenersException e)
  54.  
  55. {
  56. System.out.println(e);
  57. }
  58.  
  59. serialPort.notifyOnDataAvailable(true);
  60.  
  61. try {
  62. serialPort.setSerialPortParams(9600,
  63. SerialPort.DATABITS_8,
  64. SerialPort.STOPBITS_1,
  65. SerialPort.PARITY_NONE);
  66.  
  67. }
  68.  
  69. catch (UnsupportedCommOperationException e)
  70. {
  71. System.out.println(e);
  72. }
  73.  
  74. readThread = new Thread(this);
  75. readThread.start();
  76.  
  77. }
  78.  
  79. public void run() {
  80. try {
  81. Thread.sleep(20000);
  82. }
  83. catch (InterruptedException e)
  84. {
  85. System.out.println(e);
  86. }
  87. }
  88.  
  89. public void serialEvent(SerialPortEvent event) {
  90. switch(event.getEventType()) {
  91. case SerialPortEvent.BI:
  92. case SerialPortEvent.OE:
  93. case SerialPortEvent.FE:
  94. case SerialPortEvent.PE:
  95. case SerialPortEvent.CD:
  96. case SerialPortEvent.CTS:
  97. case SerialPortEvent.DSR:
  98. case SerialPortEvent.RI:
  99. case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
  100. break;
  101.  
  102. case SerialPortEvent.DATA_AVAILABLE:
  103.  
  104. byte[] readBuffer = new byte[20];
  105.  
  106.  
  107.  
  108. // print to console
  109.  
  110. try {
  111. while (inputStream.available() > 0) {
  112. int numBytes = inputStream.read(readBuffer);
  113.  
  114.  
  115. }
  116.  
  117.  
  118.  
  119. String newtuple = new String(readBuffer);
  120.  
  121.  
  122.  
  123. usercon newcon = new usercon(con, newtuple);
  124.  
  125. System.out.print(newtuple + "n");
  126.  
  127. } catch (IOException e)
  128. {
  129. System.out.println(e);
  130. }
  131. break;
  132. }
  133. }
  134. }
  135.  
  136. System.out.print(newtuple + "n");
  137.  
  138. while (inputStream.available() > 0) {
  139. int numBytes = inputStream.read(readBuffer);
  140. }
  141.  
  142. byte[] buf = new byte[12];
  143. int len = is.read(buf,0,buf.length);
  144. if (len != buf.length ) {
  145. throw new RuntimeException("the stream is closed and i failed to read enough data");
  146. }
Add Comment
Please, Sign In to add comment