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.

104 lines
3.0 KiB

5 years ago
  1. import gnu.io.*;
  2. import java.util.*;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. public class Simple implements SerialPortEventListener
  6. {
  7. CommPortIdentifier cpi;
  8. Enumeration ports;
  9. SerialPort port = null;
  10. InputStream input;
  11. OutputStream output;
  12. public static void main( String args[] )
  13. {
  14. boolean done = false;
  15. Simple reader = new Simple( );
  16. while ( !done )
  17. {
  18. try {
  19. Thread.sleep(100);
  20. } catch (Exception e) {}
  21. }
  22. }
  23. public Simple( )
  24. {
  25. System.out.println("Getting PortIdentifiers");
  26. ports = CommPortIdentifier.getPortIdentifiers();
  27. while ( ports.hasMoreElements() )
  28. {
  29. cpi = (CommPortIdentifier) ports.nextElement();
  30. if (cpi.getPortType() == CommPortIdentifier.PORT_SERIAL)
  31. {
  32. if ( cpi.getName().equals( "/dev/ttyS1" ) )
  33. {
  34. try {
  35. port = (SerialPort) cpi.open("Simple", 2000);
  36. port.addEventListener(this);
  37. port.notifyOnDataAvailable(true);
  38. output=port.getOutputStream();
  39. input=port.getInputStream();
  40. System.out.println("writing output");
  41. output.write( new String("123456789\0").getBytes() );
  42. } catch (Exception e)
  43. {
  44. e.printStackTrace();
  45. }
  46. }
  47. }
  48. }
  49. return;
  50. }
  51. public void serialEvent(SerialPortEvent event) {
  52. switch(event.getEventType())
  53. {
  54. case SerialPortEvent.BI:
  55. System.out.print("BI\n");
  56. case SerialPortEvent.OE:
  57. System.out.print("OE\n");
  58. case SerialPortEvent.FE:
  59. System.out.print("FE\n");
  60. case SerialPortEvent.PE:
  61. System.out.print("PE\n");
  62. case SerialPortEvent.CD:
  63. System.out.print("CD\n");
  64. case SerialPortEvent.CTS:
  65. System.out.print("CTS\n");
  66. case SerialPortEvent.DSR:
  67. System.out.print("DSR\n");
  68. case SerialPortEvent.RI:
  69. System.out.print("RI\n");
  70. case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
  71. System.out.print("Out Buff Empty\n");
  72. break;
  73. case SerialPortEvent.DATA_AVAILABLE:
  74. byte in[] = new byte[800];
  75. int ret = 0;
  76. System.out.println("Got Data Available");
  77. try {
  78. ret = input.read( in, 0, 63 );
  79. } catch (Exception e) {
  80. System.out.println("Input Exception");
  81. }
  82. System.out.println("Printing read() results");
  83. if ( ret > 0 )
  84. {
  85. try {
  86. System.out.write( in, 0, ret );
  87. System.out.println();
  88. } catch ( Exception e )
  89. {
  90. e.printStackTrace();
  91. }
  92. }
  93. System.exit( 0 );
  94. break;
  95. }
  96. }
  97. }