Results 1 to 20 of 20
  1. #1
    ali_sakar is offline Member
    Join Date
    Feb 2009
    Posts
    29
    Rep Power
    0

    Question Sending commands to Serial Port(COM1)

    Hi all,

    I am new to Java and has been assigned a task which seems to be quite an Advanced Java stuff.

    I have a GSM modem connected to the serial port(COM1) and I want to send AT commands to the modem(for sending sms). I searched the net but am not able to find an appropriate solution for the same.

    A complete and stepwise solution would be greatly appreciable.

    Thanking in advance.

    Ali.

  2. #2
    wolfcro is offline Member
    Join Date
    Feb 2009
    Location
    Italy
    Posts
    51
    Rep Power
    0

  3. #3
    Join Date
    Oct 2008
    Posts
    73
    Rep Power
    0

    Smile

    i have done same project in my collage but i dont have its code now ...
    i am giving you one link that will hepl you ...
    if you still not clear then i can give you some code 2moro...


    :) but first try this code...


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

    public class SimpleRead implements Runnable, SerialPortEventListener {
    static CommPortIdentifier portId;
    static Enumeration portList;

    InputStream inputStream;
    SerialPort serialPort;
    Thread readThread;

    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("COM1")) {
    // 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);
    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(20000);
    } catch (InterruptedException e) {System.out.println(e);}
    }

    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:
    break;
    case SerialPortEvent.DATA_AVAILABLE:
    byte[] readBuffer = new byte[20];

    try {
    while (inputStream.available() > 0) {
    int numBytes = inputStream.read(readBuffer);
    }
    System.out.print(new String(readBuffer));
    } catch (IOException e) {System.out.println(e);}
    break;
    }
    }
    }
    Last edited by rakesh_n_mehta; 02-11-2009 at 01:56 PM.
    Rakesh Mehta

  4. #4
    ali_sakar is offline Member
    Join Date
    Feb 2009
    Posts
    29
    Rep Power
    0

    Default

    Hi Rakesh,

    Thanx for the update. I tried running the above code but it is not able to import javax.comm and therefore it gives a lot of compilation errors. And I believe this code is to read from the Serial Port whereas I want to send commands to the port. How do I go about?
    I have one more piece of code as below. It gets compiled successfully but it does not send the sms. When I try to send the same commands from hyperterminal, it works fine and sms is also sent.

    package serialport;

    import java.io.BufferedOutputStream;
    import java.io.FileOutputStream;

    public class SerialTest {
    //static byte[] newline[]={(byte)13,(byte)10};

    public static void main( String args[]) {
    Runtime rt = Runtime.getRuntime();
    Process p = null;
    String portname = "COM1";
    // for Win95 : c:\\windows\\command.com
    // c:\\windows\\command\\mode.com
    String cmd[] = {
    "c:\\windows\\system32\\cmd.exe", "/c",
    "start", "/min",
    "c:\\windows\\system32\\mode.com", portname,
    "baud=9600", "parity=n", "data=8",
    "stop=1",
    };
    try {
    p = rt.exec( cmd );
    if( p.waitFor() != 0 ) {
    System.out.println("Error executing command: " + cmd );
    System.exit( -1 );
    }
    byte data[] =
    "AT+CMGS=\"9819143791\",\"Hello there".getBytes();
    FileOutputStream fos = new FileOutputStream( portname );
    BufferedOutputStream bos = new BufferedOutputStream( fos );
    fos.write( data, 0, data.length );
    fos.close();
    }
    catch( Exception e ) {
    e.printStackTrace();
    }
    }
    }

    I found this code from this forum only. Could you pls. tell me that why SMS is not being sent by this code??

    Thanking in advance.

    Regards
    Ali.

  5. #5
    Join Date
    Oct 2008
    Posts
    73
    Rep Power
    0

    Smile

    hey you have to download jar file for communication....
    than ur problem will be solved
    Rakesh Mehta

  6. #6
    Join Date
    Oct 2008
    Posts
    73
    Rep Power
    0

    Default

    have u found comm jar file??
    is code working now???:confused::confused::confused:
    Rakesh Mehta

  7. #7
    ali_sakar is offline Member
    Join Date
    Feb 2009
    Posts
    29
    Rep Power
    0

    Default

    No buddy,

    Still hunting for the same. I am checking on sun.com but till now no luck... could u help in this as well...??

  8. #8
    Join Date
    Oct 2008
    Posts
    73
    Rep Power
    0

    Smile

    ok giving u some file ...try it and infor me later
    Attached Files Attached Files
    Last edited by rakesh_n_mehta; 02-12-2009 at 01:12 PM.
    Rakesh Mehta

  9. #9
    ali_sakar is offline Member
    Join Date
    Feb 2009
    Posts
    29
    Rep Power
    0

    Default

    Thanx a lot rakesh... I am using JDeveloper 11g and now its getting compiled and runs w/o errors. Sorry to trouble you more but do you know AT commands that we use in the Hyperterminal. What I need this class to do is send the below command to COM1.

    AT+CMGS="9819143791" <CR>
    Hi... <Ctrl z>

  10. #10
    Join Date
    Oct 2008
    Posts
    73
    Rep Power
    0

    Smile

    your welcome..

    i have downloaded some file for at commmand.. here they are...
    hope that will help u..

    hey its size is too much ... not able too send u..
    search at command in google u will find some thing other wise give me ur mail add..
    Rakesh Mehta

  11. #11
    ali_sakar is offline Member
    Join Date
    Feb 2009
    Posts
    29
    Rep Power
    0

    Default

    I think you have misunderstood me.. I dont need the AT commands... I want to send the below commands to the modem via serial port.

    AT+CMGS="9819143791" <CR>
    Hi... <Ctrl z>

    I want to know how do i implement this in a class. I have got this code.

    ******************************
    package serialport;

    import java.io.*;
    import java.util.*;
    import javax.comm.*; //for accessing serialport

    public class OwnPort {
    static CommPortIdentifier portId;
    static CommPortIdentifier saveportId;
    static Enumeration portList;
    static SerialPort serialPort;
    static OutputStream outputStream;
    static InputStream inputStream;
    static boolean outputBufferEmptyFlag = false;
    public static void main(String[] args) {
    boolean portFound = false;
    String defaultPort= "COM1";
    System.out.println("Set default port to "+defaultPort);
    portList = CommPortIdentifier.getPortIdentifiers();
    while (portList.hasMoreElements()) {
    portId = (CommPortIdentifier) portList.nextElement();
    if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
    if (portId.getName().equals(defaultPort)) {
    System.out.println("Found port: "+defaultPort);
    portFound = true;
    // init reader thread
    OwnPort op=new OwnPort();
    op.initwritetoport();
    }
    }

    }
    if (!portFound) {
    System.out.println("port " + defaultPort + " not found.");
    }
    }
    public void initwritetoport()
    {
    System.out.println("inside initwriteport");
    String s1="at";
    String s2="\r\n";
    String s3="at+cmgf=1";
    String s4="at+cmgs=\"9819143791\"";
    // char[] s5={’"‘,’9',’3',’4',’6',’4',’6',’2',’1',’6',’5',’" ‘};
    String s6="32";
    // for(int i=0;i<s5.length;i++)
    // {
    // System.out.println(s5.toString());
    // }
    // System.out.println("s6="+s6);
    String messageString = "hey ee mesage GSM nunchi vachindi!";

    // get the outputstream
    try {
    serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
    } catch (PortInUseException e) {}

    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+cmgs="<mobilenumber>"
    // outputStream.wait(1000);
    outputStream.write(s2.getBytes()); //enter
    Thread.sleep(1000); //thread sleeping
    System.out.println("–4–");
    // outputStream.wait(1000);
    outputStream.write(messageString.getBytes()); // message
    // outputStream.wait(1000);
    outputStream.write(s2.getBytes());
    Thread.sleep(1000);
    outputStream.write(s6.getBytes());
    Thread.sleep(1000);
    System.out.println("–5–");

    // 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)
    {

    }

    }
    }

    ******************************
    When I run the above code, I get this o/p

    Set default port to COM1
    port COM1 not found.
    Process exited with exit code 0.

    but when if I try from hyperterminal, sms goes.
    I m unable to understand the problem.

  12. #12
    ali_sakar is offline Member
    Join Date
    Feb 2009
    Posts
    29
    Rep Power
    0

    Default

    Its not going in the while loop

    while (portList.hasMoreElements())
    but why its not going that I dont understand.

  13. #13
    Join Date
    Oct 2008
    Posts
    73
    Rep Power
    0

    Smile

    hey in this type of project you have to check so much things...
    like mobile you r using have internal memory or memory card...
    command supported in that or not that typr of error u can get in ur consol area.. but they r that..
    Rakesh Mehta

  14. #14
    ali_sakar is offline Member
    Join Date
    Feb 2009
    Posts
    29
    Rep Power
    0

    Default

    Hi rakesh,

    I have achieved almost everything except for sending ctrl+z key. Could you help in that??

    I am able to send commands and text to the port and then when I connect to hyperterminal and press ctrl+z all the text which i sent from the java class goes in the sms. Are you mumbai based.. is it possible to talk on phone?

    Regards
    Ali.

  15. #15
    ali_sakar is offline Member
    Join Date
    Feb 2009
    Posts
    29
    Rep Power
    0

    Default

    Hey Rakesh,

    I have done it. Now its working using JDeveloper. Now I am trying to send it using command line. Will bug you if I face issues in achieving it via command line. :)

    Thanx a lot for all the replies.

    Regards
    Ali.

  16. #16
    Join Date
    Oct 2008
    Posts
    73
    Rep Power
    0

    Smile

    thats great..
    Rakesh Mehta

  17. #17
    ibanhoe is offline Member
    Join Date
    Apr 2009
    Posts
    1
    Rep Power
    0

    Default Hi guys

    Im new in this forum

    I was looking for info about the Java app using AT commands and i found this one

    I can send commands to a SMS sender device, but the event thta the Port Commm return back to me is OUTPUT_BUFFER_EMPTY always.

    I could run my app a few months ago, but now i don't know why it does not run...

    Could you give a suggestion about what can i looking for?

    Thank you very much

    Regards!!!

  18. #18
    kalu143_2003 is offline Member
    Join Date
    Aug 2009
    Posts
    1
    Rep Power
    0

    Default

    Dear Rakesh and ali

    I connected internal modem in my computer and when i see the properties of modem then it display that modem is connected to COM 6.

    The main problem is I have already written java code to connect to modem but the code line;

    while(portList.hasMoreElements())

    always return false. due to this the execution doesnt enter the while loop.

    Please write me why is it returning false though there is modem in com port 6.

    please reply . I am from nepal

    Thank you
    dipesh

  19. #19
    jurez is offline Member
    Join Date
    Sep 2009
    Location
    bangalore india
    Posts
    1
    Rep Power
    0

    Default my project also same processing and sending sms

    i used gsm modem with AT command using java net bean which i include three file which in can download by comm.jar in this u find three file u should allocate according by path.


    so my project is processing sms from sms modem and send the sms according to the received sms so if u want code of me i will send that u for sending and receiving command from com1 using RS232 with javax.comm header file

  20. #20
    iwonawas is offline Member
    Join Date
    Oct 2009
    Posts
    1
    Rep Power
    0

    Default

    hi
    i'm new in this forum...
    i have big problem with sending only two bytes to serial port...
    if my device doesn't respond on my these two bytes i must send new two bytes (the same two bytes)...
    and unfortunatly this second sending doesn' work...app has blocked...killing this app doesn't work...it is killed when i plug out device...i don't receive error...nothing...
    but if i send once this two bytes and device respond it is everything ok...i don't know what i do wrong...maybe i must clear sth?
    please help me!!!!!!

Similar Threads

  1. Serial Port
    By radhika in forum New To Java
    Replies: 5
    Last Post: 11-06-2009, 11:40 AM
  2. Accessing Serial port using JDK 1.6
    By hasani6leap in forum Advanced Java
    Replies: 5
    Last Post: 01-24-2009, 04:23 AM
  3. Access Serial port
    By hasani6leap in forum Advanced Java
    Replies: 0
    Last Post: 02-12-2008, 10:45 AM
  4. Writing to serial port (com1)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-05-2008, 10:02 AM
  5. serial port
    By musiigedeo in forum Advanced Java
    Replies: 0
    Last Post: 07-23-2007, 05: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
  •