0

I would like to get some help with this:

I want to communicate with a serial device with Java and I have started to play a little bit with java.comm package. My device should be responding to my messages and I don't know how to achieve this. After first message it doesn't work any more. InputStream becomes unavailable.

While debugging I came down to this simple case:

//question no. 1
outputStream.write(first_question);                                                            
outputStream.flush();                    

while (inputStream.available() > 0) {                         
  line = inputStream.read();                        
  System.out.println(line);                                                
}

//question no. 2
outputStream.write(second_question);       
outputStream.flush();

while (inputStream.available() > 0) {                         
   line = this.in.read();                        
   System.out.println(line);  
}

` Thanks for your help and suggestions!

Peter Oram
  • 6,213
  • 2
  • 27
  • 40
GGBB
  • 1
  • 1

1 Answers1

0

If you are using windows machine ensure,

  1. win32com.dll to directory : jdk1.7.0/bin
  2. javax.comm.properties to directory : jdk1.7.0/jre/lib
  3. comm.jar to directory : jdk1.7.0/lib

And Try following code by putting delay using thread after writing to port.

outputStream.write(first_question);                                                            
outputStream.flush();
Thread.sleep(1000);`

byte[] readBuffer = new byte[500];

while (inputStream.available() > 0) { 

  int numBytes = inputStream.read(readBuffer);

  msg = new String(readBuffer, 0, numBytes);

   message += msg;

}

it may help You.

kleopatra
  • 51,061
  • 28
  • 99
  • 211
Nithin CV
  • 7
  • 1
  • 6