java talks over usb
I was working on RFID, and had to write a stand alone application which can talk to the desktop reader, over USB, and pull the data from the reader (UDL50 reader). I started exploring various APIs, and I loved the Java comm API, which worked great on windows XP. Wanted to share with you guys some learnings..just in case u guys are stuck with similar problem…
1) Download RXTX implementation of Java Comm API. Java provides a specification for communicating with devices over both serial and parallel ports. The following files are important to have rxtxParallel.dll, rxtxSerial.dll, RXTXcomm.jar
2) Copy the DLLs (which have the native implementation code) to system32 or keep them in the PATH
3) move the RXTXcomm.jar file into the CLASSPATH
4) Find below the sample code that implements the Serial port communication
public class UDL50PortReader extends Observable implements Runnable, SerialPortEventListener {
/*Serial Port Event listener is the callback when some data comes on the port.*/
//Port ID
static CommPortIdentifier portId;
//List of all the ports
static Enumeration portList;
InputStream inputStream;
//The actual port object
SerialPort serialPort;
//I created a thread which keeps reading the data
Thread readThread;
// A flag which stops the threads and exits
private boolean shutDown;
private void init(String comPort) throws CommPortIOException{
//Get All the port identifiers
portList = CommPortIdentifier.getPortIdentifiers();
boolean portFound = false;
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement(); //Get each port
System.out.println("Port Type"+ portId.getPortType() + " Port Name " + portId.getName());
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { //Check the port type
if (portId.getName().equals(comPort)) {
portFound = true;
try {
serialPort
= (SerialPort) portId.open("portname typically COM1 COM2 etc", 2000);
inputStream = serialPort.getInputStream(); //Get the input stream
serialPort.addEventListener(this); //register a call back
serialPort.notifyOnDataAvailable(true); //enable call back
serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE); //Set the baud parameters
OutputStream outputStream = serialPort.getOutputStream(); //Create output stream
outputStream.write("data that needs to be written to the port..typically there will be lot of init data written");
outputStream.flush();
outputStream.close();
} catch (PortInUseException e) {
e.printStackTrace();
throw new CommPortIOException(Messages.getString("UDL50PortReader.1"), e);
} catch (IOException e) {
e.printStackTrace();
throw new CommPortIOException(Messages.getString("UDL50PortReader.2"), e);
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
throw new CommPortIOException(Messages.getString("UDL50PortReader.3"), e); //$NON-NLS-1$
} catch (TooManyListenersException e) {
e.printStackTrace();
throw new CommPortIOException(Messages.getString("UDL50PortReader.4"), e); //$NON-NLS-1$
}
readThread = new Thread(this, "UDL50 Listener Thread");
readThread.start();
}
}
}
if (!portFound) {
throw new CommPortIOException(Messages.getString("UDL50PortReader.5")); //$NON-NLS-1$
}
shutDown = false;
}
public UDL50PortReader(String comPort) throws CommPortIOException {
init(comPort);
}
public void run() {
try {
while (!shutDown) {
Thread.sleep(10000);
}
cleanUp();
} catch (InterruptedException e) {
System.out.println(e);
}
}
private void cleanUp() {
try {
serialPort.removeEventListener();
inputStream.close();
serialPort.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[50];
int numBytes = 0;
try {
while (inputStream.available() > 0) {
numBytes = inputStream.read(readBuffer);
}
int x = 5;
if (readBuffer[x] == 0x41) {
// Tag data
StringBuffer tag = new StringBuffer(""); //$NON-NLS-1$
byte[] hex = new byte[24];
int index = 0;
//process the data
this.setChanged();
this.notifyObservers();
}
}
} catch (IOException e) {
System.out.println(e);
this.notifyObservers(e);
}
break;
}
}
public void shutDown() {
shutDown = true;
}
public String getCurrentTagRead() {
return currentTagRead;
}
public void setCurrentTagRead(String currentTagRead) {
this.currentTagRead = currentTagRead;
}
public static String getHexString(byte[] raw) throws UnsupportedEncodingException {
byte[] hex = new byte[2 * raw.length];
int index = 0;
for (int i = 0; i >> 4];
hex[index++] = HEX_CHAR_TABLE[v & 0xF];
}
return new String(hex, "ASCII");
}
public static void main(String[] args) {
byte b[] = {(byte) 0xff, 0x05};
try {
System.out.println(UDL50PortReader.getHexString(b));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}