栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Java自动检测哪个Com端口连接到USB GSM调制解调器

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Java自动检测哪个Com端口连接到USB GSM调制解调器

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/495172.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号