ComWriter.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package customComponents;
  2. import gnu.io.CommPortIdentifier;
  3. import gnu.io.NoSuchPortException;
  4. import gnu.io.PortInUseException;
  5. import gnu.io.SerialPort;
  6. import gnu.io.SerialPortEvent;
  7. import gnu.io.SerialPortEventListener;
  8. import gnu.io.UnsupportedCommOperationException;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.OutputStream;
  12. import java.util.ArrayList;
  13. import java.util.TooManyListenersException;
  14. // Com1Writer.java 1.1 12/02/22 voor BlueJ omgeving
  15. // @author Jan Woolderink
  16. // @author Johan Talboom
  17. // @version 24/12/13
  18. //
  19. public class ComWriter
  20. {
  21. private CommPortIdentifier portId;
  22. private static SerialPort serialPort;
  23. private OutputStream outputStream;
  24. private InputStream in;
  25. public ComWriter()
  26. {
  27. initCom();
  28. }
  29. private void initCom()
  30. {
  31. try
  32. {
  33. portId = CommPortIdentifier.getPortIdentifier("/dev/rfcomm0");
  34. } catch (NoSuchPortException e) { e.printStackTrace(); }
  35. try
  36. {
  37. serialPort = (SerialPort) portId.open("Boebot", 2000);
  38. } catch (PortInUseException e) { e.printStackTrace(); }
  39. try
  40. {
  41. outputStream = serialPort.getOutputStream();
  42. in = serialPort.getInputStream();
  43. } catch (IOException e) { e.printStackTrace(); }
  44. try
  45. {
  46. serialPort.setSerialPortParams(9600,
  47. SerialPort.DATABITS_8,
  48. SerialPort.STOPBITS_1,
  49. SerialPort.PARITY_NONE);
  50. } catch (UnsupportedCommOperationException e) {}
  51. try {
  52. serialPort.addEventListener(new SerialPortEventListener() {
  53. @Override
  54. public void serialEvent(SerialPortEvent arg0) {
  55. System.out.println("tset");
  56. }
  57. });
  58. } catch (TooManyListenersException e) {
  59. e.printStackTrace();
  60. }
  61. System.out.println("COM6: ");
  62. System.out.println(serialPort.getBaudRate());
  63. System.out.println(serialPort.getDataBits());
  64. System.out.println(serialPort.getStopBits());
  65. }
  66. private void setBaudRate(int baudRate)
  67. {
  68. int dataBits = serialPort.getDataBits();
  69. int stopBits = serialPort.getStopBits();
  70. int parity = serialPort.getParity();
  71. try
  72. {
  73. serialPort.setSerialPortParams(baudRate,dataBits, stopBits, parity);
  74. } catch (UnsupportedCommOperationException e)
  75. {System.out.println("Onbekende baudrate");}
  76. }
  77. public InputStream getInput(){
  78. return in;
  79. }
  80. public void writeString(String tekst)
  81. {
  82. try
  83. {
  84. outputStream.write(tekst.getBytes());
  85. } catch (IOException e) {}
  86. }
  87. //route verzenden naar de boebot
  88. public void sendRoute(ArrayList<Character> route){
  89. String routestring = "?";
  90. for(Character step:route){
  91. routestring = routestring + step;
  92. }
  93. System.out.println(routestring);
  94. routestring = routestring + "?";
  95. writeString(routestring);
  96. }
  97. public void sendSpeed(int snelheid){
  98. String st = ""+snelheid;
  99. st="f"+(int)((snelheid-1)/10);
  100. writeString(st);
  101. }
  102. }