1

I am new to javax.comm and I have a project in which I want to send USSD commands to my modem and receive the response for further processing. In my code below, everything works well, the USSD code is sent, but the program remains infinitely at command: inputStream.read(buffer);

It returns no value and the program remains at that point. Any help?

import java.io.*;
import java.util.*;
import java.lang.*;

import javax.comm.*;

public class SerialWrite implements Runnable, SerialPortEventListener{

    static String output="";

    public void run(){
    }

    static Enumeration portList;
    static CommPortIdentifier portId;
    static String dest = "+923216159133";
    static String messageString = "Hello Testing";
    static InputStream inputStream;
    static SerialPort serialPort;
    static OutputStream outputStream;

    public void serialEvent(SerialPortEvent event){
        switch (event.getEventType()){
            case SerialPortEvent.BI:
            case SerialPortEvent.OE:
            case SerialPortEvent.FE:
            case SerialPortEvent.PE:
            case SerialPortEvent.CD:
            case SerialPortEvent.CTS:
            case SerialPortEvent.DSR:
            case SerialPortEvent.RI:
            case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
                System.out.println("Error");
            break;
            case SerialPortEvent.DATA_AVAILABLE:{
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                String line = "";
                try{
                    while ( (line = reader.readLine()) != null){
                        if(line.equalsIgnoreCase("OK") || (line.indexOf("+CMGS") != -1)){
                            output=line;
                        }
                    Thread.sleep(10);
                    }
                }
                catch (Exception e){
                    System.err.println("Error while reading Port " + e);
                }
            break;
        }
    } //switch
}

public SerialWrite(SerialPort serial){
    try{
        inputStream = serial.getInputStream();
        try{
            serial.addEventListener(this);
        }
        catch (TooManyListenersException e){
            System.out.println("Exception in Adding Listener" + e);
        }
        serial.notifyOnDataAvailable(true);
    }
    catch (Exception ex){
        System.out.println("Exception in getting InputStream" + ex);
    }
}

public static void main(String[] args) throws Exception{
    int i=0;
    String line1 = "AT+CMGF=1\r\n";
    //String line2 = "AT+CMGS=" + "\"" + dest + "\""+"\r\n";
    String line2 = "ATQ0V1E0";
    System.out.println("This " + line2);
    String line3 = messageString;
    String line4 = "<ctrl+z>";
    portList = CommPortIdentifier.getPortIdentifiers();
    while (portList.hasMoreElements()){
        portId = (CommPortIdentifier) portList.nextElement();
        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
            System.out.println("SMS Sending........"+portId.getName());
            if ( portId.getName().equals("COM9")){
                System.out.println("SMS Sending....Port Found");
                try{
                    serialPort = (SerialPort) portId.open("SerialTestApp", 2000);
                    SerialWrite wr = new SerialWrite(serialPort);
                }
                catch (PortInUseException e){
                    System.out.println("Port In Use " + e);
                }
                try{
                    outputStream = serialPort.getOutputStream();
                }
                catch (IOException e){
                    System.out.println("Error writing to output stream " + e);
                }
                try{
                    serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
                }
                catch (UnsupportedCommOperationException e){
                    System.out.println("Error");
                }
                try{
                    System.out.println("It seems OK now");
                    outputStream.write(line2.getBytes());
                    System.out.println("Write Ends. Sleeping Starts...");
                    byte buffer[] = new byte[10000];
                    Thread.sleep(500);
                    System.out.println("Sleeping Ends. Reading Starts...");
                    // read the response from mobile phone
                    int j = inputStream.read(buffer);
                    System.out.println("AT Comand response: "+j+" - "+buffer.toString());
                    /*System.out.println ("done");
                    outputStream.write(line2.getBytes());
                    System.out.println("It seems OK now");
                    outputStream.write(line3.getBytes());
                    outputStream.write(line4.getBytes());
                    System.out.println("This one is output "+output);
                    outputStream.flush();
                    System.out.println("Message Sent!");*/
                }
                catch (IOException e){
                    System.out.println("Error writing message " + e);
                }
            }
        }
    }
}


public static void showText(String Text){
    System.out.println("TEXT "+Text);
    }

public void serialEvent1(SerialPortEvent arg0) {
    // TODO Auto-generated method stub

}
}
Nate Barbettini
  • 51,256
  • 26
  • 134
  • 147
  • Without looking into details: read will hang if there is no input. So you're reading too far. Maybe you can set a timeout, but likely you should think over whether you should stop reading earlier. – qwerty_so Jan 30 '15 at 14:17
  • Please, how can I do that? Do you have a working example? – user3302213 Jan 30 '15 at 14:22
  • Unfortunately no (I'm not a java coder). But I guess Google is your friend: http://stackoverflow.com/questions/804951/is-it-possible-to-read-from-a-inputstream-with-a-timeout – qwerty_so Jan 30 '15 at 15:13

0 Answers0