Monday, January 3, 2011

CommunicationFlow.java

FOR FIRST PART - CLICK HERE { PART 1 }

package com.jovialjava.example;

public class Common {
    
    public static String appendSTX_ETX_LRC(String request){
        String LRCString = calculateLRC(request + "03");
        System.out.println(LRCString);
        return "02" + request + "03" + LRCString ;
    }
    
    public static String calculateLRC(String inputString){
        /*
         * String is Hex String, need to convert in ASCII.
         */
        inputString = convertHexToString(inputString);
        int checksum = 0;
        char[] chars = inputString.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            checksum ^= chars[i];
        }    
        String val = Integer.toHexString(checksum);        
        if(val.length()< 2){
            val = "0"+ val;
        }
        
        return val;       
    }
    public static String convertHexToString(String hex){
         
          StringBuilder sb = new StringBuilder();
          StringBuilder temp = new StringBuilder();             
          //49204c6f7665204a617661 split into two characters 49, 20, 4c...
          for( int i=0; i<hex.length()-1; i+=2 ){
               //grab the hex in pairs
              String output = hex.substring(i, (i + 2));
              //convert hex to decimal
              int decimal = Integer.parseInt(output, 16);                  
                  sb.append((char)decimal);
//              }
              temp.append(decimal);
          }         
     
          return sb.toString();
      }

    public static String asciiToHex(String ascii){
        StringBuilder hex = new StringBuilder();
        
        for (int i=0; i < ascii.length(); i++) {
            hex.append(Integer.toHexString(ascii.charAt(i)));
        }       
        return hex.toString();
    } 
    
    /*
     * This method will check if LRC received is ok.
     */
    public static boolean verify(String response){
        try {
            String newResponse = Common.convertHexToString(response);
            int lrc = getLRCValue(newResponse);
            response = getLRCString(response);    
            System.out.println(response);
            return verifyLrc(response, lrc);
        } catch (Exception e) {
            System.err.println("!!!ERROR!!! -Exception while calcualting LRC");
            e.printStackTrace();
            return false;
        }
    }
    private static int getLRCValue(String response){        
        byte[] bytes = response.getBytes();
        int tmp=-1;
        boolean flag = false;        
        for(byte b : bytes){            
            if(b == 3){
                flag = true;                
                continue;
            }
            if(flag){
            tmp = b;
            break;
            }            
        }        
        return tmp; 
    }
    
    private static String getLRCString(String response) throws Exception{
        if(response.length() % 2 != 0){
            System.err.println("Response MSG doesn't have even number of chars");
            throw new IllegalArgumentException();
        }
        boolean exit = false;
        boolean start = false;
        String LRCStr = "";
        for( int i=0; i<response.length()-1; i+=2 ){
               //grab the hex in pairs
              String output = response.substring(i, (i + 2));
              if(output.equals("03")){    
                  LRCStr = LRCStr + output;
                  exit = true;
              }else if(output.equals("02")){
                  start = true;
                  continue;
              }
              
              if(!exit){
                  if(start){
                      LRCStr = LRCStr + output;
                  }
              }else{
                  break;
              }
        }
        return LRCStr;
    }
    
    private static boolean verifyLrc(String resp, int lrcVal)
    {
        if(resp == null || resp.length() == 0)
            return false;
            //int lrc_encountered = calculate1byteLRC(resp);
        int lrc_encountered = Integer.parseInt(calculateLRC(resp),16);

            if( lrcVal != lrc_encountered ) {
                System.err.println("Mismatched LRC (expected [" + lrc_encountered + "], encountered ["
                    + lrcVal + "])");
                return false;
            }else{    
                System.out.println("LRC Matched (expected [" + lrc_encountered + "], encountered ["
                    + lrcVal + "])");
                return true;
            }
        
    }
    

}


===========================================================================
Download Both Files from Here

No comments: