Results 1 to 8 of 8
  1. #1
    lim.th is offline Member
    Join Date
    Jul 2014
    Posts
    4
    Rep Power
    0

    Default I need to help about reading COM port on GPRS Modem

    Hi everybody!
    I'm a new in java program. I need to help about reading COM port on GPRS Modem. I plug the GPRS Modem in my PC and it has connected. I use Eclipse and create a project and new a file a.java, i collect this following code from the internet and paste it into the a.java file.

    Java Code:
    package serial;
    
    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= "COM4";
    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)
    {
    
    }
    
    }
    }

    You can see the result here



    I don't know the result why return " Set default port to COM4. port COM4 not found."
    Maybe somthing wrong here? Please tell me solution!
    Thanks first

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: I need to help about reading COM port on GPRS Modem

    Use getPortIdentifiers to find out what's actually available.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    lim.th is offline Member
    Join Date
    Jul 2014
    Posts
    4
    Rep Power
    0

    Default Re: I need to help about reading COM port on GPRS Modem

    Java Code:
    package serial;
    
    import java.util.*;
    import javax.comm.*;
    //import com.sun.comm.Win32Driver;
    
    public class Communication {
    
              public static void main(String[] args) {
                   //initialisation du driver
                   //Win32Driver w32Driver= new Win32Driver();
                   //w32Driver.initialize();
                   
                   Enumeration portList=CommPortIdentifier.getPortIdentifiers();
                   System.out.println(portList.nextElement());
                   System.out.println(portList.hasMoreElements());
                   
                   CommPortIdentifier portId;
                   while (portList.hasMoreElements()){
                        portId=(CommPortIdentifier)portList.nextElement();
                        System.out.println(portId.getName());
                        //getPortIdentifiers
                   }
              }
         
    }
    Result:
    ==>
    null
    false

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: I need to help about reading COM port on GPRS Modem

    StackOverflow to the rescue?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    lim.th is offline Member
    Join Date
    Jul 2014
    Posts
    4
    Rep Power
    0

    Default Re: I need to help about reading COM port on GPRS Modem

    Thanks Tolls! I have read some topics which have content like the link you posted to me. I'm very happy because i fixed my error.

    First step: read com port of GPRS Modem -> completed

    My purpose is: i can use my mobile phone call to GPRS Modem which is pluged in PC and of course i like to the message voice, the message time, phone number and something like that can be wrote to java program. But i dont know next step i will do. Please give me the direction so that i can finish my dream!

    If u have some projects like this, please share for me! Thanks u first!
    My email: liemth_oracle@yahoo.com

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: I need to help about reading COM port on GPRS Modem

    tip: the javax.comm package is long dead; it wasn't created through a jsr, its api sucks etc. etc. Better use the RXTX package; although it uses the same lousy api, it is actively maintained and contains far less bugs.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: I need to help about reading COM port on GPRS Modem

    Quote Originally Posted by JosAH View Post
    tip: the javax.comm package is long dead; it wasn't created through a jsr, its api sucks etc. etc. Better use the RXTX package; although it uses the same lousy api, it is actively maintained and contains far less bugs.

    kind regards,

    Jos
    I had a niggling suspicion that was the case, but was too busy to go looking it up.
    Surprised that SO thread (from 2013?) didn't mention it...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: I need to help about reading COM port on GPRS Modem

    Quote Originally Posted by Tolls View Post
    I had a niggling suspicion that was the case, but was too busy to go looking it up.
    Surprised that SO thread (from 2013?) didn't mention it...
    Sun maintained the package a bit longer for Linux boxes but cut off everything for MS Windows boxes (MS had changed their 'interpretation' for usb devices); RXTX is still alive and kicking ... if people don't know about it, they include that (old) code in their own code and have to live with the bugs ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 2
    Last Post: 11-30-2011, 07:36 AM
  2. Reading used port using socket
    By vishrut_n_shah in forum Networking
    Replies: 0
    Last Post: 11-03-2011, 06:12 PM
  3. Serial port programming using modem and telephone
    By maskey_dipesh in forum Advanced Java
    Replies: 1
    Last Post: 09-03-2009, 11:53 AM
  4. Replies: 4
    Last Post: 02-28-2009, 08:39 PM
  5. reading from microphone port
    By chintito in forum Advanced Java
    Replies: 0
    Last Post: 01-12-2008, 04:13 AM

Posting Permissions

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