串口通訊,串口通訊方式
一. 准備工作
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_SERIAL
100 && portId.getName().equalsIgnoreCase(commPort.getCommPortName())) {
101 result = true;
102 }
103 }
104 return result;
105 }
106
107 /**
108 * 設置串口接收超時時間
109 * @param timeout 超時時間,單位秒
110 * @throws Exception
111 */
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