Hello everyone,
I am just trying my hands in Serial port programming using javax.comm package.
I am sorry if I will not be able to explain you my scenario but I will try my best to explain my problem.
I have 56k internal modem connected to my computer and its assigned with COM 6. Telephone is connected to the modem. I want to write a simple application which will hook the telephone after 2 ring and receive the message which is sent through the telephone line.
I found few example in the internet but those examples cant hook the telephone. On the output screen it will only display as:
RING
RING
RING
RING
On the another end, I have device which will dial to the phone number connected to my computer modem. What My device will expect is, my program needs to hook the call after 2 ring then it will send some message to my program.My program needs to hook the call after 2 ring and need to receive the message comming from device after 2 ring.
One of the example is;
import java.io.*; import java.util.*; import javax.comm.*; public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier portId; static Enumeration portList; static OutputStream outputStream; static InputStream inputStream; FileInputStream fis = null; SerialPort serialPort; Thread readThread; int ch; public static void main(String[] args) { portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equals("COM6")) { System.out.println("checked"); // if (portId.getName().equals("/dev/term/a")) { SimpleRead reader = new SimpleRead(); } } } } public SimpleRead() { try { serialPort = (SerialPort) portId.open("SimpleReadApp", 2000); } catch (PortInUseException e) {System.out.println(e);} try { inputStream = serialPort.getInputStream(); } catch (IOException e) {System.out.println(e);} try { serialPort.addEventListener(this); } catch (TooManyListenersException e) {System.out.println(e);} serialPort.notifyOnDataAvailable(true); serialPort.notifyOnCarrierDetect(true); serialPort.notifyOnDataAvailable(true); serialPort.notifyOnBreakInterrupt(true); serialPort.notifyOnCTS(true); serialPort.notifyOnDSR(true); serialPort.notifyOnFramingError(true); serialPort.notifyOnOutputEmpty(true); serialPort.notifyOnOverrunError(true); serialPort.notifyOnParityError(true); serialPort.notifyOnRingIndicator(true); try { serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException e) {System.out.println(e);} readThread = new Thread(this); readThread.start(); } public void run() { try { Thread.sleep(2000); } catch (InterruptedException e) {System.out.println(e);} } public void serialEvent(SerialPortEvent event) { switch(event.getEventType()) { case SerialPortEvent.BI: System.out.println("Break Interrupt"); break; case SerialPortEvent.OE: case SerialPortEvent.FE: case SerialPortEvent.PE: case SerialPortEvent.CTS: case SerialPortEvent.DSR: case SerialPortEvent.RI: System.out.println("Pick up the receiver."); if( event.getNewValue() ) { System.out.println("Ring Indicator On"); } else { System.out.println("Ring Indicator Off"); } break; case SerialPortEvent.CD: if( event.getNewValue() ) { System.out.println("Connected"); } else { System.out.println("Disconnected"); System.exit(0); } break; case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break; case SerialPortEvent.DATA_AVAILABLE: try { // outputStream.write("ata".getBytes()); byte[] readBuffer = new byte[5048]; while (inputStream.available() > 0) { // outputStream.write("ata".getBytes()); // to hook the phone // System.out.println("ram"); int numBytes = inputStream.read(readBuffer); } System.out.print(new String(readBuffer)); outputStream.write("ata".getBytes()); } catch (IOException e) {System.out.println(e);} break; } } }
It would be very helpful if anyone can help me in this issue.
Thanks in advance.