i'm trying to access the data from an RFID reader connected to PC through USB, i came to know that i need to work with RXTX library and after a long study came up to code with rxtx but couldn't cope up with what i expected. My code goes like this..
program has been compiled successfully, but at runtime it showed an exception in main block where i have high-lighted. I couldn't know why it couldn't access the data from the reader. Is there any problem with code..
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class mainrdr
{
/** Milliseconds to block while waiting for port open */
private static final int TIME_OUT = 2000;
private static final int DATA_RATE = 9600; /** Default bits per second for COM port. */
public mainrdr()
{
super();
}
void connect(String portName) throws Exception
{
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if(portIdentifier.isCurrentlyOwned())
{
System.out.println("Error: Port is currently in use");
}
else
{
CommPort commPort = portIdentifier.open(this.getClass().getName(),TIME_OUT);
if(commPort instanceof SerialPort)
{
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
InputStream in = serialPort.getInputStream();
(new Thread(new SerialReader(in))).start(); //
}
else
{
System.out.println("Error: Only serial ports are handled by this example.");
}
}
}
public static class SerialReader implements Runnable
{
InputStream in;
public SerialReader(InputStream in)
{
this.in = in;
}
public void run()
{
byte[] buffer = new byte[1024];
int len = -1;
try
{
while((len = this.in.read(buffer))>-1)
{
System.out.print(new String(buffer,0,len));
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
try
{
[size="5"][b](new mainrdr()).connect(args[0]);[/b][/size]
}
catch(Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
program has been compiled successfully, but at runtime it showed an exception in main block where i have high-lighted. I couldn't know why it couldn't access the data from the reader. Is there any problem with code..