Log in

View Full Version : openning port with java


Badguy13
20th November 2011, 01:09 AM
ini sya mau share sedit ilmu yg udah saya dapetin tentang bagaimana cara membuka port dengan menggunakan java n command prom:



/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/



package child;



import java.io.*;

import javax.swing.*;

import POSfix.Config;



/**

*

* @author lephoe

*/

public class PoleDisplay {

Runtime rt = Runtime.getRuntime();

Config conf = new Config();

Process p = null;

public String portname;

public String newPort;



public void transaksi(String msg){

FileInputStream fis = null;

try{

fis = new FileInputStream("config");

}

catch(Exception e){

JOptionPane.showMessageDialog(null, "Please configure your port",

"Port Status", JOptionPane.WARNING_MESSAGE);

}



try{

char data;

String port = "";

int temp;

do

{

temp = fis.read();

data = (char)temp;

port = port + data;

} while(temp!=-1);

portname = port;

int panjang = portname.length();

newPort = portname.substring(0, panjang-1);

fis.close();

}

catch(Exception e){

JOptionPane.showMessageDialog(null, "Cannot reading file",

"Configure", JOptionPane.WARNING_MESSAGE);

}



String cmd[] = {

"c:\\Windows\\system32\\cmd.exe", "/c",

"start", "/min",

"c:\\Windows\\system32\\mode.com", newPort,

"baud=9600", "parity=n", "data=8",

"stop=1",

};

try {

p = rt.exec( cmd );

if( p.waitFor() != 0 ) {

System.out.println("Error executing command: " + cmd );

System.exit( -1 );

}

byte data[] = msg.getBytes();

byte data1[] = "Sutisna".getBytes();

FileOutputStream fos = new FileOutputStream( newPort );

BufferedOutputStream bos = new BufferedOutputStream( fos );

fos.write(0x0c); //acsii clear screen

fos.write(data);

fos.write(0x0a); //ascii newline

fos.write(data1);

fos.close();

}

catch( Exception e ) {

e.printStackTrace();

}

}



public void welcome(){

FileInputStream fis = null;

try{

fis = new FileInputStream("config");



try{

char data;

String port = "";

int temp;

do

{

temp = fis.read();

data = (char)temp;

port = port + data;

} while(temp!=-1);

portname = port;

int panjang = portname.length();

newPort = portname.substring(0, panjang-1);

fis.close();

}

catch(Exception e){

JOptionPane.showMessageDialog(null, "Cannot reading file",

"Configure", JOptionPane.WARNING_MESSAGE);

}

}

catch(Exception e){

JOptionPane.showMessageDialog(null, "Please configure your port",

"Port Status", JOptionPane.WARNING_MESSAGE);

}



String cmd[] = {

"c:\\Windows\\system32\\cmd.exe", "/c",

"start", "/min",

"c:\\Windows\\system32\\mode.com", newPort,

"baud=9600", "parity=n", "data=8",

"stop=1",

};



try {

p = rt.exec( cmd );

if( p.waitFor() != 0 ) {

System.out.println("Error executing command: " + cmd );

System.exit( -1 );

}

byte data[] = "Have A Nice Day".getBytes();

byte data1[] = "And Thanks".getBytes();

FileOutputStream fos = new FileOutputStream( newPort );

BufferedOutputStream bos = new BufferedOutputStream( fos );

fos.write(0x0c); //acsii clear screen

fos.write(data);

fos.write(0x0a); //newline

fos.write(data1);

fos.close();

}

catch( Exception e ) {

e.printStackTrace();

}

}

}





mudah2an gk repost gan, n bleh kalo berkenan bagi :melonndan: nya donk



:ceriwislove::loveindonesia

</div>