import gnu.io.CommPortIdentifier;  
import gnu.io.SerialPort; 
import java.io.IOException;  
import java.io.InputStream;   
   
public class RS232Example {  
    public void connect(String portName) throws Exception {  
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);  
   
        if (portIdentifier.isCurrentlyOwned()) {  
            System.out.println("Port in use!");  
        } else {  
            // points who owns the port and connection timeout  
            SerialPort serialPort = (SerialPort) portIdentifier.open("RS232Example", 2000);  
              
            // setup connection parameters  
            serialPort.setSerialPortParams(  
                9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);  
   
            // setup serial port writer  
            CommPortSender.setWriterStream(serialPort.getOutputStream());  
              
            // setup serial port reader  
            new CommPortReceiver(serialPort.getInputStream()).start();  
        }  
    }  

  public static String getHex( byte [] raw ) {
    String HEXES = "0123456789ABCDEF";
    if ( raw == null ) {
      return null;
    }
    final StringBuilder hex = new StringBuilder( 2 * raw.length );
    for ( final byte b : raw ) {
      hex.append(HEXES.charAt((b & 0xF0) >> 4))
         .append(HEXES.charAt((b & 0x0F)));
    }
    return hex.toString();
  }  
    public static void main(String[] args) throws Exception {  
          
        // connects to the port which name (e.g. COM1) is in the first argument  
        new RS232Example().connect("COM3");  
          
        // send HELO message through serial port using protocol implementation  

        //byte b[]=new ProtocolImpl().getMessage("1 3 0 3 0 2 52 11");
        byte b[]={(byte)1,(byte)3,(byte)0,(byte)3,(byte)0,(byte)2,(byte)52,(byte)11};
        System.out.println(getHex(b ));
        for(int i=0;i<b.length;i++)
        {System.out.println("i="+i+"b="+b[i]);}
        CommPortSender.send(b); 
        }  
}
