標籤:通訊 cat class ret bin code return 平台
RXTX是javacomm串口通訊的一個擴充
RXTX開發所需檔案的:http://rxtx.qbang.org/wiki/index.php/Download
解壓之後可以看到支援各個平台的檔案夾:
如果java的安裝路徑為D:\java\jre7,則將選中的檔案夾中的rxtxSerial.dll複製到D:\java\jre7\bin\,
將RXTXcomm.jar複製到D:java\jre\lib\ext\
然後在項目的lib包中引入RXTXcomm.jar。
1 /** 2 * 擷取電腦串口 3 * @return 4 * @throws Exception 5 * RXTX 6 */ 7 public List<ComBean> listAllCom() throws Exception { 8 List<ComBean> listCom = new ArrayList<>(); 9 CommPortIdentifier portId;10 Enumeration<?> portList;11 try {12 portList = CommPortIdentifier.getPortIdentifiers();13 while (portList.hasMoreElements()) {14 CarManageBean car = new CarManageBean();15 portId = (CommPortIdentifier) portList.nextElement();16 if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {17 car.setCom(portId.getName());18 listCom.add(car);19 }20 }21 if (listCom.size() == 0) {22 CarManageBean car = new CarManageBean();23 car.setCom("未找到串口!");24 listCom.add(car);25 }26 } catch (Exception e) {27 e.printStackTrace();28 }29 return listCom;30 }
使用RXTX擷取電腦串口