Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. void connect ( String portName ) throws Exception
  2. {
  3. CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
  4. if ( portIdentifier.isCurrentlyOwned() )
  5. {
  6. System.out.println("Error: Port is currently in use");
  7. }
  8. else
  9. {
  10. CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
  11.  
  12. if ( commPort instanceof SerialPort )
  13. {
  14. SerialPort serialPort = (SerialPort) commPort;
  15. serialPort.setSerialPortParams(57600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
  16.  
  17. InputStream in = serialPort.getInputStream();
  18. OutputStream out = serialPort.getOutputStream();
  19.  
  20. (new Thread(new SerialWriter(out))).start();
  21.  
  22. serialPort.addEventListener(new SerialReader(in));
  23. serialPort.notifyOnDataAvailable(true);
  24.  
  25. }
  26. else
  27. {
  28. System.out.println("Error: Only serial ports are handled by this example.");
  29. }
  30. }
  31. }
  32.  
  33. /**
  34. * Handles the input coming from the serial port. A new line character
  35. * is treated as the end of a block in this example.
  36. */
  37. public static class SerialReader implements SerialPortEventListener
  38. {
  39. Scanner scanner;
  40. private InputStream in;
  41. private byte[] buffer = new byte[1024];
  42.  
  43. public SerialReader ( InputStream in )
  44. {
  45. this.in = in;
  46. this.scanner = new Scanner(in);
  47. }
  48.  
  49. public void serialEvent(SerialPortEvent event) {
  50. int data;
  51. StringBuilder value = new StringBuilder();
  52.  
  53. if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
  54. try
  55. {
  56. int len = 0;
  57. while ( ( data = in.read()) > -1 )
  58. {
  59. if ( data == 'n' ) {
  60. break;
  61. }
  62. buffer[len++] = (byte) data;
  63. }
  64. String s = new String(buffer,0,len);
  65. // String s = new String(buffer, Charset.forName("UTF-32"));
  66.  
  67.  
  68.  
  69. }
  70. catch ( IOException e )
  71. {
  72. e.printStackTrace();
  73. System.exit(-1);
  74. }
  75. }
  76. }
  77.  
  78. }
  79.  
  80. /** */
  81. public static class SerialWriter implements Runnable
  82. {
  83. OutputStream out;
  84.  
  85. public SerialWriter ( OutputStream out )
  86. {
  87. this.out = out;
  88. }
  89.  
  90. public void run ()
  91. {
  92. try
  93. {
  94. int c = 0;
  95. while ( ( c = System.in.read()) > -1 )
  96. {
  97. this.out.write(c);
  98. }
  99. }
  100. catch ( IOException e )
  101. {
  102. e.printStackTrace();
  103. System.exit(-1);
  104. }
  105. }
  106. }
  107.  
  108.  
  109.  
  110. public static void main ( String[] args )
  111. {
  112. try
  113. {
  114. (new TwoWaySerialComm()).connect("COM7");
  115. }
  116. catch ( Exception e )
  117. {
  118. // TODO Auto-generated catch block
  119. e.printStackTrace();
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement