title: Serial Communication using NFC reader using java
body: <p>I have a NFC reader that comes with SDK for communication.I can use VB, VC++ or C# with the dll provided by the vendor to communicate using USB or RS232. The reader can be communicated using C/C++ over RS 232 without using the SDK. I am trying to use java for serial communication. I am able to send the command using RS 232 but I am not getting the response from the reader. For example I send one command to detect card presence. The reader should respond back with card serial number if card is present else will return some error code. But I am getting nothing. Attached here is my sample code to detect card. Just let me know if the code is correct or I need to change the approach for communication with the reader.</p>
  <pre><code>package com.pramod.serialcomm;
  
  import gnu.io.CommPort;
  
  import gnu.io.CommPortIdentifier;
  
  import gnu.io.SerialPort;
  
  import gnu.io.SerialPortEvent;
  
  import gnu.io.SerialPortEventListener;
  
  import java.io.IOException;
  
  import java.io.InputStream;
  
  import java.io.OutputStream;`enter code here`
  
  public class TwoWaySerialComm {
      InputStream in ;
      OutputStream out;
  
      void connect( String portName ) throws Exception {
          CommPortIdentifier portIdentifier = CommPortIdentifier
              .getPortIdentifier( portName );
          if( portIdentifier.isCurrentlyOwned() ) {
            System.out.println( &quot;Error: Port is currently in use&quot; );
          } else {
            int timeout = 2000;
            CommPort commPort = portIdentifier.open( this.getClass().getName(), timeout );
            System.out.println( &quot;Success: Port currently in use : &quot; + commPort );
            if( commPort instanceof SerialPort ) {
              SerialPort serialPort = ( SerialPort )commPort;
              serialPort.setSerialPortParams( 115200,
                                              SerialPort.DATABITS_8,
                                              SerialPort.STOPBITS_1,
                                              SerialPort.PARITY_NONE );
  
               in = serialPort.getInputStream();
               out = serialPort.getOutputStream();
  
              ( new Thread( new SerialReader( in ) ) ).start();
              ( new Thread( new SerialWriter( out ) ) ).start();
  
  
            } else {
              System.out.println( &quot;Error: Only serial ports are handled by this example.&quot; );
            }
          }
        }
  
        public static class SerialReader implements  SerialPortEventListener,Runnable {
  
          InputStream in;
          private byte[] buffer = new byte[1024];
          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 ) ) &gt; -1 ) {
                System.out.println(&quot;reading : &quot; +  new String( buffer, 0, len ) );
              }
            } catch( IOException e ) {
              e.printStackTrace();
            }
  
          }
  
          @Override
          public void serialEvent(SerialPortEvent arg0) {
              
              int data;
              try {
                  int len = 0;
                  while ((data = in.read()) &gt; -1) {
                      if (data == '\n') {
                          break;
                      }
                      buffer[len++] = (byte) data;
                  }
                  synchronized (this){
                      System.out.println(new String(buffer, 0, len)
                      );
                  }
              } catch (IOException e) {
                  e.printStackTrace();
                  System.exit(-1);
              }
          }
        }
  
        public static class SerialWriter implements Runnable {
  
          OutputStream out;
  
          public SerialWriter( OutputStream out ) {
            this.out = out;
          }
  
          public void run() {
            try {
              int c = 0;
              while( ( c = System.in.read() ) &gt; -1 ) {
                  
                this.out.write( c );
              }
            } catch( IOException e ) {
                try {
                  Thread.sleep(1000);
              } catch (InterruptedException e1) {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
              }
              e.printStackTrace();
            }
          }
        }
  
        public static void main( String[] args ) {
          try {
              TwoWaySerialComm commTwo = new TwoWaySerialComm() ;
              commTwo.connect(&quot;COM10&quot;);
              String str = &quot;4C00000100&quot;;  // Command to detect card presence by the NFC reader
              commTwo.out.write(str.getBytes());
              System.out.println(&quot;writting : &quot; + str);
          } catch( Exception e ) {
            e.printStackTrace();
          }
        }
  }
  </code></pre>
  <p>Thanks
  Pramod</p>