how to fetch meaningful
        Posted  
        
            by 
                user1298017
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by user1298017
        
        
        
        Published on 2012-03-28T11:26:33Z
        Indexed on 
            2012/03/28
            11:28 UTC
        
        
        Read the original article
        Hit count: 267
        
javax.comm
Hi Every body, I have connected my device to serial port. I am using Javax.comm API to fetch the data from my device. On tapping of card i am reading data as below: newData = inputStream.read(); and able to fetch data : 0 128 161 132 132 132 132 132 132 132 101 65 2 226 99 98 132 132 132 132 132 132 132 132 131 134 164 132 132 132 165 134 132 196 230 167 132 132 132 132 132 132 132 197 196 132 133 132 132 164 197 132 132 198 103 255 How can deciper meaning ful text from it. My code is as below:
import java.io.*;
import java.util.*;
import javax.comm.*;
import javax.comm.SerialPort;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
public class NFCReader3 implements Runnable, SerialPortEventListener {
    static CommPortIdentifier portId;
    static Enumeration          portList;
    InputStream              inputStream;
    SerialPort              serialPort;
    Thread              readThread;
    int i=0;
    public static void main(String[] args) {
    boolean              portFound = false;
    String              defaultPort = "COM9";
    portList = CommPortIdentifier.getPortIdentifiers();
    while (portList.hasMoreElements()) {
        portId = (CommPortIdentifier) portList.nextElement();
        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
        if (portId.getName().equals(defaultPort)) {
            System.out.println("Found port: "+defaultPort);
            portFound = true;
            NFCReader3 reader = new NFCReader3();
        }
        }
    }
    if (!portFound) {
        System.out.println("port " + defaultPort + " not found.");
    }
    }
    public NFCReader3() {
    try {
        serialPort = (SerialPort) portId.open("SimpleReadApp2S", 2000);
    } catch (PortInUseException e) {}
    try {
        inputStream = serialPort.getInputStream();
    } catch (IOException e) {}
    try {
        serialPort.addEventListener(this);
    } catch (TooManyListenersException e) {}
    serialPort.notifyOnDataAvailable(true);
    try {
        serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
                       SerialPort.STOPBITS_1,
                       SerialPort.PARITY_NONE);
    } catch (UnsupportedCommOperationException e) {}
    readThread = new Thread(this);
    readThread.start();
    }
    public void run() {
    try {
        Thread.sleep(20000);
    } catch (InterruptedException e) {}
    }
    public void serialEvent(SerialPortEvent event) {
         StringBuffer inputBuffer = new StringBuffer(); 
         int newData = 0;
    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:
        while (newData != -1) { 
            try { 
                newData = inputStream.read();                
                System.out.print(newData);
                System.out.print(" ");
            if (newData == -1) { 
                break; 
            } 
            if ('\r' == (char)newData) { 
           inputBuffer.append('\n'); 
            } else { 
            inputBuffer.append((char)newData); 
            } 
                        } catch (IOException ex) {
                System.out.println("exception");
                System.err.println(ex); 
                return; 
              }
               }     
        break;
© Stack Overflow or respective owner