Archive

Archive for May, 2010

java talks over usb

May 4, 2010 2 comments

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();
}
}
}

Categories: Technology Tags: , , , , ,

hi5 to html5

May 3, 2010 5 comments

Last few days I have been digging around to understand what is so great about HTML5…soon I realized, that this is what I always wanted, when ever I did HTML coding…HTML5 seems to be logically the right step towards RIA.

Why should I install plugins such as Silverlight, Java FX etc?? why can’t the browsers have it in-built?? why can’t the HTML move beyond SUBMIT button, INPUT elements etc??…HTML5 is my dream come true…and I was really happy to read in the news that Microsoft, Mac and everybody else seems to embrace this standard…Does that mean we are going to get rid of JavaScript based Dojo or Silverlight or Flex or JavaFX??? I don’t think so, I think we still need them for a VRIA (very rich internet application…don’t search the web for VRIA…its a word, I coined, and I use it for myself ;-) to refer to highly interactive and rich internet application.)..HTML5 should take care of my basic RIA applications.

So here are a few things I learnt about HTML5, that I would like to share with you all.

Semantics – HTML5 has simplified the semantics. the elements are more meaningful, and readable..to give a few examples for header, for navigation (menus), – generic sections, – for content, – footer…and there are a few new elements, which we always wanted, – to insert audio content, – for video…The API is also enhanced to support these new elements..so writing JavaScript to handle the HTML5 elements just got better.
There is one more interesting element , which can be used to draw anything (using JavaScript). to draw menus, which supports both toolbar menu and context menu…no need to struggle with JavaScript to create menus anymore. ain’t that cool?
Then there is element which can be used to draw the data grids, tables..and then there are , and whole host of FORM elements such as datetime, range, email, color, number etc..please go check out the HTML5 semantics.

Native DnD support – Drag Drop was a big challenge to implement on web pages, I have used dojo and various other JavaScript based frameworks to get DnD working, and at any point, I can never promise that DnD will work for sure :-) its always tough to get these things working..and now its a big relief to see HTML5 comes with native DnD support..so how simple is DnD?? create a 2 divs, one the drag source and the other drag target. Now implement the JavaScript events ondragstart and ondragdrop..and see it working..ofcourse there are a few things we need to do to get the events working ..which I will cover in my next blogs with live examples

Web Sockets – Now we can use JavaScript to create websockets from the web page and communicate with the other servers…yes CROSS DOMAIN..how cool is that??..and these sockets support both normal(wss) and secured (wss)..now imagine how this can help in building HTML5 frontends with REST backends?? that too CROSS DOMAIN REST…or are we going back to the classic client server model.. :-) ..but more built on standards such as HTML, XML, browser runtime based??

Server side events – Oh what a relief?? no need to keep polling the server if the latest data has arrived anymore…just relax..the server will send a notification, when the data has arrived. I think this is a huge step in making AJAX easier.

Offline mode – HTML5 comes with native offline support, which is much needed as we are not just looking HTML for the PC based browsers..but also to be used on handhelds..where the offline mode is very much required. So HTML5 comes with the feature to store the data and work offline – Both Session Storage and Local Storage features.

So is that all?? no there are lot more..these are the top things which caught my eye…will get back with more deep dives and examples…until then..what do you guys think about HTML5…

Categories: Technology Tags: , , , ,
Follow

Get every new post delivered to your Inbox.