ComWriter.java 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. // Com1Writer.java 1.1 12/02/22 voor BlueJ omgeving
  11. // @author Jan Woolderink
  12. // @author Johan Talboom
  13. // @version 24/12/13
  14. //
  15. public class ComWriter
  16. {
  17. private CommPortIdentifier portId;
  18. private static SerialPort serialPort;
  19. private OutputStream outputStream;
  20. private InputStream inputStream;
  21. public ComWriter()
  22. {
  23. initCom();
  24. }
  25. private void initCom()
  26. {
  27. try
  28. {
  29. portId = CommPortIdentifier.getPortIdentifier("/dev/rfcomm0");
  30. } catch (NoSuchPortException e) { e.printStackTrace(); }
  31. try
  32. {
  33. serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000);
  34. } catch (PortInUseException e) { e.printStackTrace(); }
  35. try
  36. {
  37. outputStream = serialPort.getOutputStream();
  38. inputStream = serialPort.getInputStream();
  39. } catch (IOException e) { e.printStackTrace(); }
  40. try
  41. {
  42. serialPort.setSerialPortParams(9600,
  43. SerialPort.DATABITS_8,
  44. SerialPort.STOPBITS_1,
  45. SerialPort.PARITY_NONE);
  46. } catch (UnsupportedCommOperationException e) {}
  47. System.out.println("COM6: ");
  48. System.out.println(serialPort.getBaudRate());
  49. System.out.println(serialPort.getDataBits());
  50. System.out.println(serialPort.getStopBits());
  51. }
  52. private void setBaudRate(int baudRate)
  53. {
  54. int dataBits = serialPort.getDataBits();
  55. int stopBits = serialPort.getStopBits();
  56. int parity = serialPort.getParity();
  57. try
  58. {
  59. serialPort.setSerialPortParams(baudRate,dataBits, stopBits, parity);
  60. } catch (UnsupportedCommOperationException e)
  61. {System.out.println("Onbekende baudrate");}
  62. }
  63. public void writeString(String tekst)
  64. {
  65. try
  66. {
  67. outputStream.write(tekst.getBytes());
  68. } catch (IOException e) {}
  69. }
  70. }