简体   繁体   English

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

[英]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. 我写了一个Java应用程序,它从USB GSM调制解调器读取和发送SMS消息。 I'm using SMSLib (which uses JavaCommAPI), and it runs on Windows. 我正在使用SMSLib(使用JavaCommAPI),它在Windows上运行。 I need to pass in the COM PORT, that the modem appears to be connected to. 我需要传入COM PORT,调制解调器似乎已连接到。

So far, I've been looking up the COM PORT manually using the Windows Device Manager, and write it into a properties file. 到目前为止,我一直在使用Windows设备管理器手动查找COM PORT,并将其写入属性文件。 I'm wondering if there's a way to detect which COM PORT, the modem is connected to programmatically? 我想知道是否有办法检测哪个COM PORT,调制解调器以编程方式连接?

  1. It'll save the trouble of looking it up every time 它可以省去每次查找它的麻烦
  2. The port number changes if I unplug/replug it sometimes 如果我有时拔下/重新插入它,端口号会改变

Thanks!! 谢谢!!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM