如何使用java检测哪个COM端口连接到USB gsm调制解调器

时间:2014-10-05 03:03:37

标签: java serial-port sms smslib

我有一个使用usb gsm调制解调器发送短信的java程序。我需要找到usb gsm调制解调器所连接的com端口。我用Google搜索并找到下面的代码。

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();
      }
   }
}

在此代码中,他们导入了两个类 import com.cubepro.general.CommonConstants; import com.cubepro.util.SendMessage;

但我在哪里可以找到这些com.cubepro?

0 个答案:

没有答案