Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. package skl;
  2.  
  3. import java.io.*;
  4. import gnu.io.*;
  5. import java.util.*;
  6.  
  7. public class ArduinoConnection{
  8. private SerialPort serialPort;
  9. //array port yang akan digunakan
  10. private String PORT_NAMES[] = {
  11. "/dev/ttyACM0",
  12. "/dev/ttyACM1",
  13. "/dev/ttyACM2"};
  14. //var memasukan input ke port
  15. private InputStream input;
  16. //var memasukan output ke port
  17. private OutputStream output;
  18. //jarak untuk menunggu respon serial port
  19. private int TIME_OUT = 2000;
  20. //jumlah bit per detik
  21. private int DATA_RATE = 9600;
  22. //
  23. public CommPortIdentifier portId;
  24.  
  25. public void Setup(){
  26. portId = null;
  27. Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
  28.  
  29. //mencek port aktif
  30. while(portEnum.hasMoreElements()){
  31. CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
  32. for(String portName : PORT_NAMES) {
  33. if(currPortId.getName().equals(portName)){
  34. portId = currPortId;
  35. break;
  36. }
  37. }
  38.  
  39. //apa bila port tidak tersedia
  40. if(portId == null){
  41. System.out.println("Port Tidak Ditemukan");
  42. return;
  43. }
  44.  
  45. open();
  46. }
  47. }
  48.  
  49. public void open(){
  50. System.out.println("Open Connections");
  51. try{
  52. //membuka serial port
  53. serialPort = (SerialPort) portId.open(this.getClass().getName(),
  54. TIME_OUT);
  55.  
  56. //tentukan port parameter
  57. serialPort.setSerialPortParams(
  58. DATA_RATE,
  59. SerialPort.DATABITS_8,
  60. SerialPort.STOPBITS_1,
  61. SerialPort.PARITY_NONE);
  62. input = serialPort.getInputStream();
  63. output = serialPort.getOutputStream();
  64. }catch(Exception e){
  65. System.err.println(e.toString());
  66. }
  67. }
  68.  
  69.  
  70. public void Kirim(int pin){
  71. try {
  72. System.out.println(pin);
  73. output.write(pin);
  74. } catch (Exception e) {
  75. System.out.println(e);
  76. }
  77. }
  78.  
  79. public static void main(String[] args) throws Exception{
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement