code_junkie

reading serial port in java

Nov 12th, 2011
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*; //import gnu.io.*;
  3. import javax.comm.*;
  4.  
  5. public class SimpleRead implements Runnable, SerialPortEventListener {
  6. static CommPortIdentifier portId;
  7. static Enumeration portList;
  8.  
  9. InputStream inputStream;
  10. SerialPort serialPort;
  11. Thread readThread;
  12. byte[] readBuffer;
  13.  
  14. public static void main(String[] args) {
  15. portList = CommPortIdentifier.getPortIdentifiers();
  16. System.out.println("portList... " + portList);
  17. while (portList.hasMoreElements()) {
  18. portId = (CommPortIdentifier) portList.nextElement();
  19. if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
  20. System.out.println("port identified is Serial.. "
  21. + portId.getPortType());
  22. if (portId.getName().equals("COM2")) {
  23. System.out.println("port identified is COM2.. "
  24. + portId.getName());
  25. // if (portId.getName().equals("/dev/term/a")) {
  26. SimpleRead reader = new SimpleRead();
  27. } else {
  28. System.out.println("unable to open port");
  29. }
  30. }
  31. }
  32. }
  33.  
  34. public SimpleRead() {
  35. try {
  36. System.out.println("In SimpleRead() contructor");
  37. serialPort = (SerialPort) portId.open("SimpleReadApp1111",500);
  38. System.out.println(" Serial Port.. " + serialPort);
  39. } catch (PortInUseException e) {
  40. System.out.println("Port in use Exception");
  41. }
  42. try {
  43. inputStream = serialPort.getInputStream();
  44. System.out.println(" Input Stream... " + inputStream);
  45. } catch (IOException e) {
  46. System.out.println("IO Exception");
  47. }
  48. try {
  49. serialPort.addEventListener(this);
  50.  
  51. } catch (TooManyListenersException e) {
  52. System.out.println("Tooo many Listener exception");
  53. }
  54. serialPort.notifyOnDataAvailable(true);
  55. try {
  56.  
  57. serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
  58. SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
  59.  
  60. // no handshaking or other flow control
  61. serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
  62.  
  63. // timer on any read of the serial port
  64. serialPort.enableReceiveTimeout(500);
  65.  
  66. System.out.println("................");
  67.  
  68. } catch (UnsupportedCommOperationException e) {
  69. System.out.println("UnSupported comm operation");
  70. }
  71. readThread = new Thread(this);
  72. readThread.start();
  73. }
  74.  
  75. public void run() {
  76. try {
  77. System.out.println("In run() function ");
  78. Thread.sleep(500);
  79. // System.out.println();
  80. } catch (InterruptedException e) {
  81. System.out.println("Interrupted Exception in run() method");
  82. }
  83. }
  84.  
  85. public void serialEvent(SerialPortEvent event) {
  86.  
  87. // System.out.println("In Serial Event function().. " + event +
  88. // event.getEventType());
  89. switch (event.getEventType()) {
  90. /*
  91. * case SerialPortEvent.BI: case SerialPortEvent.OE: case
  92. * SerialPortEvent.FE: case SerialPortEvent.PE: case SerialPortEvent.CD:
  93. * case SerialPortEvent.CTS: case SerialPortEvent.DSR: case
  94. * SerialPortEvent.RI: case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break;
  95. */
  96. case SerialPortEvent.DATA_AVAILABLE:
  97. readBuffer = new byte[8];
  98.  
  99. try {
  100.  
  101. while (inputStream.available()>0) {
  102.  
  103. int numBytes = inputStream.read(readBuffer);
  104. // System.out.println("Number of bytes read " + numBytes);
  105. }
  106.  
  107. System.out.print(new String(readBuffer));
  108.  
  109. } catch (IOException e) {
  110. System.out.println("IO Exception in SerialEvent()");
  111. }
  112. break;
  113. }
  114. // System.out.println();
  115. /* String one = new String(readBuffer);
  116. char two = one.charAt(0);
  117. System.out.println("Character at three: " + two);*/
  118. }
  119.  
  120. }
  121.  
  122. while (inputStream.available()>0) {
  123. int numBytes = inputStream.read(readBuffer);
  124. System.out.print(new String(readBuffer));
  125. }
Add Comment
Please, Sign In to add comment