This page last changed on May 04, 2014 by gillen.

Hi, I'm new to the forum, I trying to interface my Audio system ROTEL which is controlled by serial port. I have just implemented commands towards amplifier using an interface TCP-RS232 using hex bytes and it work. The protocol for reading status is: send request(6 hex bytes)then amp. response is echo command (6 bytes) + answer(26 bytes) total 32 bytes. I used sensor with command associate but it's not stable in sense of sometime read less bytes than 32. To better test what's happen I modified source java and I've noticed that buffer is 200 chrs so there is no problem with length, it seems that for some reason 'read function' stop early.Any suggestion?
Thank You


readReplay1.txt (text/plain)

What happens if you replace the method with following (untested code so fix where necessary):


private String readReply(java.net.Socket socket) throws IOException 
{
  BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  char[] buffer = new char[200];
     
  int len;
  int offset = 0;
     
  while (true)
  {
    len = bufferedReader.read(buffer, offset, 200-offset);  
    
    if (len == -1)
    {
      break;
    }

    offset += len;
       
    if (offset >= 200)
    {
      offset = 200;

      break;
    }
  }
     
  if (offset > 0) 
  {
    String reply = new String(buffer, 0, offset);
    return reply;
  } 

  else 
  {
    return "";
  }
}

EDIT: removed reference to unknown readChars variable and replaced it with offset instead
EDIT2: fixed a bug

Posted by juha at May 05, 2014 18:38

Thank You Juha, Your suggestion works with a few mod. Here is the code used for testing scope (I'm newer in Java programming so .......)

private String readReply(java.net.Socket socket) throws IOException 
{
  BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  char[] buffer = new char[200];
     
  int len;
  int offset = 0;
     
  while (true)
  {
    len = bufferedReader.read(buffer, offset, 200-offset);  
    offset += len;
    logger.info("offset: " + offset);  
 //   if (len == -1 || offset >= 200)
 //   {
 //     break;
 //   }   
  if (offset >= 32) 
  {
    String reply = new String(buffer, 0, offset);
    return reply;
  } 
}
//  else 
//  {
//    return "";
//  }
}

	
   public void run() {
      
      while (doPoll) {
         // Strip response from control characters (non-ASCII)...
         // Patch provided by Phillip Lavender
         String rawResult = this.requestSocket();
         Pattern p = Pattern.compile("\\p{Cntrl}");
         Matcher m = p.matcher(rawResult);
         String readValue = m.replaceAll("");

         if (regex != null) {
           Pattern regexPattern = Pattern.compile(regex);
           Matcher matcher = regexPattern.matcher(readValue);
           if (matcher.find()) {
             String result = matcher.group();
             logger.info("result of regex evaluation: " + result);
             sensor.update(result);
           } else {
             logger.info("regex evaluation did not find a match");
             sensor.update("N/A");
           }
         } else {
		String s = rawResult;
		int len = s.length();
		logger.info("rawresult: " + rawResult);
		logger.info("Lunghezza s: " + len);
		if (len == 32){
		String g = s.substring(10,23);	//extract 13 CHRS ASCII 
		readValue = g;
		} else {
			readValue = "No data";
			}
           sensor.update(readValue);
         }
         try {
            Thread.sleep(pollingInterval); // We wait for the given pollingInterval before reading socket again
         } catch (InterruptedException e) {
            doPoll = false;
            pollingThread.interrupt();
         }
      }
      logger.debug("*** Out of run method: " + sensor);
   }

}

Posted by gillen at May 07, 2014 20:24

Hmm, that won't work in general case though, even if it would happen to work in your current case.

Posted by juha at May 08, 2014 05:48

I agree with You, mine was only a test, I'm thinking to develop a protocol like Russound or Denon Avr, but for now I have no time.
Thank You

Posted by gillen at May 08, 2014 08:30
Document generated by Confluence on Jun 05, 2016 09:39