BAB 14 - Mengakses Port pada Hardware Komputer - TeachMeSoft

BAB 14 - Mengakses Port pada Hardware Komputer

BAB 14 - Mengakses Port pada Hardware Komputer

Capaian dan Indikator


1. Capaian Pembelajaran

Setelah mempelajari bab ini, mahasiswa diharapkan:
  1. Mengetahui cara menerapkan library rxtx dalam program
  2. Mengetahui cara komunikasi serial pada comm port

2. Indikator

  1. Mahasiswa mampu mengintegrasikan library rxtx dalam program
  2. Mahasiswa mampu mengimplementasikan kode program untuk melakukan komunikasi pada comm port.

Uraian Materi


Sebuah aplikasi yang melibatkan komunikasi dengan hardware komputer dapat dilakukan dalam Java, salah satunya dengan menggunakan library Java Comm yang bisa Anda unduh dari link berikut ini: http://rxtx.qbang.org/wiki/index.php/Download. Sebagai informasi, jika Anda menggunakan IDE Netbeans, maka sebaiknya Anda mengund uh rxtx versi binaryfile.

A.  Mencari COMM Port yang Aktif

Untuk mencari comm port yang sedang aktif menggunakan library rxtx, kita bisa menggunakan method di bawah ini:
public static HashSet<CommPortIdentifier> get.AvailableSerialPorts(){
  HashSet<CommPortIdentifier> h = new HashSet <CommPortIdentifier>(); 
  Enumerationt.thePort = CommPortIdentifier.getPortIdentifiers();
  while (thePorts.hasMoreElements()){
   CommPortIdentifier com = (CommPortIdentifier) thePorts.nextElement(); 
   switch (com .getPortType()) {
   case CommPortIdentifier.PORT_SERIAL:
    try{
     CommPort thePort = com.open("CommUtil", 50);
     thePort.close();
     h.add(com);
    }catch (PortInUseException e){
     System.out.println("Port, " + com.getName() +", is in use.");
    }catch (Exception e){
     System.println("Failed to open port " + com.getName());
     e.printStackTrace();
    }
   }
  }
  return h;
}


Keterangan program :


B.  Mencari Semua COMM Port yang Ada

Untuk mencari semua comm port, baik yang aktif ataupun yang tidak menggunakan library rxtx, kita bisa menggunakan method di bawah ini:
import gnu.io.*;
static void listPorts()
{
 java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
 while (portEnum.hasMoreElements())
 {
  CommPortIdentifier portIdentifier = portEnum.nextElement();
  System.out.println(portIdentifier.getName() + "-"+ getPortTypeName(portIdentifier.getPortType()));
 }
}

static String getPortTypeName(int portType)
{
 switch(portType)
 {
  case CommPortIdentifier.PORT_I2C;
   return "I2C";
  case CommPortIdentifier.PORT_PARALLEL;
   return "Parallel";
  case CommPortIdentifier.PORT_RAW;
   return "Raw";
  case CommPortIdentifier.PORT_RS485;
   return "RS485";
  case CommPortIdentifier.PORT_SERIAL;
   return "Serial";
  default:
   return "unknown type";
 }
}


Keterangan program :



C.  Komunikasi Serial

Berikut adalah contoh kode untuk melakukan komunikasi seriap pada comm port menggunakan  library rxtx.
import gnu.io.CommPort;
import gnu.io.IOException;
import gnu.io.SerialPort;

import java.io.FileDescriptor;
import java.io.IOException;
import java.io.OutputStream;

public class TwoWaySeralComm
{
 public TwoWaySeralComm()
 {
  super();
 }
 void connect(String portName) throws Exception
 {
  CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifiers(portName);
  if(portIdentifier.isCurrentlyOwned())
  {
   System.out.println("Error: port is currently is use");
  }
  else
  {
   CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
   if(commPort instanceof SerialPort)
   {
    SerialPort serialPort = (SerialPort) commPort;
    serialPort.setSerialPortParams(57600,SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
    
    InputStream in = serialPort.getInputStream();
    OutputStream out = serialPort.getOutputStream();
    
    (new Thread(new SerialReader(in)))).start();
    (new Thread(new SerialWriter(out))).start();
   }
   else
   {
    System.out.println("Error: Only serial ports are handled by this example.");
   }
  }
 }
 
 /** */
 public static class SerialReader implements Runnable
 {
  OutputStream out;
  public SerialWriter(OutputStream out)
  {
   this.out = out;
  }
  
  public void run()
  {
   try 
   {
    int c = 0;
    while((c = System.in.read()) > -1)
    {
     this.out.write(c):
    }
   }catch(IOException e)
   {
    e.printStackTrace();
   }
  }
 }
 
 public static void main(String[] args)
 {
  try
  {
   (new TwoWaySeralComm()).connect("COM3");
  }
  catch(Exception e)
  {
   //TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}


Keterangan program :




Disqus comments