// Hardware dependant methods for the communication with the mobile phone over serial line // // file: Port.java // used by: Msg.java // // For comments see header of SMS.java. //--------------------------------------------------------------------------------------- package smspack; import java.io.*; import java.rmi.*; import java.util.*; import javax.comm.*; class Port { private static String portName; private static SerialPort port; private static OutputStreamWriter out; private static InputStreamReader in; private static String number; private static String message; private static boolean numbertype; // national or international dialing number /** open the connection via the serial line * @throws Exception */ public static void open() throws Exception { //----- open the connection via the serial line try { CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(SMS.COMPORT); port = (SerialPort)portId.open("SMS Transceiver", 10); // open port // set parameter for serial port port.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); out = new OutputStreamWriter(port.getOutputStream(), "ISO-8859-1"); in = new InputStreamReader(port.getInputStream(), "ISO-8859-1"); System.out.println("open port\n"); // for debugging } // try catch (Exception e) { SMS.showText("could not open port:" + e); System.exit(0); } // catch } // open /** sends an AT command to ME and receives the answer of the ME * @param atcommand AT command for the mobile phone * @return answer string of the ME * @throws java.rmi.RemoteException */ public static String sendAT(String atcommand) throws java.rmi.RemoteException { String s=""; try { Thread.sleep(300); // wait 300 msec [Timing] writeln(atcommand); // send AT command to ME Thread.sleep(300); // wait 300 msec [Timing] s = read(); // get response from ME Thread.sleep(300); // wait 300 msec [Timing] } // try catch (Exception e) { SMS.showText("ERROR: send AT command failed; " + "Command: " + atcommand + "; Answer: " + s + " " + e); } // catch return s; } // sendAT /** write a string to the output buffer * @param s string for the serial output buffer * @throws Exception */ public static void write(String s) throws Exception { out.write(s); out.flush(); } // write /** write a character to the output buffer * @param s character for the serial output buffer * @throws Exception */ public static void write(char[] s) throws Exception { out.write(s); out.flush(); } // write /** write a string with CR at the end to the output buffer * @param s string for the serial output buffer * @throws Exception */ public static void writeln(String s) throws Exception { out.write(s); out.write('\r'); out.flush(); System.out.println("write port: " + s + "\n"); // for debugging } // writeln /** receives a character string from the serial line * @return received character string from ME * @throws Exception * Remark: Some mobile phones don't send one byte immediate after the other * byte to the PC. Thus it is necessary to check (about) 5 times after * a delay that all bytes are received. It is also important to collect * the bytes not too fast, because this can result in some lost bytes. */ public static String read() throws Exception { int n, i; char c; String answer = new String(""); // do { // wait until the first byte is received // Thread.sleep(100); // wait at least 100 msec [Timing] // } while (in.ready() == false); for (i = 0; i < 10; i++) { // look 5 times for character string to receive //----- collect all characters from the serial line while (in.ready()) { // there is a byte available n = in.read(); // get the byte from the serial line if (n != -1) { // one byte received c = (char)n; // convert the received integer to a character answer = answer + c; // collect the characters from the serial line Thread.sleep(1); // wait 1 msec between every collected byte from the mobile phone [Timing] } // if else break; // no more bytes available } // while Thread.sleep(100); // wait 100 msec [Timing] } // for System.out.println("read port: " + answer + "\n"); // for debugging return answer; // give the received string back to the caller } // read /** close the connection via the serial line * @throws Exception */ public static void close() throws Exception { try { port.close(); System.out.println("close port\n"); // for debugging } // try catch (Exception e) { SMS.showText("Error: close port failed: " + e); } // catch } // close } // Port