chanda
Posts: 11
Nickname: rcss
Registered: Sep, 2004
About javx.comm.... reading the serial port
Posted: Jan 28, 2005 12:38 AM
Advertisement
My phone is connected to modem through a serial port
I want to read the modem after the phone ring has indicated, I am using javax.comm. package
If phone rings then it shows that phone is ringing
But my problem is, It doesnt reads the data after I pick the phone
Please suggest me what should I do?
. wether i have to go for event listning or use a Thread which continously monitor the serial port ... Thanks in advance
My code
> import javax.comm.*; import java.io.IOException; import java.util.TooManyListenersException; import java.io.*; public class PhoneListener1 implements SerialPortEventListener { SerialPort modem; private InputStream inputStream; public PhoneListener1() { } public void setup() { String portName = "COM3"; try { CommPortIdentifier cpi = CommPortIdentifier.getPortIdentifier(portName); if (cpi.getPortType() == CommPortIdentifier.PORT_SERIAL) { modem = (SerialPort)cpi.open("Phone Listener", 1000); inputStream = modem.getInputStream(); modem.addEventListener(this); modem.notifyOnRingIndicator(true); modem.notifyOnDataAvailable(true); } } catch (NoSuchPortException e) { System.err.println("Usage: java PhoneListener port_name"); } catch (PortInUseException e) { System.err.println(e); } catch (IOException ioe) { System.out.println("in catch3"); } catch (TooManyListenersException e){ } } public void close() { try { modem.removeEventListener(); modem.close(); } catch(Exception e) { System.out.println(e); } } public static void main(String[] args) throws InterruptedException, IOException { System.out.println("in main"); PhoneListener1 pl = new PhoneListener1(); while(true) { pl.setup(); System.out.println("Press ENTER to stop"); System.in.read(new byte[10]); pl.close(); } } public void serialEvent(SerialPortEvent evt) { if (evt.getEventType() == SerialPortEvent.RI) { System.out.println("Ringing..."); } if (evt.getEventType() == SerialPortEvent.DATA_AVAILABLE) { System.out.println("Data available..."); byte[] readBuffer = new byte[8]; try { while (inputStream.available() > 0) { int numBytes = inputStream.read(readBuffer); System.out.println("The read value in integer::" + numBytes); } } catch (IOException ioe) { System.out.println("ReaderClient: Caught IOException: " + ioe.getMessage()); } } } }