Results 1 to 3 of 3
  1. #1
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Post Automatically detect which COM port is connected to a USB GSM Modem using java

    I wrote a Java application that reads and sends SMS messages from a USB GSM modem. I'm using SMSLib (which uses JavaCommAPI), and it runs on Windows. I need to pass in the COM PORT, that the modem appears to be connected to. Now in my application am giving hardcoded value for the COM port number which is not user friendly coz the com port depend on the machine the application is run on.

    Any ideas on how i can do this will be highly appreciated

    cheerz!!
    We Learn Through Mistakes..,
    Manfizy:rolleyes:

  2. #2
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Thumbs up

    Well guyz, i found a solution. Those interested, check out the code below
    Java Code:
    package com.cubepro.util;
    
    import java.util.Collection;
    import java.util.Collections;
    import java.util.Enumeration;
    import java.util.Formatter;
    
    import org.apache.log4j.Logger;
    import org.smslib.helper.CommPortIdentifier;
    
    import com.cubepro.general.CommonConstants;
    
    import com.cubepro.util.SendMessage;
    
    public class CommPortTester {
       private static final String _NO_DEVICE_FOUND = "  no device found";
    
       private final static Formatter _formatter = new Formatter(System.out);
    
       private static Logger log = Logger.getLogger(CommPortTester.class);
    
       static CommPortIdentifier portId;
    
       static Enumeration<CommPortIdentifier> portList;
    
       static int bauds[] = { 9600, 14400, 19200, 28800, 33600, 38400, 56000,
                              57600, 115200 };
    
       public static final String MAINCLASS = "org.smslib.Service";
    
       public CommPortTester() throws Exception {
          Class.forName(MAINCLASS);
       }
    
       /**
        * Wrapper around {@link CommPortIdentifier#getPortIdentifiers()} to be
        * avoid unchecked warnings.
        */
       private static Enumeration<CommPortIdentifier> getCleanPortIdentifiers() {
          return CommPortIdentifier.getPortIdentifiers();
       }
    
       public String testAndQualifyPort() throws Exception {
          String status = CommonConstants.MODEM_STATUS_ERROR;
          SendMessage sendMessage = new SendMessage();
    
          log.debug("\nSearching for devices...");
          portList = getCleanPortIdentifiers();
    
          while (portList.hasMoreElements()) {
             portId = portList.nextElement();
             if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                _formatter.format("%nFound port: %-5s%n", portId.getName());
                try {
                   if(portId.getName()
                   boolean comPortSuccess = sendMessage.doIt(portId.getName());
                   if(comPortSuccess == true){
                      return portId.getName();
                   }
                } catch (final Exception e) {
                   log.debug(" Modem error occured -",e);
                }
             }
          }
          log.debug("\nTest complete.");
          return status;
       }
    
       public static void main(String[]args){
          try{
          CommPortTester tester = new CommPortTester(); 
          tester.testAndQualifyPort();
          }catch(Exception e){
             e.printStackTrace();
          }
       }
    }
    We Learn Through Mistakes..,
    Manfizy:rolleyes:

  3. #3
    Carlitos is offline Member
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Default Re: Automatically detect which COM port is connected to a USB GSM Modem using java

    Hello Manfizy.

    I came across this forum looking for a way to write a java app to send/receive sms messages through a USB GSM modem. Would you be kind to guide me on how to
    go about it. I would really appreciate your help. Thanks in advance

Similar Threads

  1. Detect USB port signal
    By mine0926 in forum Advanced Java
    Replies: 4
    Last Post: 01-28-2011, 06:37 AM
  2. how to detect signal strength of modem?
    By boyaxnxtdoor in forum NetBeans
    Replies: 1
    Last Post: 01-25-2011, 04:11 PM
  3. seek device connected to usb port
    By kievari in forum Advanced Java
    Replies: 2
    Last Post: 08-28-2010, 12:18 AM
  4. Serial port programming using modem and telephone
    By maskey_dipesh in forum Advanced Java
    Replies: 1
    Last Post: 09-03-2009, 12:53 PM
  5. Replies: 4
    Last Post: 02-28-2009, 09:39 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
  •