ComWriter.java 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.UnsupportedCommOperationException;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.OutputStream;
  10. import java.util.ArrayList;
  11. // Com1Writer.java 1.1 12/02/22 voor BlueJ omgeving
  12. // @author Jan Woolderink
  13. // @author Johan Talboom
  14. // @version 24/12/13
  15. //
  16. public class ComWriter
  17. {
  18. private CommPortIdentifier portId;
  19. private static SerialPort serialPort;
  20. private OutputStream outputStream;
  21. private InputStream inputStream;
  22. public ComWriter()
  23. {
  24. initCom();
  25. }
  26. private void initCom()
  27. {
  28. try
  29. {
  30. portId = CommPortIdentifier.getPortIdentifier("/dev/rfcomm0");
  31. } catch (NoSuchPortException e) { e.printStackTrace(); }
  32. try
  33. {
  34. serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000);
  35. } catch (PortInUseException e) { e.printStackTrace(); }
  36. try
  37. {
  38. outputStream = serialPort.getOutputStream();
  39. inputStream = serialPort.getInputStream();
  40. } catch (IOException e) { e.printStackTrace(); }
  41. try
  42. {
  43. serialPort.setSerialPortParams(9600,
  44. SerialPort.DATABITS_8,
  45. SerialPort.STOPBITS_1,
  46. SerialPort.PARITY_NONE);
  47. } catch (UnsupportedCommOperationException e) {}
  48. System.out.println("COM6: ");
  49. System.out.println(serialPort.getBaudRate());
  50. System.out.println(serialPort.getDataBits());
  51. System.out.println(serialPort.getStopBits());
  52. }
  53. private void setBaudRate(int baudRate)
  54. {
  55. int dataBits = serialPort.getDataBits();
  56. int stopBits = serialPort.getStopBits();
  57. int parity = serialPort.getParity();
  58. try
  59. {
  60. serialPort.setSerialPortParams(baudRate,dataBits, stopBits, parity);
  61. } catch (UnsupportedCommOperationException e)
  62. {System.out.println("Onbekende baudrate");}
  63. }
  64. public void writeString(String tekst)
  65. {
  66. try
  67. {
  68. outputStream.write(tekst.getBytes());
  69. } catch (IOException e) {}
  70. }
  71. //route verzenden naar de boebot
  72. public void sendRoute(ArrayList<Character> route){
  73. String routestring = "?";
  74. for(Character step:route){
  75. routestring = routestring + step;
  76. }
  77. routestring = routestring + "?";
  78. writeString(route.toString());
  79. }
  80. public void sendSpeed(int snelheid){
  81. String st = ""+snelheid;
  82. st="f"+(int)((snelheid-1)/10);
  83. writeString(st);
  84. }
  85. }