Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用java检测哪个com端口连接到USB gsm调制解调器_Java_Serial Port_Sms_Smslib - Fatal编程技术网

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

如何使用java检测哪个com端口连接到USB gsm调制解调器,java,serial-port,sms,smslib,Java,Serial Port,Sms,Smslib,我有一个使用usb gsm调制解调器发送短信的java程序。我需要找到usb gsm调制解调器连接的com端口。我在谷歌上搜索了一下,找到了下面的代码 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

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

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 java.util.Collection;
导入java.util.Collections;
导入java.util.Enumeration;
导入java.util.Formatter;
导入org.apache.log4j.Logger;
导入org.smslib.helper.CommPortIdentifier;
导入com.cubepro.general.CommonConstants;
导入com.cubepro.util.SendMessage;
公共类通信测试仪{
私有静态最终字符串\u NO\u DEVICE\u FOUND=“NO DEVICE FOUND”;
私有最终静态格式化程序_Formatter=新格式化程序(System.out);
私有静态记录器log=Logger.getLogger(CommPortTester.class);
静态通信识别器端口;
静态枚举端口列表;
静态整数波特率[]={9600,14400,19200,28800,33600,38400,56000,
57600, 115200 };
公共静态最终字符串MAINCLASS=“org.smslib.Service”;
公共CommPortTester()引发异常{
Class.forName(主类);
}
/**
*将{@link CommPortIdentifier#getPortIdentifiers()}包装为
*避免未经检查的警告。
*/
私有静态枚举getCleanPortIdentifiers(){
返回CommPortIdentifier.getPortIdentifiers();
}
公共字符串testAndQualifyPort()引发异常{
字符串状态=CommonConstants.MODEM\u status\u错误;
SendMessage SendMessage=新建SendMessage();
log.debug(“\n搜索设备…”);
portList=getCleanPortIdentifiers();
while(portList.hasMoreElements()){
portId=portList.nextElement();
if(portId.getPortType()==CommPortIdentifier.PORT_串行){
_formatter.format(“%n查找端口:%-5s%n”,portId.getName());
试一试{
if(portId.getName()
boolean comPortSuccess=sendMessage.doIt(portId.getName());
if(comPortSuccess==true){
返回portId.getName();
}
}捕获(最终异常e){
调试(“出现调制解调器错误-”,e);
}
}
}
log.debug(“\n测试完成”);
返回状态;
}
公共静态void main(字符串[]args){
试一试{
CommPortTester=新的CommPortTester();
tester.testAndQualifyPort();
}捕获(例外e){
e、 printStackTrace();
}
}
}
在这段代码中,他们导入了两个类 导入com.cubepro.general.CommonConstants; 导入com.cubepro.util.SendMessage


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

你在org.apache.log4j中在哪里“找到”了Logger类?回答这个问题,你可能会找到你问题的答案。我导入了jar文件org.apache.log4jSo,你需要一个包含你正在寻找的类的jar文件。要么搜索这样一个jar,要么自己编写这两个类。Pe编写CommPortTester的rhaps也编写了CommonConstants和SendMessage,但没有与您共享。顺便说一句,您导入的是类/包,而不是jar文件。