串口通訊,串口通訊方式
一. 準備工作
1. 點擊此下載java串口通訊相關工具
2. 將RXTXcomm.jar放到 %JAVA_HOME%\jre\lib\ext\ 目錄下,工程中引入該jar包
3. 將rxtxSerial.dll放到 %JAVA_HOME%\jre\bin\ 目錄下
二. 串口相關類
1. 串口類:CommPort.class
1 public class CommPort { 2 /** 3 * 串口名稱,如:COM1、COM2 4 */ 5 private String commPortName=""; 6 7 /** 8 * 串口所有者名稱,一般為應用程式的名稱 9 */10 private String ownerName; 11 12 /**13 * 傳輸速率14 */15 private String baudRate=""; 16 17 /**18 * 資料位元19 */20 private String dataBit=""; 21 22 /**23 * 校正位24 */25 private String partityBit=""; 26 27 /**28 * 停止位29 */30 private String stopBit=""; 31 32 public String getCommPortName() {33 return commPortName;34 }35 36 public void setCommPortName(String commPortName) {37 this.commPortName = commPortName;38 }39 40 public String getOwnerName() {41 return ownerName;42 }43 44 public void setOwnerName(String ownerName) {45 this.ownerName = ownerName;46 }47 48 public String getBaudRate() {49 return baudRate;50 }51 52 public void setBaudRate(String baudRate) {53 this.baudRate = baudRate;54 }55 56 public String getDataBit() {57 return dataBit;58 }59 60 public void setDataBit(String dataBit) {61 this.dataBit = dataBit;62 }63 64 public String getPartityBit() {65 return partityBit;66 }67 68 public void setPartityBit(String partityBit) {69 this.partityBit = partityBit;70 }71 72 public String getStopBit() {73 return stopBit;74 }75 76 public void setStopBit(String stopBit) {77 this.stopBit = stopBit;78 }79 80 }View Code
2. 串口管理員類:CommPortManager.class
1 import gnu.io.CommPortIdentifier; 2 import gnu.io.SerialPort; 3 4 import java.io.BufferedReader; 5 import java.io.BufferedWriter; 6 import java.io.DataInputStream; 7 import java.io.DataOutputStream; 8 import java.io.InputStream; 9 import java.io.InputStreamReader; 10 import java.io.OutputStream; 11 import java.io.OutputStreamWriter; 12 import java.util.Enumeration; 13 14 import com.erp.common.ConfigHolder; 15 16 public class CommPortManager { 17 /** 18 * 串口定義 19 */ 20 private CommPort commPort; 21 22 /** 23 * 串口對象 24 */ 25 private SerialPort serialPort; 26 27 /** 28 * 位元組輸入 29 */ 30 private InputStream in; 31 32 /** 33 * 位元組輸出 34 */ 35 private OutputStream out; 36 37 /** 38 * 字元輸入 39 */ 40 private BufferedReader bufReader; 41 42 /** 43 * 字元輸出 44 */ 45 private BufferedWriter bufWriter; 46 47 /** 48 * 資料輸入 49 */ 50 private DataInputStream dataIn; 51 52 /** 53 * 資料輸出 54 */ 55 private DataOutputStream dataOut; 56 57 /** 58 * 串口是否在使用 59 */ 60 private boolean isUse; 61 62 public CommPortManager(CommPort commPort){ 63 this.commPort = commPort; 64 } 65 66 /** 67 * 開啟串口 68 * @throws Exception 69 */ 70 public void open() throws Exception{ 71 CommPortIdentifier commPortId = CommPortIdentifier.getPortIdentifier(commPort.getCommPortName()); 72 73 // 第一個參數:通常設定為你的應用程式的名字;第二個參數:開啟連接埠逾時的毫秒數 74 serialPort = (SerialPort)commPortId.open(commPort.getOwnerName(), 5000); 75 serialPort.setOutputBufferSize(Integer.valueOf(ConfigHolder.getValue("comm.buffersize"))); 76 77 in = serialPort.getInputStream(); 78 out = serialPort.getOutputStream(); 79 80 bufReader = new BufferedReader(new InputStreamReader(in, "Unicode")); 81 bufWriter = new BufferedWriter(new OutputStreamWriter(out)); 82 83 dataIn = new DataInputStream(in); 84 dataOut = new DataOutputStream(out); 85 86 // 設定串口參數 87 serialPort.setSerialPortParams(Integer.valueOf(commPort.getBaudRate()), Integer.valueOf(commPort.getDataBit()), 88 Integer.valueOf(commPort.getStopBit()), Integer.valueOf(commPort.getPartityBit())); 89 } 90 91 /** 92 * 判斷串口是否可用 93 */ 94 public boolean commPortEnable(){ 95 boolean result = false; 96 Enumeration en = CommPortIdentifier.getPortIdentifiers(); 97 while (en.hasMoreElements()) { 98 CommPortIdentifier portId = (CommPortIdentifier) en.nextElement(); 99 if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL100 && portId.getName().equalsIgnoreCase(commPort.getCommPortName())) {101 result = true;102 }103 }104 return result;105 }106 107 /**108 * 設定串口接收逾時時間109 * @param timeout 逾時時間,單位秒110 * @throws Exception111 */112 public void setReceiveTimeout(int timeout) throws Exception{113 getSerialPort().enableReceiveTimeout(timeout*1000);114 }115 116 public CommPort getCommPort() {117 return commPort;118 }119 120 public SerialPort getSerialPort() {121 return serialPort;122 }123 124 public InputStream getIn() {125 return in;126 }127 128 public OutputStream getOut() {129 return out;130 }131 132 public BufferedReader getBufReader() {133 return bufReader;134 }135 136 public BufferedWriter getBufWriter() {137 return bufWriter;138 }139 140 public DataInputStream getDataIn() {141 return dataIn;142 }143 144 public DataOutputStream getDataOut() {145 return dataOut;146 }147 148 public boolean isUse() {149 return isUse;150 }151 152 public void setUse(boolean isUse) {153 this.isUse = isUse;154 }155 }View Code