Sunday, June 29, 2008

Serial Port Access using RXTX in JAVA

This is the sample code to access serial port using RXTX in java,,,,
the packages can be downloded from rxtx.org and to be placed in the locations specified in that link,,,,,




import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Enumeration;

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

public class Port {

private CommPortIdentifier portId;
private SerialPort port;
private OutputStreamWriter out;
private InputStreamReader in;

public void open(String comportUsed) {
try {

Enumeration portList;
String defaultPort;

String osname = System.getProperty("os.name", "").toLowerCase();
if (osname.startsWith("windows")) {
// windows
defaultPort = "COM1";
} else if (osname.startsWith("linux")) {
// linux
defaultPort = "/dev/ttyS0";
} else if (osname.startsWith("mac")) {
// mac
defaultPort = "????";
} else {
System.out
.println("Sorry, your operating system is not supported");

}

portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(comportUsed)) {
System.out.println("Found port: " + comportUsed);
break;
}
}
}

this.port = (SerialPort) this.portId.open(comportUsed, 2000);
port.setSerialPortParams(19200, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
in = new InputStreamReader(port.getInputStream());
out = new OutputStreamWriter(port.getOutputStream());
System.err.println("Port Open:Success");

} catch (Exception e) {
System.err.println("Port Open:Failed");
System.out.println("Port is already used from other application,");
System.out.println("Close that application and re-run this application");
}
}

private void write(String s) throws Exception {

out.write(s);
out.flush();
}

private void write(byte s) throws Exception {
out.write(s);
out.flush();
}

private void write(int s) throws Exception {
out.write(s);
out.flush();
}


private String read() throws Exception {
int n, i;
char c;
String answer = new String("");

for (i = 0; i <>
while (in.ready()) {
n = in.read();
if (n != -1) {
c = (char) n;
answer = answer + c;
Thread.sleep(1);
} else
break;
}
delay(1);
}


return answer;
}



private void delay(int a) {
try {
Thread.sleep(a);
} catch (InterruptedException e) {
}
}



public void close() throws Exception {
try {
port.close();
System.err.println("close port");
} catch (Exception e) {
System.err.println("Error: close port failed: " + e);
}
}


}


1 comment:

  1. Please, correct the code.
    Obviously, the part of code:

    for (i = 0; i <>
    while (in.ready()) {

    Can't work (the if() is not closed)

    ReplyDelete

Powered By Blogger