Results 1 to 4 of 4
  1. #1
    spacer is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default Send a string from serial port via bluetooth

    Hi guys, Im studying at university I need help on my project which makes me confused and crazy : ) I m writing an application which can serially connect and send some commands like AT commands to phone. Code below connects me my phone, I got connection however I really cannot implement the function which should work like try-catch and send and receive back commands.

    Hope one have time for a favor and this guy will be so happy..




    package portopener;

    /**
    *
    * @author Orkungen
    */

    import javax.comm.*;
    import java.util.TooManyListenersException;


    public class PhoneListener implements SerialPortEventListener {

    public static void main(String[] args) {

    String portName = "COM40";
    if (args.length > 0) portName = args[0];

    PhoneListener pl = new PhoneListener();

    try {
    CommPortIdentifier cpi = CommPortIdentifier.getPortIdentifier(portName);
    if (cpi.getPortType() == CommPortIdentifier.PORT_SERIAL) {
    SerialPort modem = (SerialPort) cpi.open("Phone Listener", 1000);
    modem.notifyOnRingIndicator(true);
    modem.addEventListener(pl);

    }
    }
    catch (NoSuchPortException e) {
    System.err.println("Usage: java PhoneListener port_name");
    }
    catch (TooManyListenersException e) {
    // shouldn't happen in this
    }
    catch (PortInUseException e) {
    System.err.println(e);
    }

    }

    public void serialEvent(SerialPortEvent evt) {

    System.err.println(evt.getEventType());
    if (evt.getEventType() == SerialPortEvent.RI) {
    System.out.println("The phone is found");
    }

    }


    }

  2. #2
    spacer is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default no answer?

    Hi again everybody, i think no one could find a solution for my case :D

    anyway, bests

  3. #3
    spacer is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default

    Hi again,

    I extended my code by forum, but still it does fail, below is fail message :
    run:
    java.lang.NullPointerException
    1
    at portopener.PhoneListener.writetoport(PhoneListener .java:74)
    at portopener.PhoneListener.main(PhoneListener.java:3 5)
    Java Result: 2147483647
    BUILD SUCCESSFUL (total time: 3 minutes 28 seconds)


    and my extended code is:



    package portopener;

    /**
    *
    * @author Orkungen
    */

    import java.io.InputStream;
    import java.io.OutputStream;
    import javax.comm.*;
    import java.util.TooManyListenersException;


    public class PhoneListener implements SerialPortEventListener {

    static OutputStream outputStream;
    static InputStream inputStream;
    static SerialPort serialPort;




    public static void main(String[] args) {

    String portName = "COM40";
    if (args.length > 0) portName = args[0];
    PhoneListener pl = new PhoneListener();

    try {
    CommPortIdentifier cpi = CommPortIdentifier.getPortIdentifier(portName);
    if (cpi.getPortType() == CommPortIdentifier.PORT_SERIAL) {
    SerialPort modem = (SerialPort) cpi.open("Phone Listener", 1000);
    pl.writetoport();
    modem.notifyOnRingIndicator(true);
    modem.addEventListener(pl);

    }
    }
    catch (NoSuchPortException e) {
    System.err.println("Usage: java PhoneListener COM40");
    }
    catch (TooManyListenersException e) {

    }
    catch (PortInUseException e) {
    System.err.println(e);
    }

    }

    public void serialEvent(SerialPortEvent evt) {

    System.err.println(evt.getEventType());
    if (evt.getEventType() == SerialPortEvent.RI) {
    System.out.println("The phone is found");

    }



    }
    public void writetoport() {
    String s1="AT";
    String s2="\r\n";//enter
    String s3="AT+CMGF=1";//set text mode
    String s4="AT+CSCA=\"+4540390999\"";//service centre number
    String s5="AT+CMGS=\"+4552731228\"";//receiver number, can be extendent
    String s6="<ctrl+z>";//ctrl+z should be merged after text tı be sent
    String messageString = "hello woorld!";
    try {
    // set port parameters
    serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
    SerialPort.STOPBITS_1,
    SerialPort.PARITY_NONE);
    outputStream =serialPort.getOutputStream();
    inputStream = serialPort.getInputStream();
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    try
    {
    System.out.println("1");
    outputStream.write(s1.getBytes()); // AT command
    // System.out.println(s1.getBytes());
    // outputStream.wait(10000);
    outputStream.write(s2.getBytes()); // enter
    Thread.sleep(1000); //sleeping thread
    System.out.println("2");
    // outputStream.wait(1000);
    outputStream.write(s3.getBytes()); //at+cmgf=1 command
    // outputStream.wait(1000);
    outputStream.write(s2.getBytes()); //enter
    Thread.sleep(1000);// thread sleeping
    System.out.println("3");
    // outputStream.wait(1000);
    outputStream.write(s4.getBytes()); //at+csca="<servicecentrenumber>"
    // outputStream.wait(1000);
    outputStream.write(s2.getBytes()); //enter
    Thread.sleep(1000); //thread sleeping
    System.out.println("4");
    // outputStream.wait(1000);
    outputStream.write(s5.getBytes());//at+cmgs="receiver number"
    outputStream.write(s2.getBytes()); //enter
    Thread.sleep(1000); //thread sleeping
    System.out.println("5");
    outputStream.write(messageString.getBytes()); // message
    // outputStream.wait(1000);
    outputStream.write(s6.getBytes());//ctrl+z
    outputStream.write(s2.getBytes()); //enter
    Thread.sleep(1000);
    System.out.println("6");

    // outputStream.wait(1000);
    byte[] readBuffer = new byte[23];
    try
    {
    while (inputStream.available() > 0) {
    //int numBytes = inputStream.read(readBuffer);
    //System.out.println(numBytes);

    // print data

    String result = new String(readBuffer);
    System.out.println("Read: "+result);
    }
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    outputStream.close();
    serialPort.close();
    }
    catch(Exception e)
    {

    }


    }


    }


    indeed i still need help, anyone interested?

  4. #4
    vietnamtour is offline Member
    Join Date
    Dec 2009
    Posts
    1
    Rep Power
    0

Similar Threads

  1. Serial Port
    By radhika in forum New To Java
    Replies: 5
    Last Post: 11-06-2009, 10:40 AM
  2. Serial port - send command respond
    By boss-tech in forum Java Applets
    Replies: 0
    Last Post: 07-23-2009, 12:53 PM
  3. Replies: 4
    Last Post: 02-28-2009, 08:39 PM
  4. rs232 serial port on xp
    By simo_mon in forum New To Java
    Replies: 0
    Last Post: 12-04-2008, 11:25 AM
  5. serial port
    By musiigedeo in forum Advanced Java
    Replies: 0
    Last Post: 07-23-2007, 04:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •