A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Super_Class 高级黑马   /  2013-5-1 00:33  /  1592 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 Super_Class 于 2013-5-1 21:56 编辑

可以运行。但是结果误差
  1. import java.util.Enumeration;

  2. import javax.comm.CommPortIdentifier;
  3. import javax.comm.NoSuchPortException;

  4. public class RS232 {
  5.         public static void main(String [] args) {
  6.                 System.out.println("start");
  7.                 String portName ;
  8.                 int i = 0;
  9.                 while(true){
  10.                         i++;
  11.                         portName = "COM"+i;
  12.                         try {
  13.                                 CommPortIdentifier portID = CommPortIdentifier.getPortIdentifier(portName);
  14.                                 System.out.println("端口 "+portName+" 存在");

  15.                         } catch (Exception e) {
  16.                                 //e.printStackTrace();
  17.                                 System.out.println("系统没有端口"+portName);
  18.                                 break;
  19.                         }
  20.                 }
  21. //                Enumeration portList = CommPortIdentifier.getPortIdentifiers();
  22. //                while(portList.hasMoreElements()){
  23. //                        CommPortIdentifier portID = (CommPortIdentifier)portList.nextElement();
  24. //                        if(portID.getPortType()==CommPortIdentifier.PORT_SERIAL){
  25. //                                System.out.println("串口: "+portID.getName());
  26. //                        }
  27. //                        else if (portID.getPortType() == CommPortIdentifier.PORT_PARALLEL) {
  28. //                                System.out.println("并口 : "+portID.getName());
  29. //                        }
  30. //                        else {
  31. //                                System.out.println("其他口:  "+portID.getName());
  32. //                        }
  33. //                        System.out.println("start");
  34. //                }
  35.         }
  36. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马